Search in sources :

Example 6 with SourcePath

use of org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath 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;
}
Also used : FileRename(org.eclipse.lsp4j.FileRename) SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) SubMonitor(org.eclipse.core.runtime.SubMonitor) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit)

Example 7 with SourcePath

use of org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath 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)

Example 8 with SourcePath

use of org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath in project eclipse.jdt.ls by eclipse.

the class BuildPathCommandTest method testBuildPathOperationInEclipseProject.

@Test
public void testBuildPathOperationInEclipseProject() throws Exception {
    importProjects("eclipse/hello");
    IProject project = WorkspaceHelper.getProject("hello");
    PreferenceManager manager = JavaLanguageServerPlugin.getPreferencesManager();
    manager.getPreferences().setRootPaths(Arrays.asList(project.getLocation()));
    ListCommandResult listResult = (ListCommandResult) BuildPathCommand.listSourcePaths();
    assertTrue(listResult.status);
    SourcePath[] sourcePaths = listResult.data;
    assertNotNull(sourcePaths);
    assertEquals(sourcePaths.length, 2);
    assertEquals(sourcePaths[0].displayPath, new Path("hello").append("src").toOSString());
    assertEquals(sourcePaths[1].displayPath, new Path("hello").append("test").toOSString());
    IResource srcJavaResource = project.findMember("src/java");
    Result addSrcJavaResult = BuildPathCommand.addToSourcePath(JDTUtils.getFileURI(srcJavaResource));
    assertFalse(addSrcJavaResult.status);
    IJavaProject javaProject = JavaCore.create(project);
    IResource nopackageResource = project.findMember("nopackage");
    Result addNopackageResult = BuildPathCommand.addToSourcePath(JDTUtils.getFileURI(nopackageResource));
    assertTrue(addNopackageResult.status);
    assertEquals(ProjectUtils.listSourcePaths(javaProject).length, 3);
    Result removeNopackageResult = BuildPathCommand.removeFromSourcePath(JDTUtils.getFileURI(nopackageResource));
    assertTrue(removeNopackageResult.status);
    assertEquals(ProjectUtils.listSourcePaths(javaProject).length, 2);
}
Also used : SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) IPath(org.eclipse.core.runtime.IPath) SourcePath(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath) Path(org.eclipse.core.runtime.Path) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult) IJavaProject(org.eclipse.jdt.core.IJavaProject) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) IProject(org.eclipse.core.resources.IProject) IResource(org.eclipse.core.resources.IResource) Result(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.Result) ListCommandResult(org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Aggregations

SourcePath (org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath)8 ListCommandResult (org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.ListCommandResult)6 IProject (org.eclipse.core.resources.IProject)4 IResource (org.eclipse.core.resources.IResource)4 IPath (org.eclipse.core.runtime.IPath)4 Result (org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.Result)4 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)4 PreferenceManager (org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager)4 Path (org.eclipse.core.runtime.Path)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 Test (org.junit.Test)3 CoreException (org.eclipse.core.runtime.CoreException)2 SubMonitor (org.eclipse.core.runtime.SubMonitor)2 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)2 FileRename (org.eclipse.lsp4j.FileRename)2 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1