Only replace text if both rangeLength and range are not set

This commit is contained in:
Peter Elmers 2017-11-20 10:57:01 -08:00 committed by Jacob Dufault
parent 31d7423490
commit 3341b1f13d

View File

@ -327,20 +327,27 @@ void WorkingFiles::OnChange(const Ipc_TextDocumentDidChange::Params& change) {
for (const Ipc_TextDocumentDidChange::lsTextDocumentContentChangeEvent& diff : for (const Ipc_TextDocumentDidChange::lsTextDocumentContentChangeEvent& diff :
change.contentChanges) { change.contentChanges) {
// std::cerr << "|" << file->buffer_content << "|" << std::endl; // std::cerr << "|" << file->buffer_content << "|" << std::endl;
// If range or rangeLength are emitted we replace everything, per the spec. // Per the spec replace everything if the rangeLength and range are not set.
if (diff.rangeLength == -1) { // 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->buffer_content = diff.text;
file->OnBufferContentUpdated(); file->OnBufferContentUpdated();
// std::cerr << "-> Replacing entire content"; // std::cerr << "-> Replacing entire content";
} else { } else {
int start_offset = int start_offset = GetOffsetForPosition(diff.range.start, file->buffer_content);
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() // std::cerr << "-> Applying diff start=" << diff.range.start.ToString()
// << ", end=" << diff.range.end.ToString() << ", start_offset=" << // << ", end=" << diff.range.end.ToString() << ", start_offset=" <<
// start_offset << std::endl; // start_offset << std::endl;
file->buffer_content.replace( file->buffer_content.replace(
file->buffer_content.begin() + start_offset, file->buffer_content.begin() + start_offset,
file->buffer_content.begin() + start_offset + diff.rangeLength, file->buffer_content.begin() + start_offset + length,
diff.text); diff.text);
file->OnBufferContentUpdated(); file->OnBufferContentUpdated();
} }