diff --git a/src/language_server_api.h b/src/language_server_api.h index 28e5877a..18863c71 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -260,7 +260,7 @@ MAKE_REFLECT_STRUCT(lsTextDocumentIdentifier, uri); struct lsVersionedTextDocumentIdentifier { lsDocumentUri uri; // The version number of this document. - int version = 0; + optional version; lsTextDocumentIdentifier AsTextDocumentIdentifier() const; }; @@ -990,9 +990,9 @@ MAKE_REFLECT_STRUCT(lsMarkedString, language, value); struct lsTextDocumentContentChangeEvent { // The range of the document that changed. - lsRange range; + optional range; // The length of the range that got replaced. - int rangeLength = -1; + optional rangeLength; // The new text of the range/document. std::string text; }; diff --git a/src/working_files.cc b/src/working_files.cc index eef15d67..37b68e6c 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -324,28 +324,25 @@ void WorkingFiles::OnChange(const lsTextDocumentDidChangeParams& change) { return; } - file->version = change.textDocument.version; + if (change.textDocument.version) + file->version = *change.textDocument.version; for (const lsTextDocumentContentChangeEvent& diff : change.contentChanges) { // Per the spec replace everything if the rangeLength and range are not set. // See https://github.com/Microsoft/language-server-protocol/issues/9. - if (diff.rangeLength == -1 && - diff.range.start == lsPosition::kZeroPosition && - diff.range.end == lsPosition::kZeroPosition) { + if (!diff.range) { file->buffer_content = diff.text; file->OnBufferContentUpdated(); } else { 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; - } + diff.rangeLength + ? start_offset + *diff.rangeLength + : GetOffsetForPosition(diff.range->end, file->buffer_content); file->buffer_content.replace( file->buffer_content.begin() + start_offset, - file->buffer_content.begin() + start_offset + length, diff.text); + file->buffer_content.begin() + end_offset, diff.text); file->OnBufferContentUpdated(); } } diff --git a/src/working_files.h b/src/working_files.h index 4c82cdab..7089c0be 100644 --- a/src/working_files.h +++ b/src/working_files.h @@ -103,4 +103,4 @@ struct WorkingFiles { // invalidated if we resize files. std::vector> files; std::mutex files_mutex; // Protects |files|. -}; \ No newline at end of file +};