From c83b9eab7717b254d396d919cc5468141dac569e Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sat, 29 Jul 2017 21:13:22 -0700 Subject: [PATCH] Fix issue when buffer contents do not get synced properly near start of file --- src/lex_utils.cc | 19 ++++++++++++++++--- src/working_files.cc | 10 ++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/lex_utils.cc b/src/lex_utils.cc index 112743d1..a2ed2e69 100644 --- a/src/lex_utils.cc +++ b/src/lex_utils.cc @@ -17,7 +17,7 @@ int GetOffsetForPosition(lsPosition position, const std::string& content) { ++offset; } - return std::min(offset + position.character, content.size() - 1); + return std::min(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(); diff --git a/src/working_files.cc b/src/working_files.cc index 8fea96a4..736c747a 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -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; }