Search in sources :

Example 26 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.

the class AssistQuickFixTest method testAssignParamToFieldInGeneric.

@Test
public void testAssignParamToFieldInGeneric() throws Exception {
    Preferences corePrefs = JavaPlugin.getJavaCorePluginPreferences();
    corePrefs.setValue(JavaCore.CODEASSIST_FIELD_PREFIXES, "f");
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("public class E<T> {\n");
    buf.append("    public  E(int count, Vector<String>[] vec) {\n");
    buf.append("        super();\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String selection = "vec";
    int offset = buf.toString().indexOf(selection);
    AssistContext context = getCorrectionContext(cu, offset, selection.length());
    List<IJavaCompletionProposal> proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 2);
    assertCorrectLabels(proposals);
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("public class E<T> {\n");
    buf.append("    private Vector<String>[] fVec;\n");
    buf.append("\n");
    buf.append("    public  E(int count, Vector<String>[] vec) {\n");
    buf.append("        super();\n");
    buf.append("        fVec = vec;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String ex1 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { ex1 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) Preferences(org.eclipse.core.runtime.Preferences) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) Test(org.junit.Test)

Example 27 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.

the class AssistQuickFixTest method testExtractToMethod3.

@Test
public void testExtractToMethod3() throws Exception {
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=41302
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        int a = 1;\n");
    buf.append("        int b = 1;\n");
    buf.append("        int d = a + b;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    int offset1 = buf.toString().indexOf("int a = 1;");
    int offset2 = buf.toString().indexOf("a + b;") + 6;
    AssistContext context = getCorrectionContext(cu, offset1, offset2 - offset1);
    List<IJavaCompletionProposal> proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 3);
    assertCorrectLabels(proposals);
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        extracted();\n");
    buf.append("    }\n");
    buf.append("\n");
    buf.append("    private void extracted() {\n");
    buf.append("        int a = 1;\n");
    buf.append("        int b = 1;\n");
    buf.append("        int d = a + b;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String ex1 = buf.toString();
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("public class E {\n");
    buf.append("    void foo() {\n");
    buf.append("        final int a = 1;\n");
    buf.append("        final int b = 1;\n");
    buf.append("        final int d = a + b;\n");
    buf.append("    }\n");
    buf.append("}\n");
    String ex2 = buf.toString();
    // Wrap in buf.append() (to clipboard)
    String ex3 = null;
    assertExpectedExistInProposals(proposals, new String[] { ex1, ex2, ex3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) Test(org.junit.Test)

Example 28 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.

the class AssistQuickFixTest method testInvertEquals6.

@Test
public void testInvertEquals6() 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("    static String get() {\n");
    buf.append("        return \"a\";\n");
    buf.append("    }\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        \"a\".equals(A.get());\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String str = "equals";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str), 0);
    List<IJavaCompletionProposal> proposals = collectAssists(context, FILTER_EQ);
    assertNumberOfProposals(proposals, 1);
    assertCorrectLabels(proposals);
    cu = pack1.createCompilationUnit("E.java", buf.toString(), true, null);
    CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
    String preview = getPreviewContent(proposal);
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("class A {\n");
    buf.append("    static String get() {\n");
    buf.append("        return \"a\";\n");
    buf.append("    }\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        A.get().equals(\"a\");\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(preview, buf.toString());
    cu = pack1.createCompilationUnit("E.java", buf.toString(), true, null);
    context = getCorrectionContext(cu, buf.toString().indexOf(str), 0);
    proposals = collectAssists(context, FILTER_EQ);
    assertNumberOfProposals(proposals, 1);
    assertCorrectLabels(proposals);
    proposal = (CUCorrectionProposal) proposals.get(0);
    preview = getPreviewContent(proposal);
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("class A {\n");
    buf.append("    static String get() {\n");
    buf.append("        return \"a\";\n");
    buf.append("    }\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        \"a\".equals(A.get());\n");
    buf.append("    }\n");
    buf.append("}\n");
    assertEqualString(preview, buf.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) Test(org.junit.Test)

Example 29 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.

the class AssistQuickFixTest method testAssignToLocal16.

// bug 217984
@Test
public void testAssignToLocal16() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("import java.util.ArrayList;\n");
    buf.append("import java.util.List;\n");
    buf.append("import java.util.RandomAccess;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("\n");
    buf.append("class Gen<E extends List<String> & RandomAccess> extends ArrayList<E> {\n");
    buf.append("    void foo() {\n");
    buf.append("        Gen<? super Vector<String>> gs = new Gen<>();\n");
    buf.append("        gs.get(0);\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("Gen.java", buf.toString(), false, null);
    String str = "gs.get(0)";
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf(str) + str.length(), 0);
    List<IJavaCompletionProposal> proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 7);
    assertCorrectLabels(proposals);
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("import java.util.ArrayList;\n");
    buf.append("import java.util.List;\n");
    buf.append("import java.util.RandomAccess;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("\n");
    buf.append("class Gen<E extends List<String> & RandomAccess> extends ArrayList<E> {\n");
    buf.append("    void foo() {\n");
    buf.append("        Gen<? super Vector<String>> gs = new Gen<>();\n");
    buf.append("        List<String> list = gs.get(0);\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("import java.util.ArrayList;\n");
    buf.append("import java.util.List;\n");
    buf.append("import java.util.RandomAccess;\n");
    buf.append("import java.util.Vector;\n");
    buf.append("\n");
    buf.append("class Gen<E extends List<String> & RandomAccess> extends ArrayList<E> {\n");
    buf.append("    private List<String> list;\n");
    buf.append("\n");
    buf.append("    void foo() {\n");
    buf.append("        Gen<? super Vector<String>> gs = new Gen<>();\n");
    buf.append("        list = gs.get(0);\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1, expected2 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) Test(org.junit.Test)

Example 30 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project eclipse.jdt.ui by eclipse-jdt.

the class AssistQuickFixTest method testConvertToStringBufferNoFixWithoutString.

@Test
public void testConvertToStringBufferNoFixWithoutString() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("public class A {\n");
    buf.append("    public void foo() {\n");
    buf.append("        int strX = 5+1;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("A.java", buf.toString(), false, null);
    AssistContext context = getCorrectionContext(cu, buf.toString().indexOf("strX ="), 0);
    List<IJavaCompletionProposal> proposals = collectAssists(context, false);
    assertCommandIdDoesNotExist(proposals, QuickAssistProcessor.CONVERT_TO_STRING_BUFFER_ID);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) Test(org.junit.Test)

Aggregations

IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)1716 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1665 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1652 Test (org.junit.Test)1647 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)869 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)590 AssistContext (org.eclipse.jdt.internal.ui.text.correction.AssistContext)578 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)67 CompletionProposalCollector (org.eclipse.jdt.ui.text.java.CompletionProposalCollector)63 IDocument (org.eclipse.jface.text.IDocument)62 FillArgumentNamesCompletionProposalCollector (org.eclipse.jdt.internal.ui.text.java.FillArgumentNamesCompletionProposalCollector)40 ArrayList (java.util.ArrayList)34 IEditorPart (org.eclipse.ui.IEditorPart)33 HashMap (java.util.HashMap)32 Document (org.eclipse.jface.text.Document)29 IInvocationContext (org.eclipse.jdt.ui.text.java.IInvocationContext)26 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)26 Path (org.eclipse.core.runtime.Path)23 IProblem (org.eclipse.jdt.core.compiler.IProblem)22 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)15