Search in sources :

Example 1 with InferSelectionParams

use of org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams in project eclipse.jdt.ls by eclipse.

the class InferSelectionHandlerTest method testInferSelectionWhenExtractVariable.

@Test
public void testInferSelectionWhenExtractVariable() 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() {\n");
    buf.append("        boolean b1 = true;\n");
    buf.append("        boolean b2 = false;\n");
    buf.append("        boolean b3 = true && !b2;\n");
    buf.append("        boolean b4 = b3 || /*|*/b2 && b1;\n");
    buf.append("        if ((b1||b4) && (b2||b3))\n");
    buf.append("            return 1;\n");
    buf.append("        \n");
    buf.append("        return 0;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getVerticalBarRange(cu);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    InferSelectionParams inferParams = new InferSelectionParams(RefactorProposalUtility.EXTRACT_VARIABLE_COMMAND, params);
    List<SelectionInfo> infos = InferSelectionHandler.inferSelectionsForRefactor(inferParams);
    Assert.assertNotNull(infos);
    Assert.assertEquals(infos.size(), 3);
    Assert.assertEquals(infos.get(0).name, "b2");
    Assert.assertEquals(infos.get(0).length, 2);
    Assert.assertEquals(infos.get(1).name, "b2 && b1");
    Assert.assertEquals(infos.get(1).length, 8);
    Assert.assertEquals(infos.get(2).name, "b3 || b2 && b1");
    Assert.assertEquals(infos.get(2).length, 19);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InferSelectionParams(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams) SelectionInfo(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)

Example 2 with InferSelectionParams

use of org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams in project eclipse.jdt.ls by eclipse.

the class InferSelectionHandlerTest method testInferSelectionWhenExtractField.

@Test
public void testInferSelectionWhenExtractField() 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("    private String test = \"test\";\n");
    buf.append("    public int foo() {\n");
    buf.append("        int hashCode = this./*|*/test.hashCode();\n");
    buf.append("        return 0;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getVerticalBarRange(cu);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    InferSelectionParams inferParams = new InferSelectionParams(RefactorProposalUtility.EXTRACT_FIELD_COMMAND, params);
    List<SelectionInfo> infos = InferSelectionHandler.inferSelectionsForRefactor(inferParams);
    Assert.assertNotNull(infos);
    Assert.assertEquals(infos.size(), 2);
    Assert.assertEquals(infos.get(0).name, "this.test");
    Assert.assertEquals(infos.get(0).length, 14);
    Assert.assertEquals(infos.get(1).name, "this.test.hashCode()");
    Assert.assertEquals(infos.get(1).length, 25);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InferSelectionParams(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams) SelectionInfo(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)

Example 3 with InferSelectionParams

use of org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams in project eclipse.jdt.ls by eclipse.

the class InferSelectionHandlerTest method testInferSelectionWhenExtractConstant.

@Test
public void testInferSelectionWhenExtractConstant() 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() {\n");
    buf.append("        boolean b1 = true;\n");
    buf.append("        boolean b2 = false;\n");
    buf.append("        boolean b3 = /*|*/true || false;\n");
    buf.append("        boolean b4 = b3 || b2 && b1;\n");
    buf.append("        if ((b1||b4) && (b2||b3))\n");
    buf.append("            return 1;\n");
    buf.append("        \n");
    buf.append("        return 0;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getVerticalBarRange(cu);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    InferSelectionParams inferParams = new InferSelectionParams(RefactorProposalUtility.EXTRACT_CONSTANT_COMMAND, params);
    List<SelectionInfo> infos = InferSelectionHandler.inferSelectionsForRefactor(inferParams);
    Assert.assertNotNull(infos);
    Assert.assertEquals(infos.size(), 2);
    Assert.assertEquals(infos.get(0).name, "true");
    Assert.assertEquals(infos.get(0).length, 4);
    Assert.assertEquals(infos.get(1).name, "true || false");
    Assert.assertEquals(infos.get(1).length, 13);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InferSelectionParams(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams) SelectionInfo(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)

Example 4 with InferSelectionParams

use of org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams in project eclipse.jdt.ls by eclipse.

the class InferSelectionHandlerTest method testInferSelectionWhenExtractMethod.

@Test
public void testInferSelectionWhenExtractMethod() 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() {\n");
    buf.append("        boolean b1 = true;\n");
    buf.append("        boolean b2 = false;\n");
    buf.append("        boolean b3 = true && !b2;\n");
    buf.append("        if (b1 && (/*|*/b2 || b3))\n");
    buf.append("            return 1;\n");
    buf.append("        \n");
    buf.append("        return 0;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getVerticalBarRange(cu);
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
    InferSelectionParams inferParams = new InferSelectionParams(RefactorProposalUtility.EXTRACT_METHOD_COMMAND, params);
    List<SelectionInfo> infos = InferSelectionHandler.inferSelectionsForRefactor(inferParams);
    Assert.assertNotNull(infos);
    Assert.assertEquals(infos.size(), 3);
    Assert.assertEquals(infos.get(0).name, "b2");
    Assert.assertEquals(infos.get(0).length, 2);
    Assert.assertEquals(infos.get(1).name, "b2 || b3");
    Assert.assertEquals(infos.get(1).length, 8);
    Assert.assertEquals(infos.get(2).name, "b1 && (b2 || b3)");
    Assert.assertEquals(infos.get(2).length, 21);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InferSelectionParams(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams) SelectionInfo(org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 AbstractSelectionTest (org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)4 InferSelectionParams (org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.InferSelectionParams)4 SelectionInfo (org.eclipse.jdt.ls.core.internal.handlers.InferSelectionHandler.SelectionInfo)4 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)4 Range (org.eclipse.lsp4j.Range)4 Test (org.junit.Test)4