150
|
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 "Path.h"
|
|
13 #include "Protocol.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 class DraftStore {
|
|
27 public:
|
|
28 /// \return Contents of the stored document.
|
|
29 /// For untracked files, a llvm::None is returned.
|
|
30 llvm::Optional<std::string> getDraft(PathRef File) const;
|
|
31
|
|
32 /// \return List of names of the drafts in this store.
|
|
33 std::vector<Path> getActiveFiles() const;
|
|
34
|
|
35 /// Replace contents of the draft for \p File with \p Contents.
|
|
36 void addDraft(PathRef File, StringRef Contents);
|
|
37
|
|
38 /// Update the contents of the draft for \p File based on \p Changes.
|
|
39 /// If a position in \p Changes is invalid (e.g. out-of-range), the
|
|
40 /// draft is not modified.
|
|
41 ///
|
|
42 /// \return The new version of the draft for \p File, or an error if the
|
|
43 /// changes couldn't be applied.
|
|
44 llvm::Expected<std::string>
|
|
45 updateDraft(PathRef File,
|
|
46 llvm::ArrayRef<TextDocumentContentChangeEvent> Changes);
|
|
47
|
|
48 /// Remove the draft from the store.
|
|
49 void removeDraft(PathRef File);
|
|
50
|
|
51 private:
|
|
52 mutable std::mutex Mutex;
|
|
53 llvm::StringMap<std::string> Drafts;
|
|
54 };
|
|
55
|
|
56 } // namespace clangd
|
|
57 } // namespace clang
|
|
58
|
|
59 #endif
|