Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use deprecated rangeLength property in handleChanged #1928

Merged
merged 3 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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) {
Expand All @@ -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);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,30 @@
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;
import org.eclipse.core.runtime.CoreException;
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;
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
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
Expand Down Expand Up @@ -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<TextDocumentContentChangeEvent> 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;
}

}
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