use of org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestination in project eclipse.jdt.ls by eclipse.
the class MoveHandler method moveCU.
private static RefactorWorkspaceEdit moveCU(String[] sourceUris, String targetUri, boolean updateReferences, IProgressMonitor monitor) {
URI targetURI = JDTUtils.toURI(targetUri);
if (targetURI == null) {
return new RefactorWorkspaceEdit("Failed to move the files because of illegal uri '" + targetUri + "'.");
}
List<IJavaElement> elements = new ArrayList<>();
for (String uri : sourceUris) {
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
if (unit == null) {
continue;
}
elements.add(unit);
}
SubMonitor submonitor = SubMonitor.convert(monitor, "Moving File...", 100);
try {
IResource[] resources = ReorgUtils.getResources(elements);
IJavaElement[] javaElements = ReorgUtils.getJavaElements(elements);
IContainer[] targetContainers = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(targetURI);
if (targetContainers == null || targetContainers.length == 0) {
return new RefactorWorkspaceEdit("Failed to move the files because cannot find the target folder '" + targetUri + "' in the workspace.");
} else if ((resources == null || resources.length == 0) && (javaElements == null || javaElements.length == 0)) {
return new RefactorWorkspaceEdit("Failed to move the files because cannot find any resources or Java elements associated with the files.");
}
// For multi-module scenario, findContainersForLocationURI API may return a container array, need put the result from the nearest project in front.
Arrays.sort(targetContainers, (Comparator<IContainer>) (IContainer a, IContainer b) -> {
return a.getFullPath().toPortableString().length() - b.getFullPath().toPortableString().length();
});
IJavaElement targetElement = null;
for (IContainer container : targetContainers) {
targetElement = JavaCore.create(container);
if (targetElement instanceof IPackageFragmentRoot) {
targetElement = ((IPackageFragmentRoot) targetElement).getPackageFragment("");
}
if (targetElement != null) {
break;
}
}
if (targetElement == null) {
JavaLanguageServerPlugin.logError("Failed to move the files because cannot find the package associated with the path '" + targetUri + "'.");
return new RefactorWorkspaceEdit("Failed to move the files because cannot find the package associated with the path '" + targetUri + "'.");
}
IReorgDestination packageDestination = ReorgDestinationFactory.createDestination(targetElement);
WorkspaceEdit edit = move(resources, javaElements, packageDestination, updateReferences, submonitor);
if (edit == null) {
return new RefactorWorkspaceEdit("Cannot enable move operation.");
}
return new RefactorWorkspaceEdit(edit);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Failed to move the files.", e);
return new RefactorWorkspaceEdit("Failed to move the files because of " + e.toString());
} finally {
submonitor.done();
}
}
use of org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestination 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;
}
Aggregations