Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- DraftStore.h - File contents container -----------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H |
| 12 | |
| 13 | #include "Path.h" |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 14 | #include "Protocol.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 15 | #include "clang/Basic/LLVM.h" |
| 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include <mutex> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | namespace clang { |
| 22 | namespace clangd { |
| 23 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 24 | /// A thread-safe container for files opened in a workspace, addressed by |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 25 | /// filenames. The contents are owned by the DraftStore. This class supports |
| 26 | /// both whole and incremental updates of the documents. |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 27 | class DraftStore { |
| 28 | public: |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 29 | /// \return Contents of the stored document. |
| 30 | /// For untracked files, a llvm::None is returned. |
| 31 | llvm::Optional<std::string> getDraft(PathRef File) const; |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 32 | |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 33 | /// \return List of names of the drafts in this store. |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 34 | std::vector<Path> getActiveFiles() const; |
| 35 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 36 | /// Replace contents of the draft for \p File with \p Contents. |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 37 | void addDraft(PathRef File, StringRef Contents); |
| 38 | |
| 39 | /// Update the contents of the draft for \p File based on \p Changes. |
| 40 | /// If a position in \p Changes is invalid (e.g. out-of-range), the |
| 41 | /// draft is not modified. |
| 42 | /// |
| 43 | /// \return The new version of the draft for \p File, or an error if the |
| 44 | /// changes couldn't be applied. |
| 45 | llvm::Expected<std::string> |
| 46 | updateDraft(PathRef File, |
| 47 | llvm::ArrayRef<TextDocumentContentChangeEvent> Changes); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 48 | |
| 49 | /// Remove the draft from the store. |
| 50 | void removeDraft(PathRef File); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | mutable std::mutex Mutex; |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 54 | llvm::StringMap<std::string> Drafts; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | } // namespace clangd |
| 58 | } // namespace clang |
| 59 | |
| 60 | #endif |