Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- DraftStore.h - File contents container -----------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H |
| 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DRAFTSTORE_H |
| 11 | |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 12 | #include "Protocol.h" |
Sam McCall | ad97ccf | 2020-04-28 17:49:17 +0200 | [diff] [blame] | 13 | #include "support/Path.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 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 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 23 | /// A thread-safe container for files opened in a workspace, addressed by |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 24 | /// filenames. The contents are owned by the DraftStore. This class supports |
| 25 | /// both whole and incremental updates of the documents. |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 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. |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 28 | class DraftStore { |
| 29 | public: |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 30 | struct Draft { |
| 31 | std::string Contents; |
| 32 | int64_t Version = -1; |
| 33 | }; |
| 34 | |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 35 | /// \return Contents of the stored document. |
| 36 | /// For untracked files, a llvm::None is returned. |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 37 | llvm::Optional<Draft> getDraft(PathRef File) const; |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 38 | |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 39 | /// \return List of names of the drafts in this store. |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 40 | std::vector<Path> getActiveFiles() const; |
| 41 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 42 | /// Replace contents of the draft for \p File with \p Contents. |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 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); |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 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. |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 51 | /// If no version is specified, one will be automatically assigned. |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 52 | /// |
| 53 | /// \return The new version of the draft for \p File, or an error if the |
| 54 | /// changes couldn't be applied. |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 55 | llvm::Expected<Draft> |
| 56 | updateDraft(PathRef File, llvm::Optional<int64_t> Version, |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 57 | llvm::ArrayRef<TextDocumentContentChangeEvent> Changes); |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 58 | |
| 59 | /// Remove the draft from the store. |
| 60 | void removeDraft(PathRef File); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | mutable std::mutex Mutex; |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame] | 64 | llvm::StringMap<Draft> Drafts; |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | } // namespace clangd |
| 68 | } // namespace clang |
| 69 | |
| 70 | #endif |