Search in sources :

Example 1 with MoveParams

use of org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams 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 MoveParams

use of org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams 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 MoveParams

use of org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams 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 MoveParams

use of org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams 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 MoveParams

use of org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams in project eclipse.jdt.ls by eclipse.

the class MoveHandlerTest method testMoveInstanceMethod.

@Test
public void testMoveInstanceMethod() throws Exception {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    // @formatter:off
    ICompilationUnit cuSecond = pack1.createCompilationUnit("Second.java", "package test1;\n" + "\n" + "public class Second {\n" + "    public void foo() {\n" + "    }\n" + "}", false, null);
    // @formatter:on
    // @formatter:off
    pack1.createCompilationUnit("Third.java", "package test1;\n" + "\n" + "public class Third {\n" + "    public void bar() {\n" + "    }\n" + "}", false, null);
    // @formatter:on
    // @formatter:off
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", "package test1;\n" + "\n" + "public class E {\n" + "    Second s;\n" + "    String name;\n" + "    int id;\n" + "    public void print(Third t) {\n" + "        System.out.println(name);\n" + "        s.foo();\n" + "        t.bar();\n" + "    }\n" + "\n" + "    public void log(Third t) {\n" + "        print(t);\n" + "    }\n" + "}", false, null);
    // @formatter:on
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "s.foo");
    MoveParams moveParams = new MoveParams("moveInstanceMethod", new String[] { JDTUtils.toURI(cu) }, params);
    MoveDestinationsResponse response = MoveHandler.getMoveDestinations(moveParams);
    assertNotNull(response);
    assertNull(response.errorMessage);
    assertNotNull(response.destinations);
    assertEquals(2, response.destinations.length);
    RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveInstanceMethod", params, response.destinations[1], 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" + "    Second s;\n" + "    String name;\n" + "    int id;\n" + "    public void log(Third t) {\n" + "        s.print(this, t);\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 Second {\n" + "    public void foo() {\n" + "    }\n" + "\n" + "	public void print(E e, Third t) {\n" + "	    System.out.println(e.name);\n" + "	    foo();\n" + "	    t.bar();\n" + "	}\n" + "}";
    // @formatter:on
    textEdit = changes.get(1).getLeft();
    assertNotNull(textEdit);
    assertEquals(expected, TextEditUtil.apply(cuSecond.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) MoveDestinationsResponse(org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveDestinationsResponse) 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)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)9 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)9 MoveParams (org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams)9 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)9 Test (org.junit.Test)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 RefactorWorkspaceEdit (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)7 TextDocumentEdit (org.eclipse.lsp4j.TextDocumentEdit)7 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)7 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)6 MoveDestinationsResponse (org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveDestinationsResponse)3 ArrayList (java.util.ArrayList)2 RenameFile (org.eclipse.lsp4j.RenameFile)2 TextEdit (org.eclipse.lsp4j.TextEdit)2 CreateFile (org.eclipse.lsp4j.CreateFile)1 ResourceOperation (org.eclipse.lsp4j.ResourceOperation)1