use of org.eclipse.lsp4j.FileRename in project eclipse.jdt.ls by eclipse.
the class FileEventHandler method computeFileRenameEdit.
private static WorkspaceEdit computeFileRenameEdit(FileRename[] renameEvents, IProgressMonitor monitor) {
SubMonitor submonitor = SubMonitor.convert(monitor, "Computing file rename updates...", 100 * renameEvents.length);
WorkspaceEdit root = null;
for (FileRename event : renameEvents) {
String oldUri = event.getOldUri();
String newUri = event.getNewUri();
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(oldUri);
SubMonitor splitedMonitor = submonitor.split(100);
try {
if (unit == null || !unit.exists()) {
JavaLanguageServerPlugin.logError("Failed to compute the file rename edit because the file '" + oldUri + "' doesn't exist.");
continue;
}
if (unit != null) {
String oldPrimaryType = getPrimaryTypeName(oldUri);
String newPrimaryType = getPrimaryTypeName(newUri);
if (!unit.getType(newPrimaryType).exists() && unit.getType(oldPrimaryType).exists()) {
WorkspaceEdit edit = getRenameEdit(unit.getType(oldPrimaryType), newPrimaryType, splitedMonitor);
root = ChangeUtil.mergeChanges(root, edit, true);
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Computing the file rename edit: ", e);
} finally {
splitedMonitor.done();
}
}
submonitor.done();
return root;
}
use of org.eclipse.lsp4j.FileRename in project eclipse.jdt.ls by eclipse.
the class FileEventHandler method handleWillRenameFiles.
public static WorkspaceEdit handleWillRenameFiles(RenameFilesParams params, IProgressMonitor monitor) {
List<FileRename> files = params.getFiles();
if (files == null || files.isEmpty()) {
return null;
}
FileRename[] renameFiles = new FileRename[0];
FileRename[] renameFolders = new FileRename[0];
FileRename[] moveFiles = new FileRename[0];
if (files.size() == 1) {
FileRename renameEvent = files.get(0);
if (isFileNameRenameEvent(renameEvent)) {
renameFiles = new FileRename[] { renameEvent };
} else if (isFolderRenameEvent(renameEvent)) {
renameFolders = new FileRename[] { renameEvent };
} else if (isMoveEvent(renameEvent)) {
moveFiles = new FileRename[] { renameEvent };
}
} else {
moveFiles = files.stream().filter(event -> isMoveEvent(event)).toArray(FileRename[]::new);
}
if (renameFiles.length == 0 && renameFolders.length == 0 && moveFiles.length == 0) {
return null;
}
SourcePath[] sourcePaths = getSourcePaths();
if (sourcePaths == null || sourcePaths.length == 0) {
return null;
}
WorkspaceEdit root = null;
SubMonitor submonitor = SubMonitor.convert(monitor, "Computing rename updates...", renameFiles.length + renameFolders.length + moveFiles.length);
if (renameFiles.length > 0) {
WorkspaceEdit edit = computeFileRenameEdit(renameFiles, submonitor.split(renameFiles.length));
root = ChangeUtil.mergeChanges(root, edit, true);
}
if (renameFolders.length > 0) {
WorkspaceEdit edit = computePackageRenameEdit(renameFolders, sourcePaths, submonitor.split(renameFolders.length));
root = ChangeUtil.mergeChanges(root, edit, true);
}
if (moveFiles.length > 0) {
WorkspaceEdit edit = computeMoveEdit(moveFiles, sourcePaths, submonitor.split(moveFiles.length));
root = ChangeUtil.mergeChanges(root, edit, true);
}
submonitor.done();
return ChangeUtil.hasChanges(root) ? root : null;
}
use of org.eclipse.lsp4j.FileRename in project eclipse.jdt.ls by eclipse.
the class FileEventHandler method computeMoveEdit.
private static WorkspaceEdit computeMoveEdit(FileRename[] moveEvents, SourcePath[] sourcePaths, IProgressMonitor monitor) {
IPath[] newPaths = Stream.of(moveEvents).map(event -> ResourceUtils.filePathFromURI(event.getNewUri())).toArray(IPath[]::new);
IPath destinationPath = ResourceUtils.getLongestCommonPath(newPaths);
if (destinationPath == null) {
return null;
}
// Verify all files are moving to the same destination.
for (FileRename event : moveEvents) {
IPath oldPath = ResourceUtils.filePathFromURI(event.getOldUri());
IPath expectedNewPath = destinationPath.append(oldPath.lastSegment());
IPath actualNewPath = ResourceUtils.filePathFromURI(event.getNewUri());
if (!Objects.equals(expectedNewPath, actualNewPath)) {
JavaLanguageServerPlugin.logError("Failed to compute move refactoring because the files are not moving to the same destination " + destinationPath.toOSString());
return null;
}
}
IPackageFragment destinationPackage = resolvePackageFragment(destinationPath, sourcePaths);
if (destinationPackage == null) {
return null;
}
// formatter:off
ICompilationUnit[] cus = Stream.of(moveEvents).filter(event -> {
IPath oldPath = ResourceUtils.filePathFromURI(event.getOldUri());
return oldPath != null && oldPath.toFile().isFile();
}).map(event -> JDTUtils.resolveCompilationUnit(event.getOldUri())).filter(cu -> cu != null && cu.getJavaProject() != null).toArray(ICompilationUnit[]::new);
// formatter:on
List<ICompilationUnit> nonClasspathCus = new ArrayList<>();
for (ICompilationUnit unit : cus) {
if (!unit.getJavaProject().isOnClasspath(unit)) {
nonClasspathCus.add(unit);
}
}
WorkspaceEdit[] root = new WorkspaceEdit[1];
if (cus.length > 0) {
try {
// otherwise invoking cu.getBuffer() will throw exception.
for (ICompilationUnit cu : nonClasspathCus) {
cu.becomeWorkingCopy(monitor);
}
IReorgDestination packageDestination = ReorgDestinationFactory.createDestination(destinationPackage);
ResourcesPlugin.getWorkspace().run((pm) -> {
root[0] = MoveHandler.move(new IResource[0], cus, packageDestination, true, pm);
}, monitor);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Failed to compute the move update", e);
} finally {
for (ICompilationUnit cu : nonClasspathCus) {
try {
cu.discardWorkingCopy();
} catch (JavaModelException e) {
// do nothing
}
}
}
}
return ChangeUtil.hasChanges(root[0]) ? root[0] : null;
}
use of org.eclipse.lsp4j.FileRename in project eclipse.jdt.ls by eclipse.
the class FileEventHandler method computePackageRenameEdit.
private static WorkspaceEdit computePackageRenameEdit(FileRename[] renameEvents, SourcePath[] sourcePaths, IProgressMonitor monitor) {
WorkspaceEdit[] root = new WorkspaceEdit[1];
SubMonitor submonitor = SubMonitor.convert(monitor, "Computing package rename updates...", 100 * renameEvents.length);
for (FileRename event : renameEvents) {
IPath oldLocation = ResourceUtils.filePathFromURI(event.getOldUri());
IPath newLocation = ResourceUtils.filePathFromURI(event.getNewUri());
IPackageFragment oldPackageFragment = resolvePackageFragment(oldLocation, sourcePaths);
SubMonitor renameMonitor = submonitor.split(100);
try {
if (oldPackageFragment != null && !oldPackageFragment.isDefaultPackage() && oldPackageFragment.getResource() != null) {
String newPackageName = resolvePackageName(newLocation, sourcePaths);
if (newPackageName == null) {
continue;
}
oldPackageFragment.getResource().refreshLocal(IResource.DEPTH_INFINITE, null);
if (oldPackageFragment.exists()) {
ResourcesPlugin.getWorkspace().run((pm) -> {
WorkspaceEdit edit = getRenameEdit(oldPackageFragment, newPackageName, pm);
root[0] = ChangeUtil.mergeChanges(root[0], edit, true);
}, oldPackageFragment.getSchedulingRule(), IResource.NONE, renameMonitor);
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Failed to compute the package rename update", e);
} finally {
renameMonitor.done();
}
}
submonitor.done();
return ChangeUtil.hasChanges(root[0]) ? root[0] : null;
}
use of org.eclipse.lsp4j.FileRename in project eclipse.jdt.ls by eclipse.
the class FileEventHandlerTest method testRenamePackage.
// Test renaming package from "parent.pack2" to "parent.newpack2"
@Test
public void testRenamePackage() throws JavaModelException, BadLocationException {
when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
IPackageFragment pack1 = sourceFolder.createPackageFragment("parent.pack1", false, null);
IPackageFragment pack2 = sourceFolder.createPackageFragment("parent.pack2", false, null);
StringBuilder codeA = new StringBuilder();
codeA.append("package parent.pack1;\n");
codeA.append("import parent.pack2.B;\n");
codeA.append("public class A {\n");
codeA.append(" public void foo() {\n");
codeA.append(" B b = new B();\n");
codeA.append(" b.foo();\n");
codeA.append(" }\n");
codeA.append("}\n");
StringBuilder codeB = new StringBuilder();
codeB.append("package parent.pack2;\n");
codeB.append("public class B {\n");
codeB.append(" public B() {}\n");
codeB.append(" public void foo() {}\n");
codeB.append("}\n");
ICompilationUnit cuA = pack1.createCompilationUnit("A.java", codeA.toString(), false, null);
ICompilationUnit cuB = pack2.createCompilationUnit("B.java", codeB.toString(), false, null);
String pack2Uri = JDTUtils.getFileURI(pack2.getResource());
String newPack2Uri = pack2Uri.replace("pack2", "newpack2");
WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new RenameFilesParams(Arrays.asList(new FileRename(pack2Uri, newPack2Uri))), new NullProgressMonitor());
assertNotNull(edit);
List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = edit.getDocumentChanges();
assertEquals(2, documentChanges.size());
assertTrue(documentChanges.get(0).isLeft());
assertEquals(documentChanges.get(0).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
assertEquals(TextEditUtil.apply(codeA.toString(), documentChanges.get(0).getLeft().getEdits()), "package parent.pack1;\n" + "import parent.newpack2.B;\n" + "public class A {\n" + " public void foo() {\n" + " B b = new B();\n" + " b.foo();\n" + " }\n" + "}\n");
assertTrue(documentChanges.get(1).isLeft());
assertEquals(documentChanges.get(1).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuB));
assertEquals(TextEditUtil.apply(codeB.toString(), documentChanges.get(1).getLeft().getEdits()), "package parent.newpack2;\n" + "public class B {\n" + " public B() {}\n" + " public void foo() {}\n" + "}\n");
}
Aggregations