mirror of
https://github.com/MaskRay/ccls.git
synced 2025-01-19 12:05:50 +00:00
Fix issue when buffer contents do not get synced properly near start of file
This commit is contained in:
parent
2a1ce80b4d
commit
c83b9eab77
@ -17,7 +17,7 @@ int GetOffsetForPosition(lsPosition position, const std::string& content) {
|
||||
++offset;
|
||||
}
|
||||
|
||||
return std::min<int>(offset + position.character, content.size() - 1);
|
||||
return std::min<int>(offset + position.character, content.size());
|
||||
}
|
||||
|
||||
lsPosition CharPos(const std::string& search, char character, int character_offset) {
|
||||
@ -211,10 +211,23 @@ bool SubstringMatch(const std::string& search, const std::string& content) {
|
||||
|
||||
TEST_SUITE("Offset");
|
||||
|
||||
TEST_CASE("over range") {
|
||||
TEST_CASE("past end") {
|
||||
std::string content = "foo";
|
||||
int offset = GetOffsetForPosition(lsPosition(10, 10), content);
|
||||
REQUIRE(offset < content.size());
|
||||
REQUIRE(offset <= content.size());
|
||||
}
|
||||
|
||||
TEST_CASE("in middle of content") {
|
||||
std::string content = "abcdefghijk";
|
||||
for (int i = 0; i < content.size(); ++i) {
|
||||
int offset = GetOffsetForPosition(lsPosition(0, i), content);
|
||||
REQUIRE(i == offset);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("at end of content") {
|
||||
REQUIRE(GetOffsetForPosition(lsPosition(0, 0), "") == 0);
|
||||
REQUIRE(GetOffsetForPosition(lsPosition(0, 1), "a") == 1);
|
||||
}
|
||||
|
||||
TEST_SUITE_END();
|
||||
|
@ -308,24 +308,30 @@ void WorkingFiles::OnChange(const Ipc_TextDocumentDidChange::Params& change) {
|
||||
}
|
||||
|
||||
file->version = change.textDocument.version;
|
||||
// std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
|
||||
//std::cerr << "VERSION " << change.textDocument.version << std::endl;
|
||||
|
||||
for (const Ipc_TextDocumentDidChange::lsTextDocumentContentChangeEvent& diff : change.contentChanges) {
|
||||
//std::cerr << "Applying rangeLength=" << diff.rangeLength;
|
||||
|
||||
// std::cerr << "|" << file->buffer_content << "|" << std::endl;
|
||||
// If range or rangeLength are emitted we replace everything, per the spec.
|
||||
if (diff.rangeLength == -1) {
|
||||
file->buffer_content = diff.text;
|
||||
file->OnBufferContentUpdated();
|
||||
// std::cerr << "-> Replacing entire content";
|
||||
}
|
||||
else {
|
||||
int start_offset = GetOffsetForPosition(diff.range.start, file->buffer_content);
|
||||
// std::cerr << "-> Applying diff start=" << diff.range.start.ToString() << ", end=" << diff.range.end.ToString() << ", start_offset=" << start_offset << std::endl;
|
||||
file->buffer_content.replace(file->buffer_content.begin() + start_offset,
|
||||
file->buffer_content.begin() + start_offset + diff.rangeLength,
|
||||
diff.text);
|
||||
file->OnBufferContentUpdated();
|
||||
}
|
||||
|
||||
// std::cerr << "|" << file->buffer_content << "|" << std::endl;
|
||||
}
|
||||
// std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
|
||||
|
||||
//std::cerr << std::endl << std::endl << "--------" << file->content << "--------" << std::endl << std::endl;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user