From 1cb892b57f159b9f01f9cb343b7cc67a7da475f4 Mon Sep 17 00:00:00 2001 From: Mathias Fussenegger Date: Mon, 1 Nov 2021 15:41:44 +0100 Subject: [PATCH] Calculate change length from range instead of rangeLength rangeLength is deprecated Signed-off-by: Mathias Fussenegger --- .../handlers/BaseDocumentLifeCycleHandler.java | 13 ++++++++----- .../handlers/DocumentLifeCycleHandlerTest.java | 13 ++++++------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BaseDocumentLifeCycleHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BaseDocumentLifeCycleHandler.java index 4c47cc77ef..ec1f40f311 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BaseDocumentLifeCycleHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/BaseDocumentLifeCycleHandler.java @@ -54,6 +54,7 @@ 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; @@ -61,6 +62,7 @@ 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; @@ -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) { @@ -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); } diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandlerTest.java index 60c6c3d9bd..c3124a5baf 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandlerTest.java @@ -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); @@ -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); @@ -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); @@ -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)); @@ -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 contentChanges = new ArrayList<>();