Make lsTextDocumentContentChangeEvent::{range,rangeLength} optional. fix #185 (#189)

Also make lsVersionedTextDocumentIdentifier::version optional per specification
This commit is contained in:
Fangrui Song 2017-12-25 00:58:26 -08:00 committed by GitHub
parent bfccac525c
commit 20c156f71d
3 changed files with 12 additions and 15 deletions

View File

@ -260,7 +260,7 @@ MAKE_REFLECT_STRUCT(lsTextDocumentIdentifier, uri);
struct lsVersionedTextDocumentIdentifier { struct lsVersionedTextDocumentIdentifier {
lsDocumentUri uri; lsDocumentUri uri;
// The version number of this document. // The version number of this document.
int version = 0; optional<int> version;
lsTextDocumentIdentifier AsTextDocumentIdentifier() const; lsTextDocumentIdentifier AsTextDocumentIdentifier() const;
}; };
@ -990,9 +990,9 @@ MAKE_REFLECT_STRUCT(lsMarkedString, language, value);
struct lsTextDocumentContentChangeEvent { struct lsTextDocumentContentChangeEvent {
// The range of the document that changed. // The range of the document that changed.
lsRange range; optional<lsRange> range;
// The length of the range that got replaced. // The length of the range that got replaced.
int rangeLength = -1; optional<int> rangeLength;
// The new text of the range/document. // The new text of the range/document.
std::string text; std::string text;
}; };

View File

@ -324,28 +324,25 @@ void WorkingFiles::OnChange(const lsTextDocumentDidChangeParams& change) {
return; return;
} }
file->version = change.textDocument.version; if (change.textDocument.version)
file->version = *change.textDocument.version;
for (const lsTextDocumentContentChangeEvent& diff : change.contentChanges) { for (const lsTextDocumentContentChangeEvent& diff : change.contentChanges) {
// Per the spec replace everything if the rangeLength and range are not set. // Per the spec replace everything if the rangeLength and range are not set.
// See https://github.com/Microsoft/language-server-protocol/issues/9. // See https://github.com/Microsoft/language-server-protocol/issues/9.
if (diff.rangeLength == -1 && if (!diff.range) {
diff.range.start == lsPosition::kZeroPosition &&
diff.range.end == lsPosition::kZeroPosition) {
file->buffer_content = diff.text; file->buffer_content = diff.text;
file->OnBufferContentUpdated(); file->OnBufferContentUpdated();
} 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 = int end_offset =
GetOffsetForPosition(diff.range.end, file->buffer_content); diff.rangeLength
int length = diff.rangeLength; ? start_offset + *diff.rangeLength
if (length == -1) { : GetOffsetForPosition(diff.range->end, file->buffer_content);
length = end_offset - start_offset;
}
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 + length, diff.text); file->buffer_content.begin() + end_offset, diff.text);
file->OnBufferContentUpdated(); file->OnBufferContentUpdated();
} }
} }