Skip to content

Commit

Permalink
Supports TextDocumentContentChangeEvent#getRangeLength with null value (
Browse files Browse the repository at this point in the history
#1696)

Supports TextDocumentContentChangeEvent#getRangeLength with null value
  • Loading branch information
jmatss authored Nov 4, 2024
1 parent 744de35 commit 830e00d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ public void update(List<TextDocumentContentChangeEvent> changes) {
int length = 0;

if (range != null) {
length = changeEvent.getRangeLength().intValue();
Integer rangeLength = changeEvent.getRangeLength();
length = rangeLength != null ? rangeLength.intValue() : offsetAt(range.getEnd()) - offsetAt(range.getStart());
} else {
// range is optional and if not given, the whole file content is replaced
length = buffer.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,55 @@ public void testChangeReplaceRange() {
assertEquals(expectedText, document.getText());
}

@Test
public void testRangeLengthPreferredOverRangeEndPosition() {
String text = "<zzz>\r\n" + // /// <-- inserting 'a' in tag name
" <b>\r\n" + //
" </b>\r\n" + //
"</aaa>\r\n";

String expectedText = "<aaa>\r\n" + // /// <-- inserted 'a' in tag name
" <b>\r\n" + //
" </b>\r\n" + //
"</aaa>\r\n";

TextDocument document = new TextDocument(text, "uri");
document.setIncremental(true);

Range range1 = new Range(new Position(0, 1), new Position(-1, -1));
TextDocumentContentChangeEvent change1 = new TextDocumentContentChangeEvent(range1, 3, "aaa");

ArrayList<TextDocumentContentChangeEvent> changes = new ArrayList<>();
changes.add(change1);

document.update(changes);

assertEquals(expectedText, document.getText());
}

// https://github.com/eclipse-lemminx/lemminx/issues/1674
@Test
public void testDeprecatedRangeLengthAllowsNull() {
String text = "<aaa>\r\n" + //
" <b/>\r\n" + //
"</aaa>\r\n";

String expectedText = "<aaa/>\r\n";

TextDocument document = new TextDocument(text, "uri");
document.setIncremental(true);

Range range1 = new Range(new Position(0, 4), new Position(2, 5));
TextDocumentContentChangeEvent change1 = new TextDocumentContentChangeEvent(range1, null, "/");

ArrayList<TextDocumentContentChangeEvent> changes = new ArrayList<>();
changes.add(change1);

document.update(changes);

assertEquals(expectedText, document.getText());
}

@Test
public void testBasicChangeMultipleChanges() {
String text = "<>\r\n" + // // <-- inserting 'a' in tag name
Expand Down

0 comments on commit 830e00d

Please sign in to comment.