From 3341b1f13de435eeb71bc965f145e3487f53c224 Mon Sep 17 00:00:00 2001 From: Peter Elmers Date: Mon, 20 Nov 2017 10:57:01 -0800 Subject: [PATCH] Only replace text if both rangeLength and range are not set --- src/working_files.cc | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/working_files.cc b/src/working_files.cc index 9c93be6e..8129ce6d 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -327,20 +327,27 @@ void WorkingFiles::OnChange(const Ipc_TextDocumentDidChange::Params& change) { for (const Ipc_TextDocumentDidChange::lsTextDocumentContentChangeEvent& diff : change.contentChanges) { // std::cerr << "|" << file->buffer_content << "|" << std::endl; - // If range or rangeLength are emitted we replace everything, per the spec. - if (diff.rangeLength == -1) { + // Per the spec replace everything if the rangeLength and range are not set. + // See https://github.com/Microsoft/language-server-protocol/issues/9. + auto zeroPosition = lsPosition(0, 0); + if (diff.rangeLength == -1 && diff.range.start == zeroPosition + && diff.range.end == zeroPosition) { file->buffer_content = diff.text; file->OnBufferContentUpdated(); // std::cerr << "-> Replacing entire content"; } else { - int start_offset = - GetOffsetForPosition(diff.range.start, file->buffer_content); + int start_offset = GetOffsetForPosition(diff.range.start, file->buffer_content); + int end_offset = GetOffsetForPosition(diff.range.end, file->buffer_content); + int length = diff.rangeLength; + if (length == -1) { + length = end_offset - start_offset; + } // 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, + file->buffer_content.begin() + start_offset + length, diff.text); file->OnBufferContentUpdated(); }