Search in sources :

Example 6 with MoveParams

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

the class MoveHandlerTest method testGetInstanceMethodDestinations.

@Test
public void testGetInstanceMethodDestinations() throws Exception {
    when(preferenceManager.getClientPreferences().isMoveRefactoringSupported()).thenReturn(true);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    // @formatter:off
    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" + "        s.foo();\n" + "        t.bar();\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);
    assertEquals("t", ((LspVariableBinding) response.destinations[0]).name);
    assertFalse(((LspVariableBinding) response.destinations[0]).isField);
    assertEquals("Third", ((LspVariableBinding) response.destinations[0]).type);
    assertEquals("s", ((LspVariableBinding) response.destinations[1]).name);
    assertTrue(((LspVariableBinding) response.destinations[1]).isField);
    assertEquals("Second", ((LspVariableBinding) response.destinations[1]).type);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) 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) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 7 with MoveParams

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

the class MoveHandlerTest method testMoveInnerTypeToFile.

@Test
public void testMoveInnerTypeToFile() throws Exception {
    System.setProperty("line.separator", "\n");
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    // @formatter:off
    ICompilationUnit cu = pack1.createCompilationUnit("Top.java", "package test1;\n" + "\n" + "public class Top {\n" + "    String name;\n\n" + "    public class Inner {\n" + "        public void print() {\n" + "            System.out.println(Top.this.name);\n" + "        }\n" + "    }\n" + "}", false, null);
    // @formatter:on
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "class Inner");
    RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveTypeToNewFile", params, "Foo", 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 test1;\n" + "\n" + "public class Top {\n" + "    String name;\n" + "}";
    // @formatter:on
    TextDocumentEdit textEdit = changes.get(0).getLeft();
    assertNotNull(textEdit);
    assertEquals(expected, TextEditUtil.apply(cu.getSource(), textEdit.getEdits()));
    ResourceOperation resourceOperation = changes.get(1).getRight();
    assertNotNull(resourceOperation);
    assertTrue(resourceOperation instanceof CreateFile);
    assertEquals(ResourceUtils.fixURI(cu.getResource().getRawLocationURI()).replace("Top", "Inner"), ((CreateFile) resourceOperation).getUri());
    // @formatter:off
    expected = "package test1;\n" + "\n" + "public class Inner {\n" + "    /**\n" + "	 *\n" + "	 */\n" + "	private final Top top;\n\n" + "	/**\n" + "	 * @param top\n" + "	 */\n" + "	Inner(Top top) {\n" + "		this.top = top;\n" + "	}\n\n" + "	public void print() {\n" + "        System.out.println(this.top.name);\n" + "    }\n" + "}";
    // @formatter:on
    textEdit = changes.get(2).getLeft();
    assertNotNull(textEdit);
    assertEquals(expected, TextEditUtil.apply(pack1.getCompilationUnit("Inner.java").getWorkingCopy(null), textEdit.getEdits()));
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) CreateFile(org.eclipse.lsp4j.CreateFile) 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) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 8 with MoveParams

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

the class MoveHandlerTest method testMoveStaticField.

@Test
public void testMoveStaticField() throws Exception {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    // @formatter:off
    pack1.createCompilationUnit("Foo.java", "package test1;\n" + "\n" + "public class Foo {\n" + "    public void foo() {\n" + "    }\n" + "}", false, null);
    // @formatter:on
    // @formatter:off
    ICompilationUnit unitUtility = pack1.createCompilationUnit("Utility.java", "package test1;\n" + "\n" + "public class Utility {\n" + "}", false, null);
    // @formatter:on
    // @formatter:off
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", "package test1;\n" + "\n" + "public class E {\n" + "    static Foo foo = new Foo();\n" + "    public void print() {\n" + "        foo.foo();\n" + "    }\n" + "}", false, null);
    // @formatter:on
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "new Foo()");
    RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveStaticMember", params, "Utility", 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" + "        Utility.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 Utility {\n" + "\n" + "	static Foo foo = new Foo();\n" + "}";
    // @formatter:on
    textEdit = changes.get(1).getLeft();
    assertNotNull(textEdit);
    assertEquals(expected, TextEditUtil.apply(unitUtility.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 9 with MoveParams

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

the class MoveHandlerTest method testGetPackageDestinations.

@Test
public void testGetPackageDestinations() throws JavaModelException, BadLocationException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", false, null);
    // @formatter:off
    ICompilationUnit unit = pack1.createCompilationUnit("A.java", "package jdtls.test1;\r\n" + "\r\n" + "public class A {\r\n" + "}", true, null);
    // @formatter:on
    MoveParams params = new MoveParams("moveResource", new String[] { JDTUtils.toURI(unit) });
    MoveDestinationsResponse response = MoveHandler.getMoveDestinations(params);
    assertNotNull(response);
    assertNotNull(response.destinations);
    assertEquals(3, response.destinations.length);
    assertTrue(((PackageNode) response.destinations[0]).isDefaultPackage);
    assertEquals("jdtls", ((PackageNode) response.destinations[1]).displayName);
    assertEquals("jdtls.test1", ((PackageNode) response.destinations[2]).displayName);
    assertTrue(((PackageNode) response.destinations[2]).isParentOfSelectedFile);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) 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) 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