Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 1 | //===--- DraftStore.cpp - 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 | #include "DraftStore.h" |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame^] | 10 | #include "Logger.h" |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 11 | #include "SourceCode.h" |
| 12 | #include "llvm/Support/Errc.h" |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 13 | |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 14 | namespace clang { |
| 15 | namespace clangd { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 16 | |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame^] | 17 | llvm::Optional<DraftStore::Draft> DraftStore::getDraft(PathRef File) const { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 18 | std::lock_guard<std::mutex> Lock(Mutex); |
| 19 | |
| 20 | auto It = Drafts.find(File); |
| 21 | if (It == Drafts.end()) |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 22 | return None; |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 23 | |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 24 | return It->second; |
| 25 | } |
| 26 | |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 27 | std::vector<Path> DraftStore::getActiveFiles() const { |
| 28 | std::lock_guard<std::mutex> Lock(Mutex); |
| 29 | std::vector<Path> ResultVector; |
| 30 | |
| 31 | for (auto DraftIt = Drafts.begin(); DraftIt != Drafts.end(); DraftIt++) |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 32 | ResultVector.push_back(std::string(DraftIt->getKey())); |
Simon Marchi | 5178f92 | 2018-02-22 14:00:39 +0000 | [diff] [blame] | 33 | |
| 34 | return ResultVector; |
| 35 | } |
| 36 | |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame^] | 37 | static void updateVersion(DraftStore::Draft &D, |
| 38 | llvm::Optional<int64_t> Version) { |
| 39 | if (Version) { |
| 40 | // We treat versions as opaque, but the protocol says they increase. |
| 41 | if (*Version <= D.Version) |
| 42 | log("File version went from {0} to {1}", D.Version, Version); |
| 43 | D.Version = *Version; |
| 44 | } else { |
| 45 | // Note that if D was newly-created, this will bump D.Version from -1 to 0. |
| 46 | ++D.Version; |
| 47 | } |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame^] | 50 | int64_t DraftStore::addDraft(PathRef File, llvm::Optional<int64_t> Version, |
| 51 | llvm::StringRef Contents) { |
| 52 | std::lock_guard<std::mutex> Lock(Mutex); |
| 53 | |
| 54 | Draft &D = Drafts[File]; |
| 55 | updateVersion(D, Version); |
| 56 | D.Contents = Contents.str(); |
| 57 | return D.Version; |
| 58 | } |
| 59 | |
| 60 | llvm::Expected<DraftStore::Draft> DraftStore::updateDraft( |
| 61 | PathRef File, llvm::Optional<int64_t> Version, |
| 62 | llvm::ArrayRef<TextDocumentContentChangeEvent> Changes) { |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 63 | std::lock_guard<std::mutex> Lock(Mutex); |
| 64 | |
| 65 | auto EntryIt = Drafts.find(File); |
| 66 | if (EntryIt == Drafts.end()) { |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 67 | return llvm::make_error<llvm::StringError>( |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 68 | "Trying to do incremental update on non-added document: " + File, |
| 69 | llvm::errc::invalid_argument); |
| 70 | } |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame^] | 71 | Draft &D = EntryIt->second; |
| 72 | std::string Contents = EntryIt->second.Contents; |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 73 | |
| 74 | for (const TextDocumentContentChangeEvent &Change : Changes) { |
| 75 | if (!Change.range) { |
| 76 | Contents = Change.text; |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | const Position &Start = Change.range->start; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 81 | llvm::Expected<size_t> StartIndex = |
| 82 | positionToOffset(Contents, Start, false); |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 83 | if (!StartIndex) |
| 84 | return StartIndex.takeError(); |
| 85 | |
| 86 | const Position &End = Change.range->end; |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 87 | llvm::Expected<size_t> EndIndex = positionToOffset(Contents, End, false); |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 88 | if (!EndIndex) |
| 89 | return EndIndex.takeError(); |
| 90 | |
| 91 | if (*EndIndex < *StartIndex) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 92 | return llvm::make_error<llvm::StringError>( |
| 93 | llvm::formatv( |
| 94 | "Range's end position ({0}) is before start position ({1})", End, |
| 95 | Start), |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 96 | llvm::errc::invalid_argument); |
| 97 | |
Sam McCall | 7189112 | 2018-10-23 11:51:53 +0000 | [diff] [blame] | 98 | // Since the range length between two LSP positions is dependent on the |
| 99 | // contents of the buffer we compute the range length between the start and |
| 100 | // end position ourselves and compare it to the range length of the LSP |
| 101 | // message to verify the buffers of the client and server are in sync. |
| 102 | |
| 103 | // EndIndex and StartIndex are in bytes, but Change.rangeLength is in UTF-16 |
| 104 | // code units. |
| 105 | ssize_t ComputedRangeLength = |
| 106 | lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex)); |
| 107 | |
| 108 | if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength) |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 109 | return llvm::make_error<llvm::StringError>( |
| 110 | llvm::formatv("Change's rangeLength ({0}) doesn't match the " |
| 111 | "computed range length ({1}).", |
Sam McCall | 6b09e9c | 2019-08-05 08:14:13 +0000 | [diff] [blame] | 112 | *Change.rangeLength, ComputedRangeLength), |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 113 | llvm::errc::invalid_argument); |
| 114 | |
| 115 | std::string NewContents; |
| 116 | NewContents.reserve(*StartIndex + Change.text.length() + |
| 117 | (Contents.length() - *EndIndex)); |
| 118 | |
| 119 | NewContents = Contents.substr(0, *StartIndex); |
| 120 | NewContents += Change.text; |
| 121 | NewContents += Contents.substr(*EndIndex); |
| 122 | |
| 123 | Contents = std::move(NewContents); |
| 124 | } |
| 125 | |
Sam McCall | caf5a4d | 2020-03-03 15:57:39 +0100 | [diff] [blame^] | 126 | updateVersion(D, Version); |
| 127 | D.Contents = std::move(Contents); |
| 128 | return D; |
Simon Marchi | 9808262 | 2018-03-26 14:41:40 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 131 | void DraftStore::removeDraft(PathRef File) { |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 132 | std::lock_guard<std::mutex> Lock(Mutex); |
| 133 | |
Simon Marchi | 9569fd5 | 2018-03-16 14:30:42 +0000 | [diff] [blame] | 134 | Drafts.erase(File); |
Ilya Biryukov | 38d7977 | 2017-05-16 09:38:59 +0000 | [diff] [blame] | 135 | } |
Sam McCall | c008af6 | 2018-10-20 15:30:37 +0000 | [diff] [blame] | 136 | |
| 137 | } // namespace clangd |
| 138 | } // namespace clang |