Search in sources :

Example 16 with RefactorWorkspaceEdit

use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit 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 17 with RefactorWorkspaceEdit

use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit 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 18 with RefactorWorkspaceEdit

use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.

the class GetRefactorEditHandlerTest method testExtractVariableAllOccurrence.

@Test
public void testExtractVariableAllOccurrence() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("class A{\n");
    buf.append("	void m(int i){\n");
    buf.append("		int x= /*]*/0/*[*/;\n");
    buf.append("	}\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getRange(cu, null);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, params);
    RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
    Assert.assertNotNull(refactorEdit);
    Assert.assertNotNull(refactorEdit.edit);
    String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("class A{\n");
    buf.append("	void m(int i){\n");
    buf.append("		int j = 0;\n");
    buf.append("        int x= /*]*/j/*[*/;\n");
    buf.append("	}\n");
    buf.append("}\n");
    AbstractSourceTestCase.compareSource(buf.toString(), actual);
    Assert.assertNotNull(refactorEdit.command);
    Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
    Assert.assertNotNull(refactorEdit.command.getArguments());
    Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) GetRefactorEditParams(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.GetRefactorEditParams) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Example 19 with RefactorWorkspaceEdit

use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.

the class GetRefactorEditHandlerTest method testExtractField.

@Test
public void testExtractField() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("class A{\n");
    buf.append("	void m(int i){\n");
    buf.append("		int x= /*]*/0/*[*/;\n");
    buf.append("	}\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getRange(cu, null);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_FIELD_COMMAND, Arrays.asList(InitializeScope.CURRENT_METHOD.getName()), params);
    RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
    Assert.assertNotNull(refactorEdit);
    Assert.assertNotNull(refactorEdit.edit);
    String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("class A{\n");
    buf.append("	private int i;\n\n");
    buf.append("    void m(int i){\n");
    buf.append("		this.i = 0;\n");
    buf.append("        int x= /*]*/this.i/*[*/;\n");
    buf.append("	}\n");
    buf.append("}\n");
    AbstractSourceTestCase.compareSource(buf.toString(), actual);
    Assert.assertNotNull(refactorEdit.command);
    Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
    Assert.assertNotNull(refactorEdit.command.getArguments());
    Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) GetRefactorEditParams(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.GetRefactorEditParams) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

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