use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testMoveMultiFiles.
@Test
public void testMoveMultiFiles() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", false, null);
// @formatter:off
ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package jdtls.test1;\r\n" + "\r\n" + "public class A {\r\n" + " private B b = new B();\r\n" + "}", true, null);
// @formatter:on
// @formatter:off
ICompilationUnit unitB = pack1.createCompilationUnit("B.java", "package jdtls.test1;\r\n" + "\r\n" + "public class B {\r\n" + "}", true, null);
// @formatter:on
IPackageFragment pack2 = sourceFolder.createPackageFragment("jdtls.test2", false, null);
String packageUri = JDTUtils.getFileURI(pack2.getResource());
RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveResource", new String[] { JDTUtils.toURI(unitA), JDTUtils.toURI(unitB) }, packageUri, true), new NullProgressMonitor());
assertNotNull(refactorEdit);
assertNotNull(refactorEdit.edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
assertEquals(4, changes.size());
// @formatter:off
String expected = "package jdtls.test2;\r\n" + "\r\n" + "public class B {\r\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
List<TextEdit> edits = new ArrayList<>(textEdit.getEdits());
assertEquals(expected, TextEditUtil.apply(unitB.getSource(), edits));
RenameFile renameFileB = (RenameFile) changes.get(1).getRight();
assertNotNull(renameFileB);
assertEquals(JDTUtils.toURI(unitB), renameFileB.getOldUri());
assertEquals(ResourceUtils.fixURI(unitB.getResource().getRawLocationURI()).replace("test1", "test2"), renameFileB.getNewUri());
// @formatter:off
expected = "package jdtls.test2;\r\n" + "\r\n" + "public class A {\r\n" + " private B b = new B();\r\n" + "}";
// @formatter:on
textEdit = changes.get(2).getLeft();
assertNotNull(textEdit);
edits = new ArrayList<>(textEdit.getEdits());
assertEquals(expected, TextEditUtil.apply(unitA.getSource(), edits));
RenameFile renameFileA = (RenameFile) changes.get(3).getRight();
assertNotNull(renameFileA);
assertEquals(JDTUtils.toURI(unitA), renameFileA.getOldUri());
assertEquals(ResourceUtils.fixURI(unitA.getResource().getRawLocationURI()).replace("test1", "test2"), renameFileA.getNewUri());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testMoveStaticInnerType.
@Test
public void testMoveStaticInnerType() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
// @formatter:off
ICompilationUnit unitFoo = pack1.createCompilationUnit("Foo.java", "package test1;\n" + "\n" + "public class Foo {\n" + "}", false, null);
// @formatter:on
// @formatter:off
ICompilationUnit cu = pack1.createCompilationUnit("E.java", "package test1;\n" + "\n" + "public class E {\n" + " public void print() {\n" + " new Inner().run();\n" + " }\n" + " static class Inner {\n" + " void run() {\n" + " }\n" + " }\n" + "}", false, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "class Inner");
RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveTypeToClass", params, "Foo", true), new NullProgressMonitor());
assertNotNull(refactorEdit);
assertNotNull(refactorEdit.edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
assertEquals(2, changes.size());
// @formatter:off
String expected = "package test1;\n" + "\n" + "public class E {\n" + " public void print() {\n" + " new Foo.Inner().run();\n" + " }\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(cu.getSource(), textEdit.getEdits()));
// @formatter:off
expected = "package test1;\n" + "\n" + "public class Foo {\n" + "\n" + " static class Inner {\n" + " void run() {\n" + " }\n" + " }\n" + "}";
// @formatter:on
textEdit = changes.get(1).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(unitFoo.getSource(), textEdit.getEdits()));
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testMoveStaticMethod.
@Test
public void testMoveStaticMethod() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
// @formatter:off
ICompilationUnit cu = pack1.createCompilationUnit("E.java", "package test1;\n" + "\n" + "public class E {\n" + " public static void foo() {\n" + " /*[*//*]*/\n" + " }\n" + "\n" + " public void bar() {\n" + " foo();\n" + " }\n" + "}", false, null);
// @formatter:on
// @formatter:off
ICompilationUnit unitFoo = pack1.createCompilationUnit("Foo.java", "package test1;\n" + "\n" + "public class Foo {\n" + "}", false, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "/*[*//*]*/");
RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveStaticMember", params, "Foo", true), new NullProgressMonitor());
assertNotNull(refactorEdit);
assertNotNull(refactorEdit.edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
assertEquals(2, changes.size());
// @formatter:off
String expected = "package test1;\n" + "\n" + "public class E {\n" + " public void bar() {\n" + " Foo.foo();\n" + " }\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(cu.getSource(), textEdit.getEdits()));
// @formatter:off
expected = "package test1;\n" + "\n" + "public class Foo {\n" + "\n" + " public static void foo() {\n" + " /*[*//*]*/\n" + " }\n" + "}";
// @formatter:on
textEdit = changes.get(1).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(unitFoo.getSource(), textEdit.getEdits()));
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testMoveFile.
@Test
public void testMoveFile() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", false, null);
// @formatter:off
ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package jdtls.test1;\r\n" + "import jdtls.test2.B;\r\n" + "\r\n" + "public class A {\r\n" + " private B b = new B();\r\n" + "}", true, null);
// @formatter:on
IPackageFragment pack2 = sourceFolder.createPackageFragment("jdtls.test2", false, null);
// @formatter:off
ICompilationUnit unitB = pack2.createCompilationUnit("B.java", "package jdtls.test2;\r\n" + "\r\n" + "public class B {\r\n" + "}", true, null);
// @formatter:on
IPackageFragment pack3 = sourceFolder.createPackageFragment("jdtls.test3", false, null);
String packageUri = JDTUtils.getFileURI(pack3.getResource());
RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveResource", new String[] { JDTUtils.toURI(unitB) }, packageUri, true), new NullProgressMonitor());
assertNotNull(refactorEdit);
assertNotNull(refactorEdit.edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
assertEquals(3, changes.size());
// @formatter:off
String expected = "package jdtls.test1;\r\n" + "import jdtls.test3.B;\r\n" + "\r\n" + "public class A {\r\n" + " private B b = new B();\r\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(unitA.getSource(), textEdit.getEdits()));
// @formatter:off
expected = "package jdtls.test3;\r\n" + "\r\n" + "public class B {\r\n" + "}";
// @formatter:on
textEdit = changes.get(1).getLeft();
assertNotNull(textEdit);
List<TextEdit> edits = new ArrayList<>(textEdit.getEdits());
assertEquals(expected, TextEditUtil.apply(unitB.getSource(), edits));
RenameFile renameFile = (RenameFile) changes.get(2).getRight();
assertNotNull(renameFile);
assertEquals(JDTUtils.toURI(unitB), renameFile.getOldUri());
assertEquals(ResourceUtils.fixURI(unitB.getResource().getRawLocationURI()).replace("test2", "test3"), renameFile.getNewUri());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit 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();
}
}
Aggregations