Search in sources :

Example 1 with RefactorWorkspaceEdit

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());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) MoveParams(org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams) ArrayList(java.util.ArrayList) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) RenameFile(org.eclipse.lsp4j.RenameFile) TextEdit(org.eclipse.lsp4j.TextEdit) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 2 with RefactorWorkspaceEdit

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()));
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) MoveParams(org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 3 with RefactorWorkspaceEdit

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()));
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) MoveParams(org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 4 with RefactorWorkspaceEdit

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());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) MoveParams(org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams) ArrayList(java.util.ArrayList) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) RenameFile(org.eclipse.lsp4j.RenameFile) TextEdit(org.eclipse.lsp4j.TextEdit) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 5 with RefactorWorkspaceEdit

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

Aggregations

RefactorWorkspaceEdit (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)19 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)18 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)14 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)12 Test (org.junit.Test)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 MoveParams (org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams)7 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)7 TextDocumentEdit (org.eclipse.lsp4j.TextDocumentEdit)7 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)7 ArrayList (java.util.ArrayList)5 CoreException (org.eclipse.core.runtime.CoreException)5 SubMonitor (org.eclipse.core.runtime.SubMonitor)5 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)5 AbstractSelectionTest (org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)5 GetRefactorEditParams (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.GetRefactorEditParams)5 Range (org.eclipse.lsp4j.Range)5 Gson (com.google.gson.Gson)3 URI (java.net.URI)3 IType (org.eclipse.jdt.core.IType)3