Search in sources :

Example 1 with IReorgDestination

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();
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IReorgDestination(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestination) ArrayList(java.util.ArrayList) SubMonitor(org.eclipse.core.runtime.SubMonitor) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) URI(java.net.URI) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) CoreException(org.eclipse.core.runtime.CoreException) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 2 with IReorgDestination

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) BuildPathCommand(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand) RenamePackageProcessor(org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenamePackageProcessor) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) JavaModelException(org.eclipse.jdt.core.JavaModelException) SubMonitor(org.eclipse.core.runtime.SubMonitor) RenameSupport(org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.RenameSupport) ProjectUtils(org.eclipse.jdt.ls.core.internal.ProjectUtils) CoreException(org.eclipse.core.runtime.CoreException) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) RefactoringTickProvider(org.eclipse.ltk.core.refactoring.RefactoringTickProvider) ArrayList(java.util.ArrayList) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) FileRename(org.eclipse.lsp4j.FileRename) IPath(org.eclipse.core.runtime.IPath) SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Change(org.eclipse.ltk.core.refactoring.Change) ReorgDestinationFactory(org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg.ReorgDestinationFactory) RenameFilesParams(org.eclipse.lsp4j.RenameFilesParams) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) NotCancelableProgressMonitor(org.eclipse.ltk.internal.core.refactoring.NotCancelableProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) IReorgDestination(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestination) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) IJavaElement(org.eclipse.jdt.core.IJavaElement) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) IResource(org.eclipse.core.resources.IResource) Path(org.eclipse.core.runtime.Path) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ChangeUtil(org.eclipse.jdt.ls.core.internal.ChangeUtil) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IReorgDestination(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestination) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) FileRename(org.eclipse.lsp4j.FileRename) CoreException(org.eclipse.core.runtime.CoreException) IResource(org.eclipse.core.resources.IResource)

Aggregations

ArrayList (java.util.ArrayList)2 IResource (org.eclipse.core.resources.IResource)2 CoreException (org.eclipse.core.runtime.CoreException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)2 IReorgDestination (org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestination)2 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)2 URI (java.net.URI)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects (java.util.Objects)1 Stream (java.util.stream.Stream)1 IContainer (org.eclipse.core.resources.IContainer)1 ResourcesPlugin (org.eclipse.core.resources.ResourcesPlugin)1 IPath (org.eclipse.core.runtime.IPath)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 Path (org.eclipse.core.runtime.Path)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1