Search in sources :

Example 71 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AdvancedQuickAssistTest method testJoinAndIfStatements4.

@Test
public void testJoinAndIfStatements4() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(boolean a, int b, boolean c) {\n");
    buf.append("        if (a && (b == 0))\n");
    buf.append("            if (c) {\n");
    buf.append("                b= 9;\n");
    buf.append("            }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("if (c");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(boolean a, int b, boolean c) {\n");
    buf.append("        if (a && (b == 0) && c) {\n");
    buf.append("            b= 9;\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 72 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AdvancedQuickAssistTest method testConvertToIfReturn1.

@Test
public void testConvertToIfReturn1() throws Exception {
    // positive cases
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo1() {\n");
    buf.append("        if (a) {\n");
    buf.append("            System.out.println(\"1\");\n");
    buf.append("            System.out.println(\"11\");\n");
    buf.append("        }\n");
    buf.append("    }\n\n");
    buf.append("    public void foo2() {\n");
    buf.append("        bar();\n");
    buf.append("        if (b) {\n");
    buf.append("            System.out.println(\"2\");\n");
    buf.append("            System.out.println(\"22\");\n");
    buf.append("        }\n");
    buf.append("    }\n\n");
    buf.append("    public void foo3() {\n");
    buf.append("        if (c) {\n");
    buf.append("            if (d) {\n");
    buf.append("                System.out.println(\"3\");\n");
    buf.append("                System.out.println(\"33\");\n");
    buf.append("        	}\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String str = "if (a)";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    StringBuffer buf1 = new StringBuffer();
    buf1.append("package test1;\n");
    buf1.append("public class E {\n");
    buf1.append("    public void foo1() {\n");
    buf1.append("        if (!a)\n");
    buf1.append("            return;\n");
    buf1.append("        System.out.println(\"1\");\n");
    buf1.append("        System.out.println(\"11\");\n");
    buf1.append("    }\n\n");
    buf1.append("    public void foo2() {\n");
    buf1.append("        bar();\n");
    buf1.append("        if (b) {\n");
    buf1.append("            System.out.println(\"2\");\n");
    buf1.append("            System.out.println(\"22\");\n");
    buf1.append("        }\n");
    buf1.append("    }\n\n");
    buf1.append("    public void foo3() {\n");
    buf1.append("        if (c) {\n");
    buf1.append("            if (d) {\n");
    buf1.append("                System.out.println(\"3\");\n");
    buf1.append("                System.out.println(\"33\");\n");
    buf1.append("        	}\n");
    buf1.append("        }\n");
    buf1.append("    }\n");
    buf1.append("}\n");
    String expected1 = buf1.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
    str = "if (b)";
    context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    buf1 = new StringBuffer();
    buf1.append("package test1;\n");
    buf1.append("public class E {\n");
    buf1.append("    public void foo1() {\n");
    buf1.append("        if (a) {\n");
    buf1.append("            System.out.println(\"1\");\n");
    buf1.append("            System.out.println(\"11\");\n");
    buf1.append("        }\n");
    buf1.append("    }\n\n");
    buf1.append("    public void foo2() {\n");
    buf1.append("        bar();\n");
    buf1.append("        if (!b)\n");
    buf1.append("            return;\n");
    buf1.append("        System.out.println(\"2\");\n");
    buf1.append("        System.out.println(\"22\");\n");
    buf1.append("    }\n\n");
    buf1.append("    public void foo3() {\n");
    buf1.append("        if (c) {\n");
    buf1.append("            if (d) {\n");
    buf1.append("                System.out.println(\"3\");\n");
    buf1.append("                System.out.println(\"33\");\n");
    buf1.append("        	}\n");
    buf1.append("        }\n");
    buf1.append("    }\n");
    buf1.append("}\n");
    String expected2 = buf1.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected2 });
    str = "if (d)";
    context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 5);
    assertCorrectLabels(proposals);
    buf1 = new StringBuffer();
    buf1.append("package test1;\n");
    buf1.append("public class E {\n");
    buf1.append("    public void foo1() {\n");
    buf1.append("        if (a) {\n");
    buf1.append("            System.out.println(\"1\");\n");
    buf1.append("            System.out.println(\"11\");\n");
    buf1.append("        }\n");
    buf1.append("    }\n\n");
    buf1.append("    public void foo2() {\n");
    buf1.append("        bar();\n");
    buf1.append("        if (b) {\n");
    buf1.append("            System.out.println(\"2\");\n");
    buf1.append("            System.out.println(\"22\");\n");
    buf1.append("        }\n");
    buf1.append("    }\n\n");
    buf1.append("    public void foo3() {\n");
    buf1.append("        if (c) {\n");
    buf1.append("            if (!d)\n");
    buf1.append("                return;\n");
    buf1.append("            System.out.println(\"3\");\n");
    buf1.append("            System.out.println(\"33\");\n");
    buf1.append("        }\n");
    buf1.append("    }\n");
    buf1.append("}\n");
    String expected3 = buf1.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 73 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AdvancedQuickAssistTest method testExchangeOperands2.

@Test
public void testExchangeOperands2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public boolean foo(int a, int b) {\n");
    buf.append("        return (0 == (a & b));\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("==");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public boolean foo(int a, int b) {\n");
    buf.append("        return ((a & b) == 0);\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 74 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AdvancedQuickAssistTest method testAddParenthesesForExpression3.

@Test
public void testAddParenthesesForExpression3() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(int a, int b, int c) {\n");
    buf.append("        if (a + b == 0 && b + c > 3) {\n");
    buf.append("            b= 9;\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf("=="), 0);
    List proposals = collectAssists(context, false);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(int a, int b, int c) {\n");
    buf.append("        if ((a + b == 0) && b + c > 3) {\n");
    buf.append("            b= 9;\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 75 with AssistContext

use of org.eclipse.jdt.internal.ui.text.correction.AssistContext in project che by eclipse.

the class AdvancedQuickAssistTest method testConvertSwitchToIfBug252040_1.

@Test
public void testConvertSwitchToIfBug252040_1() throws Exception {
    //https://bugs.eclipse.org/bugs/show_bug.cgi?id=252040
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        switch (getSomethingWithSideEffects()) {\n");
    buf.append("        case 1:\n");
    buf.append("            System.out.println();\n");
    buf.append("            break;\n");
    buf.append("        case 2:\n");
    buf.append("            System.out.println();\n");
    buf.append("            break;\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("    private int getSomethingWithSideEffects() {\n");
    buf.append("        System.out.println(\"side effect\");\n");
    buf.append("        return 2;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("switch");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 1);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        int somethingWithSideEffects = getSomethingWithSideEffects();\n");
    buf.append("        if (somethingWithSideEffects == 1) {\n");
    buf.append("            System.out.println();\n");
    buf.append("        } else if (somethingWithSideEffects == 2) {\n");
    buf.append("            System.out.println();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("    private int getSomethingWithSideEffects() {\n");
    buf.append("        System.out.println(\"side effect\");\n");
    buf.append("        return 2;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)390 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)385 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)384 Test (org.junit.Test)384 List (java.util.List)378 ArrayList (java.util.ArrayList)313 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)57 HashMap (java.util.HashMap)15 Map (java.util.Map)15 ProblemLocation (org.eclipse.jdt.internal.ui.text.correction.ProblemLocation)8 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)6 Ignore (org.junit.Ignore)4 IDocument (org.eclipse.jface.text.IDocument)3 Iterator (java.util.Iterator)2 IBuffer (org.eclipse.jdt.core.IBuffer)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 Point (org.eclipse.swt.graphics.Point)2 TextViewer (org.eclipse.che.jdt.javaeditor.TextViewer)1 LinkedModeModelImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl)1