12 #include "llvm/Support/Errc.h" 18 std::lock_guard<std::mutex> Lock(Mutex);
20 auto It = Drafts.find(File);
21 if (It == Drafts.end())
28 std::lock_guard<std::mutex> Lock(Mutex);
29 std::vector<Path> ResultVector;
31 for (
auto DraftIt = Drafts.begin(); DraftIt != Drafts.end(); DraftIt++)
32 ResultVector.push_back(DraftIt->getKey());
38 std::lock_guard<std::mutex> Lock(Mutex);
44 PathRef File, llvm::ArrayRef<TextDocumentContentChangeEvent> Changes) {
45 std::lock_guard<std::mutex> Lock(Mutex);
47 auto EntryIt = Drafts.find(File);
48 if (EntryIt == Drafts.end()) {
49 return llvm::make_error<llvm::StringError>(
50 "Trying to do incremental update on non-added document: " +
File,
51 llvm::errc::invalid_argument);
54 std::string
Contents = EntryIt->second;
58 Contents = Change.text;
62 const Position &Start = Change.range->start;
63 llvm::Expected<size_t> StartIndex =
66 return StartIndex.takeError();
68 const Position &End = Change.range->end;
71 return EndIndex.takeError();
73 if (*EndIndex < *StartIndex)
74 return llvm::make_error<llvm::StringError>(
76 "Range's end position ({0}) is before start position ({1})", End,
78 llvm::errc::invalid_argument);
87 ssize_t ComputedRangeLength =
88 lspLength(Contents.substr(*StartIndex, *EndIndex - *StartIndex));
90 if (Change.rangeLength && ComputedRangeLength != *Change.rangeLength)
91 return llvm::make_error<llvm::StringError>(
92 llvm::formatv(
"Change's rangeLength ({0}) doesn't match the " 93 "computed range length ({1}).",
94 *Change.rangeLength, *EndIndex - *StartIndex),
95 llvm::errc::invalid_argument);
97 std::string NewContents;
98 NewContents.reserve(*StartIndex + Change.text.length() +
99 (Contents.length() - *EndIndex));
101 NewContents = Contents.substr(0, *StartIndex);
102 NewContents += Change.text;
103 NewContents += Contents.substr(*EndIndex);
105 Contents = std::move(NewContents);
113 std::lock_guard<std::mutex> Lock(Mutex);
size_t lspLength(llvm::StringRef Code)
llvm::StringRef PathRef
A typedef to represent a ref to file path.
void addDraft(PathRef File, StringRef Contents)
Replace contents of the draft for File with Contents.
Documents should not be synced at all.
llvm::Expected< std::string > updateDraft(PathRef File, llvm::ArrayRef< TextDocumentContentChangeEvent > Changes)
Update the contents of the draft for File based on Changes.
llvm::Expected< size_t > positionToOffset(llvm::StringRef Code, Position P, bool AllowColumnsBeyondLineLength)
Turn a [line, column] pair into an offset in Code.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< Path > getActiveFiles() const
void removeDraft(PathRef File)
Remove the draft from the store.
llvm::Optional< std::string > getDraft(PathRef File) const