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
|
173
|
12 #include "support/Path.h"
|
150
|
13 #include "clang/Basic/LLVM.h"
|
|
14 #include "llvm/ADT/StringMap.h"
|
221
|
15 #include "llvm/Support/VirtualFileSystem.h"
|
150
|
16 #include <mutex>
|
252
|
17 #include <optional>
|
150
|
18 #include <string>
|
|
19 #include <vector>
|
|
20
|
|
21 namespace clang {
|
|
22 namespace clangd {
|
|
23
|
|
24 /// A thread-safe container for files opened in a workspace, addressed by
|
221
|
25 /// filenames. The contents are owned by the DraftStore.
|
|
26 /// Each time a draft is updated, it is assigned a version. This can be
|
173
|
27 /// specified by the caller or incremented from the previous version.
|
150
|
28 class DraftStore {
|
|
29 public:
|
173
|
30 struct Draft {
|
221
|
31 std::shared_ptr<const std::string> Contents;
|
|
32 std::string Version;
|
173
|
33 };
|
|
34
|
150
|
35 /// \return Contents of the stored document.
|
252
|
36 /// For untracked files, a std::nullopt is returned.
|
|
37 std::optional<Draft> getDraft(PathRef File) const;
|
150
|
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.
|
221
|
43 /// If version is empty, one will be automatically assigned.
|
173
|
44 /// Returns the version.
|
221
|
45 std::string addDraft(PathRef File, llvm::StringRef Version,
|
|
46 StringRef Contents);
|
150
|
47
|
|
48 /// Remove the draft from the store.
|
|
49 void removeDraft(PathRef File);
|
|
50
|
221
|
51 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> asVFS() const;
|
|
52
|
150
|
53 private:
|
221
|
54 struct DraftAndTime {
|
|
55 Draft D;
|
|
56 std::time_t MTime;
|
|
57 };
|
150
|
58 mutable std::mutex Mutex;
|
221
|
59 llvm::StringMap<DraftAndTime> Drafts;
|
150
|
60 };
|
|
61
|
|
62 } // namespace clangd
|
|
63 } // namespace clang
|
|
64
|
|
65 #endif
|