clang-tools  11.0.0
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 "Protocol.h"
13 #include "support/Path.h"
14 #include "clang/Basic/LLVM.h"
15 #include "llvm/ADT/StringMap.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. This class supports
25 /// both whole and incremental updates of the documents.
26 /// Each time a draft is updated, it is assigned a version number. This can be
27 /// specified by the caller or incremented from the previous version.
28 class DraftStore {
29 public:
30  struct Draft {
31  std::string Contents;
32  int64_t Version = -1;
33  };
34 
35  /// \return Contents of the stored document.
36  /// For untracked files, a llvm::None is returned.
37  llvm::Optional<Draft> getDraft(PathRef File) const;
38 
39  /// \return List of names of the drafts in this store.
40  std::vector<Path> getActiveFiles() const;
41 
42  /// Replace contents of the draft for \p File with \p Contents.
43  /// If no version is specified, one will be automatically assigned.
44  /// Returns the version.
45  int64_t addDraft(PathRef File, llvm::Optional<int64_t> Version,
46  StringRef Contents);
47 
48  /// Update the contents of the draft for \p File based on \p Changes.
49  /// If a position in \p Changes is invalid (e.g. out-of-range), the
50  /// draft is not modified.
51  /// If no version is specified, one will be automatically assigned.
52  ///
53  /// \return The new version of the draft for \p File, or an error if the
54  /// changes couldn't be applied.
55  llvm::Expected<Draft>
56  updateDraft(PathRef File, llvm::Optional<int64_t> Version,
57  llvm::ArrayRef<TextDocumentContentChangeEvent> Changes);
58 
59  /// Remove the draft from the store.
60  void removeDraft(PathRef File);
61 
62 private:
63  mutable std::mutex Mutex;
64  llvm::StringMap<Draft> Drafts;
65 };
66 
67 } // namespace clangd
68 } // namespace clang
69 
70 #endif
clang::clangd::DraftStore
A thread-safe container for files opened in a workspace, addressed by filenames.
Definition: DraftStore.h:28
Path.h
Contents
llvm::StringRef Contents
Definition: DraftStoreTests.cpp:22
Changes
tooling::Replacements Changes
Definition: Format.cpp:109
clang::clangd::DraftStore::removeDraft
void removeDraft(PathRef File)
Remove the draft from the store.
Definition: DraftStore.cpp:131
Protocol.h
clang::clangd::DraftStore::addDraft
int64_t addDraft(PathRef File, llvm::Optional< int64_t > Version, StringRef Contents)
Replace contents of the draft for File with Contents.
Definition: DraftStore.cpp:50
clang::clangd::DraftStore::getActiveFiles
std::vector< Path > getActiveFiles() const
Definition: DraftStore.cpp:27
clang::clangd::DraftStore::Draft::Contents
std::string Contents
Definition: DraftStore.h:31
clang::clangd::DraftStore::getDraft
llvm::Optional< Draft > getDraft(PathRef File) const
Definition: DraftStore.cpp:17
clang::clangd::DraftStore::Draft::Version
int64_t Version
Definition: DraftStore.h:32
clang::clangd::DraftStore::Draft
Definition: DraftStore.h:30
clang::clangd::CompletionItemKind::File
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::DraftStore::updateDraft
llvm::Expected< Draft > updateDraft(PathRef File, llvm::Optional< int64_t > Version, llvm::ArrayRef< TextDocumentContentChangeEvent > Changes)
Update the contents of the draft for File based on Changes.
Definition: DraftStore.cpp:60