clang-tools  16.0.0git
DraftStore.h
Go to the documentation of this file.
1 //===--- DraftStore.h - File contents container -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H
11 
12 #include "support/Path.h"
13 #include "clang/Basic/LLVM.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/Support/VirtualFileSystem.h"
16 #include <mutex>
17 #include <string>
18 #include <vector>
19 
20 namespace clang {
21 namespace clangd {
22 
23 /// A thread-safe container for files opened in a workspace, addressed by
24 /// filenames. The contents are owned by the DraftStore.
25 /// Each time a draft is updated, it is assigned a version. This can be
26 /// specified by the caller or incremented from the previous version.
27 class DraftStore {
28 public:
29  struct Draft {
30  std::shared_ptr<const std::string> Contents;
31  std::string Version;
32  };
33 
34  /// \return Contents of the stored document.
35  /// For untracked files, a llvm::None is returned.
36  llvm::Optional<Draft> getDraft(PathRef File) const;
37 
38  /// \return List of names of the drafts in this store.
39  std::vector<Path> getActiveFiles() const;
40 
41  /// Replace contents of the draft for \p File with \p Contents.
42  /// If version is empty, one will be automatically assigned.
43  /// Returns the version.
44  std::string addDraft(PathRef File, llvm::StringRef Version,
45  StringRef Contents);
46 
47  /// Remove the draft from the store.
48  void removeDraft(PathRef File);
49 
50  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> asVFS() const;
51 
52 private:
53  struct DraftAndTime {
54  Draft D;
55  std::time_t MTime;
56  };
57  mutable std::mutex Mutex;
58  llvm::StringMap<DraftAndTime> Drafts;
59 };
60 
61 } // namespace clangd
62 } // namespace clang
63 
64 #endif
clang::clangd::DraftStore
A thread-safe container for files opened in a workspace, addressed by filenames.
Definition: DraftStore.h:27
Path.h
clang::clangd::DraftStore::addDraft
std::string addDraft(PathRef File, llvm::StringRef Version, StringRef Contents)
Replace contents of the draft for File with Contents.
Definition: DraftStore.cpp:74
clang::clangd::DraftStore::Draft::Version
std::string Version
Definition: DraftStore.h:31
clang::clangd::DraftStore::removeDraft
void removeDraft(PathRef File)
Remove the draft from the store.
Definition: DraftStore.cpp:85
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
clang::clangd::DraftStore::getActiveFiles
std::vector< Path > getActiveFiles() const
Definition: DraftStore.cpp:28
clang::clangd::DraftStore::getDraft
llvm::Optional< Draft > getDraft(PathRef File) const
Definition: DraftStore.cpp:18
clang::clangd::DraftStore::Draft::Contents
std::shared_ptr< const std::string > Contents
Definition: DraftStore.h:30
clang::clangd::DraftStore::asVFS
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > asVFS() const
Definition: DraftStore.cpp:116
clang::clangd::DraftStore::Draft
Definition: DraftStore.h:29
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27