Skip to content

Commit

Permalink
Calculate change length from range instead of rangeLength
Browse files Browse the repository at this point in the history
rangeLength is deprecated

Signed-off-by: Mathias Fussenegger <f.mathias@zignar.net>
  • Loading branch information
mfussenegger committed Nov 7, 2021
1 parent c1e9c06 commit 1cb892b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@
import org.eclipse.jdt.ls.core.internal.JDTUtils;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.JobHelpers;
import org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentContentChangeEvent;
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
Expand Down Expand Up @@ -390,17 +392,19 @@ public ICompilationUnit handleChanged(DidChangeTextDocumentParams params) {

Range range = changeEvent.getRange();
int length;

IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
final int startOffset;
if (range != null) {
length = changeEvent.getRangeLength().intValue();
Position start = range.getStart();
startOffset = JsonRpcHelpers.toOffset(document, start.getLine(), start.getCharacter());
length = DiagnosticsHelper.getLength(unit, range);
} else {
// range is optional and if not given, the whole file content is replaced
length = unit.getSource().length();
range = JDTUtils.toRange(unit, 0, length);
startOffset = 0;
}

int startOffset = JsonRpcHelpers.toOffset(unit.getBuffer(), range.getStart().getLine(), range.getStart().getCharacter());

TextEdit edit = null;
String text = changeEvent.getText();
if (length == 0) {
Expand All @@ -410,7 +414,6 @@ public ICompilationUnit handleChanged(DidChangeTextDocumentParams params) {
} else {
edit = new ReplaceEdit(startOffset, length, text);
}
IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
edit.apply(document, TextEdit.NONE);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public void testNotExpectedPackage() throws Exception {
String source = cu.getSource();
int length = source.length();
source = source.replace("org", "org.eclipse");
changeDocument(cu, source, 2, JDTUtils.toRange(cu, 0, source.length()), length);
changeDocument(cu, source, 2, JDTUtils.toRange(cu, 0, length));
FileUtils.writeStringToFile(file, source);
saveDocument(cu);
cu = JDTUtils.resolveCompilationUnit(uri);
Expand Down Expand Up @@ -725,7 +725,7 @@ public void testNotExpectedPackage2() throws Exception {
String source = cu.getSource();
int length = source.length();
source = source.replace("org", "org.eclipse");
changeDocument(cu, source, 2, JDTUtils.toRange(cu, 0, source.length()), length);
changeDocument(cu, source, 2, JDTUtils.toRange(cu, 0, length));
FileUtils.writeStringToFile(file, source);
saveDocument(cu);
cu = JDTUtils.resolveCompilationUnit(uri);
Expand All @@ -736,7 +736,7 @@ public void testNotExpectedPackage2() throws Exception {
source = cu.getSource();
length = source.length();
source = source.replace("org.eclipse", "org.eclipse.toto");
changeDocument(cu, source, 3, JDTUtils.toRange(cu, 0, source.length()), length);
changeDocument(cu, source, 3, JDTUtils.toRange(cu, 0, length));
FileUtils.writeStringToFile(file, source);
saveDocument(cu);
cu = JDTUtils.resolveCompilationUnit(uri);
Expand Down Expand Up @@ -852,14 +852,14 @@ private void openDocument(String uri, String content, int version) {

private void changeDocumentIncrementally(ICompilationUnit cu, String content, int version, int offset, int length) throws JavaModelException {
Range range = JDTUtils.toRange(cu, offset, length);
changeDocument(cu, content, version, range, length);
changeDocument(cu, content, version, range);
}

private void changeDocumentFull(ICompilationUnit cu, String content, int version) throws JavaModelException {
changeDocument(cu, content, version, null, 0);
changeDocument(cu, content, version, null);
}

private void changeDocument(ICompilationUnit cu, String content, int version, Range range, int length) throws JavaModelException {
private void changeDocument(ICompilationUnit cu, String content, int version, Range range) throws JavaModelException {
DidChangeTextDocumentParams changeParms = new DidChangeTextDocumentParams();
VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
Expand All @@ -868,7 +868,6 @@ private void changeDocument(ICompilationUnit cu, String content, int version, Ra
TextDocumentContentChangeEvent event = new TextDocumentContentChangeEvent();
if (range != null) {
event.setRange(range);
event.setRangeLength(length);
}
event.setText(content);
List<TextDocumentContentChangeEvent> contentChanges = new ArrayList<>();
Expand Down

0 comments on commit 1cb892b

Please sign in to comment.