blob: 1299efbfba9fa4ca05a491ec787842510b3a9915 [file] [log] [blame]
Ilya Biryukov38d79772017-05-16 09:38:59 +00001//===--- DraftStore.cpp - File contents container ---------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Biryukov38d79772017-05-16 09:38:59 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "DraftStore.h"
Simon Marchi98082622018-03-26 14:41:40 +000010#include "SourceCode.h"
Sam McCallad97ccf2020-04-28 17:49:17 +020011#include "support/Logger.h"
Simon Marchi98082622018-03-26 14:41:40 +000012#include "llvm/Support/Errc.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000013
Sam McCallc008af62018-10-20 15:30:37 +000014namespace clang {
15namespace clangd {
Ilya Biryukov38d79772017-05-16 09:38:59 +000016
Sam McCallcaf5a4d2020-03-03 15:57:39 +010017llvm::Optional<DraftStore::Draft> DraftStore::getDraft(PathRef File) const {
Ilya Biryukov38d79772017-05-16 09:38:59 +000018 std::lock_guard<std::mutex> Lock(Mutex);
19
20 auto It = Drafts.find(File);
21 if (It == Drafts.end())
Sam McCallc008af62018-10-20 15:30:37 +000022 return None;
Simon Marchi9569fd52018-03-16 14:30:42 +000023
Ilya Biryukov38d79772017-05-16 09:38:59 +000024 return It->second;
25}
26
Simon Marchi5178f922018-02-22 14:00:39 +000027std::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 Krameradcd0262020-01-28 20:23:46 +010032 ResultVector.push_back(std::string(DraftIt->getKey()));
Simon Marchi5178f922018-02-22 14:00:39 +000033
34 return ResultVector;
35}
36
Sam McCallcaf5a4d2020-03-03 15:57:39 +010037static 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 Biryukov38d79772017-05-16 09:38:59 +000048}
49
Sam McCallcaf5a4d2020-03-03 15:57:39 +010050int64_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
60llvm::Expected<DraftStore::Draft> DraftStore::updateDraft(
61 PathRef File, llvm::Optional<int64_t> Version,
62 llvm::ArrayRef<TextDocumentContentChangeEvent> Changes) {
Simon Marchi98082622018-03-26 14:41:40 +000063 std::lock_guard<std::mutex> Lock(Mutex);
64
65 auto EntryIt = Drafts.find(File);
66 if (EntryIt == Drafts.end()) {
Sam McCall30667c92020-07-08 21:49:38 +020067 return error(llvm::errc::invalid_argument,
68 "Trying to do incremental update on non-added document: {0}",
69 File);
Simon Marchi98082622018-03-26 14:41:40 +000070 }
Sam McCallcaf5a4d2020-03-03 15:57:39 +010071 Draft &D = EntryIt->second;
72 std::string Contents = EntryIt->second.Contents;
Simon Marchi98082622018-03-26 14:41:40 +000073
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 Biryukovf2001aa2019-01-07 15:45:19 +000081 llvm::Expected<size_t> StartIndex =
82 positionToOffset(Contents, Start, false);
Simon Marchi98082622018-03-26 14:41:40 +000083 if (!StartIndex)
84 return StartIndex.takeError();
85
86 const Position &End = Change.range->end;
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000087 llvm::Expected<size_t> EndIndex = positionToOffset(Contents, End, false);
Simon Marchi98082622018-03-26 14:41:40 +000088 if (!EndIndex)
89 return EndIndex.takeError();
90
91 if (*EndIndex < *StartIndex)
Sam McCall30667c92020-07-08 21:49:38 +020092 return error(llvm::errc::invalid_argument,
93 "Range's end position ({0}) is before start position ({1})",
94 End, Start);
Simon Marchi98082622018-03-26 14:41:40 +000095
Sam McCall71891122018-10-23 11:51:53 +000096 // Since the range length between two LSP positions is dependent on the
97 // contents of the buffer we compute the range length between the start and
98 // end position ourselves and compare it to the range length of the LSP
99 // message to verify the buffers of the client and server are in sync.
100
101 // EndIndex and StartIndex are in bytes, but Change.rangeLength is in UTF-16
102 // code units.
103 ssize_t ComputedRangeLength =
104 lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
105
106 if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
Sam McCall30667c92020-07-08 21:49:38 +0200107 return error(llvm::errc::invalid_argument,
108 "Change's rangeLength ({0}) doesn't match the "
109 "computed range length ({1}).",
110 *Change.rangeLength, ComputedRangeLength);
Simon Marchi98082622018-03-26 14:41:40 +0000111
112 std::string NewContents;
113 NewContents.reserve(*StartIndex + Change.text.length() +
114 (Contents.length() - *EndIndex));
115
116 NewContents = Contents.substr(0, *StartIndex);
117 NewContents += Change.text;
118 NewContents += Contents.substr(*EndIndex);
119
120 Contents = std::move(NewContents);
121 }
122
Sam McCallcaf5a4d2020-03-03 15:57:39 +0100123 updateVersion(D, Version);
124 D.Contents = std::move(Contents);
125 return D;
Simon Marchi98082622018-03-26 14:41:40 +0000126}
127
Simon Marchi9569fd52018-03-16 14:30:42 +0000128void DraftStore::removeDraft(PathRef File) {
Ilya Biryukov38d79772017-05-16 09:38:59 +0000129 std::lock_guard<std::mutex> Lock(Mutex);
130
Simon Marchi9569fd52018-03-16 14:30:42 +0000131 Drafts.erase(File);
Ilya Biryukov38d79772017-05-16 09:38:59 +0000132}
Sam McCallc008af62018-10-20 15:30:37 +0000133
134} // namespace clangd
135} // namespace clang