• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
71