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

Support refactoring when renaming or moving files #1445

Merged
merged 2 commits into from
Mar 24, 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 @@ -13,14 +13,14 @@
package org.eclipse.jdt.ls.core.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
Expand Down Expand Up @@ -269,6 +269,11 @@ private static void convertTextEdit(WorkspaceEdit root, ICompilationUnit unit, T
}

TextEditConverter converter = new TextEditConverter(unit, edit);
List<org.eclipse.lsp4j.TextEdit> textEdits = filterTextEdits(converter.convert());
if (textEdits == null || textEdits.isEmpty()) {
return;
}

String uri = JDTUtils.toURI(unit);
if (JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isResourceOperationSupported()) {
List<Either<TextDocumentEdit, ResourceOperation>> changes = root.getDocumentChanges();
Expand All @@ -278,18 +283,38 @@ private static void convertTextEdit(WorkspaceEdit root, ICompilationUnit unit, T
}

VersionedTextDocumentIdentifier identifier = new VersionedTextDocumentIdentifier(uri, 0);
TextDocumentEdit documentEdit = new TextDocumentEdit(identifier, converter.convert());
TextDocumentEdit documentEdit = new TextDocumentEdit(identifier, textEdits);
changes.add(Either.forLeft(documentEdit));
} else {
Map<String, List<org.eclipse.lsp4j.TextEdit>> changes = root.getChanges();
if (changes.containsKey(uri)) {
changes.get(uri).addAll(converter.convert());
changes.get(uri).addAll(textEdits);
} else {
changes.put(uri, converter.convert());
changes.put(uri, textEdits);
}
}
}

private static List<org.eclipse.lsp4j.TextEdit> filterTextEdits(List<org.eclipse.lsp4j.TextEdit> textEdits) {
if (textEdits == null || textEdits.isEmpty()) {
return textEdits;
}

return textEdits.stream().filter(textEdit -> !isEmpty(textEdit)).collect(Collectors.toList());
}

private static boolean isEmpty(org.eclipse.lsp4j.TextEdit textEdit) {
if (textEdit == null || textEdit.getRange() == null) {
return true;
}

Position start = textEdit.getRange().getStart();
Position end = textEdit.getRange().getEnd();
return StringUtils.isEmpty(textEdit.getNewText())
&& start.getCharacter() == end.getCharacter()
&& start.getLine() == end.getLine();
}

private static ICompilationUnit getNewCompilationUnit(IType type, String newName) {
ICompilationUnit cu = type.getCompilationUnit();
if (isPrimaryType(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -300,4 +301,33 @@ public static String dos2Unix(String str) {
return str;
}

/**
* Returns the longest common path for an array of paths.
*/
public static IPath getLongestCommonPath(IPath[] paths) {
if (paths == null || paths.length == 0) {
return null;
}

int common = paths[0].segmentCount() - 1;
for (int i = 1; i < paths.length; i++) {
common = Math.min(paths[i].segmentCount() - 1, common);
if (common <= 0 || !Objects.equals(paths[0].getDevice(), paths[i].getDevice())) {
return null;
}

for (int j = 0; j < common; j++) {
if (!Objects.equals(paths[i].segment(j), paths[0].segment(j))) {
common = j;
break;
}
}
}

if (common <= 0) {
return null;
}

return paths[0].uptoSegment(common);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ public ICompilationUnit handleOpen(DidOpenTextDocumentParams params) {
OpenableElementInfo elementInfo = (OpenableElementInfo) pkg.getElementInfo();
elementInfo.addChild(unit);
}
} else { // File not exists
return unit;
}
} catch (CoreException e) {
// ignored
Expand Down Expand Up @@ -422,7 +424,7 @@ public ICompilationUnit handleClosed(DidCloseTextDocumentParams params) {
synchronized (toReconcile) {
toReconcile.remove(unit);
}
if (isSyntaxMode(unit) || unit.getResource().isDerived()) {
if (isSyntaxMode(unit) || !unit.exists() || unit.getResource().isDerived()) {
createDiagnosticsHandler(unit).clearDiagnostics();
} else if (hasUnsavedChanges(unit)) {
unit.discardWorkingCopy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public DocumentSymbolHandler(boolean hierarchicalDocumentSymbolSupported) {
public List<Either<SymbolInformation, DocumentSymbol>> documentSymbol(DocumentSymbolParams params, IProgressMonitor monitor) {

ITypeRoot unit = JDTUtils.resolveTypeRoot(params.getTextDocument().getUri());
if (unit == null) {
if (unit == null || !unit.exists()) {
return Collections.emptyList();
}

Expand All @@ -90,7 +90,11 @@ private SymbolInformation[] getOutline(ITypeRoot unit, IProgressMonitor monitor)
collectChildren(unit, elements, symbols, monitor);
return symbols.toArray(new SymbolInformation[symbols.size()]);
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Problem getting outline for" + unit.getElementName(), e);
if (!unit.exists()) {
JavaLanguageServerPlugin.logError("Problem getting outline for " + unit.getElementName() + ": File not found.");
} else {
JavaLanguageServerPlugin.logException("Problem getting outline for " + unit.getElementName(), e);
}
}
return new SymbolInformation[0];
}
Expand Down Expand Up @@ -134,7 +138,11 @@ private List<DocumentSymbol> getHierarchicalOutline(ITypeRoot unit, IProgressMon
} catch (OperationCanceledException e) {
logInfo("User abort while collecting the document symbols.");
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Problem getting outline for" + unit.getElementName(), e);
if (!unit.exists()) {
JavaLanguageServerPlugin.logError("Problem getting outline for " + unit.getElementName() + ": File not found.");
} else {
JavaLanguageServerPlugin.logException("Problem getting outline for " + unit.getElementName(), e);
}
}
return emptyList();
}
Expand Down
Loading