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;
}
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;
}
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);
}
Aggregations