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..d6b05042b5 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,18 @@ 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 +413,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.core/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandler.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandler.java index fa9a48c0f9..b377726367 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandler.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/DocumentLifeCycleHandler.java @@ -13,7 +13,6 @@ package org.eclipse.jdt.ls.core.internal.handlers; import java.util.Collection; -import java.util.List; import java.util.Optional; import org.eclipse.core.resources.IFile; @@ -21,8 +20,6 @@ import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.ICompilationUnit; -import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.core.manipulation.CoreASTProvider; import org.eclipse.jdt.ls.core.internal.JDTUtils; import org.eclipse.jdt.ls.core.internal.JavaClientConnection; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; @@ -30,31 +27,16 @@ import org.eclipse.jdt.ls.core.internal.managers.InvisibleProjectImporter; import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager; import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager; -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.Range; -import org.eclipse.lsp4j.TextDocumentContentChangeEvent; -import org.eclipse.text.edits.DeleteEdit; -import org.eclipse.text.edits.InsertEdit; -import org.eclipse.text.edits.MalformedTreeException; -import org.eclipse.text.edits.ReplaceEdit; -import org.eclipse.text.edits.TextEdit; public class DocumentLifeCycleHandler extends BaseDocumentLifeCycleHandler { private JavaClientConnection connection; private PreferenceManager preferenceManager; - private CoreASTProvider sharedASTProvider; - public DocumentLifeCycleHandler(JavaClientConnection connection, PreferenceManager preferenceManager, ProjectsManager projectsManager, boolean delayValidation) { super(delayValidation); this.connection = connection; this.preferenceManager = preferenceManager; - this.sharedASTProvider = CoreASTProvider.getInstance(); } @Override @@ -98,73 +80,4 @@ public ICompilationUnit resolveCompilationUnit(String uri) { return unit; } - - @Override - public ICompilationUnit handleOpen(DidOpenTextDocumentParams params) { - ICompilationUnit unit = super.handleOpen(params); - - if (unit == null || unit.getResource() == null || unit.getResource().isDerived()) { - return unit; - } - - return unit; - } - - @Override - public ICompilationUnit handleChanged(DidChangeTextDocumentParams params) { - String uri = params.getTextDocument().getUri(); - ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); - - if (unit == null || !unit.isWorkingCopy() || params.getContentChanges().isEmpty() || unit.getResource().isDerived()) { - return unit; - } - - try { - if (unit.equals(sharedASTProvider.getActiveJavaElement())) { - sharedASTProvider.disposeAST(); - } - List contentChanges = params.getContentChanges(); - for (TextDocumentContentChangeEvent changeEvent : contentChanges) { - - Range range = changeEvent.getRange(); - int length; - - if (range != null) { - length = changeEvent.getRangeLength().intValue(); - } else { - // range is optional and if not given, the whole file content is replaced - length = unit.getSource().length(); - range = JDTUtils.toRange(unit, 0, length); - } - - int startOffset = JsonRpcHelpers.toOffset(unit.getBuffer(), range.getStart().getLine(), range.getStart().getCharacter()); - - TextEdit edit = null; - String text = changeEvent.getText(); - if (length == 0) { - edit = new InsertEdit(startOffset, text); - } else if (text.isEmpty()) { - edit = new DeleteEdit(startOffset, length); - } else { - edit = new ReplaceEdit(startOffset, length, text); - } - - IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer()); - edit.apply(document, TextEdit.NONE); - - } - triggerValidation(unit); - } catch (JavaModelException | MalformedTreeException | BadLocationException e) { - JavaLanguageServerPlugin.logException("Error while handling document change. URI: " + uri, e); - } - - return unit; - } - - @Override - public ICompilationUnit handleClosed(DidCloseTextDocumentParams params) { - ICompilationUnit unit = super.handleClosed(params); - return unit; - } - } 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<>();