Fix issue when buffer contents do not get synced properly near start of file

This commit is contained in:
Jacob Dufault 2017-07-29 21:13:22 -07:00
parent 2a1ce80b4d
commit c83b9eab77
2 changed files with 24 additions and 5 deletions

View File

@ -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();

View File

@ -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;
}