Search in sources :

Example 1 with GetRefactorEditParams

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

the class GetRefactorEditHandlerTest method testExtractVariable.

@Test
public void testExtractVariable() 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_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 2 with GetRefactorEditParams

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

the class GetRefactorEditHandlerTest method testExtractMethod.

@Test
public void testExtractMethod() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public int foo(boolean b1, boolean b2) {\n");
    buf.append("        int n = 0;\n");
    buf.append("        int i = 0;\n");
    buf.append("        /*[*/\n");
    buf.append("        if (b1)\n");
    buf.append("            i = 1;\n");
    buf.append("        if (b2)\n");
    buf.append("            n = n + i;\n");
    buf.append("        /*]*/\n");
    buf.append("        return n;\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_METHOD_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("\n");
    buf.append("public class E {\n");
    buf.append("    public int foo(boolean b1, boolean b2) {\n");
    buf.append("        int n = 0;\n");
    buf.append("        int i = 0;\n");
    buf.append("        n = extracted(b1, b2, n, i);\n");
    buf.append("        return n;\n");
    buf.append("    }\n");
    buf.append("\n");
    buf.append("    private int extracted(boolean b1, boolean b2, int n, int i) {\n");
    buf.append("        /*[*/\n");
    buf.append("        if (b1)\n");
    buf.append("            i = 1;\n");
    buf.append("        if (b2)\n");
    buf.append("            n = n + i;\n");
    buf.append("        /*]*/\n");
    buf.append("        return n;\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 3 with GetRefactorEditParams

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

the class GetRefactorEditHandlerTest method testExtractConstant.

@Test
public void testExtractConstant() 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_CONSTANT_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("	private static final int _0 = /*]*/0/*[*/;\n");
    buf.append("\n");
    buf.append("    void m(int i){\n");
    buf.append("		int x= _0;\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 4 with GetRefactorEditParams

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

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

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)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 RefactorWorkspaceEdit (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)5 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)5 Range (org.eclipse.lsp4j.Range)5 Test (org.junit.Test)5