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