Search in sources :

Example 1 with AssistContext

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

the class LinkedNamesAssistProposal method apply.

/* (non-Javadoc)
     * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
     */
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    try {
        Point seletion = viewer.getSelectedRange();
        // get full ast
        CompilationUnit root = SharedASTProvider.getAST(fContext.getCompilationUnit(), SharedASTProvider.WAIT_YES, null);
        ASTNode nameNode = NodeFinder.perform(root, fNode.getStartPosition(), fNode.getLength());
        final int pos = fNode.getStartPosition();
        ASTNode[] sameNodes;
        if (nameNode instanceof SimpleName) {
            sameNodes = LinkedNodeFinder.findByNode(root, (SimpleName) nameNode);
        } else {
            sameNodes = new ASTNode[] { nameNode };
        }
        // sort for iteration order, starting with the node @ offset
        Arrays.sort(sameNodes, new Comparator<ASTNode>() {

            public int compare(ASTNode o1, ASTNode o2) {
                return rank(o1) - rank(o2);
            }

            /**
                 * Returns the absolute rank of an <code>ASTNode</code>. Nodes
                 * preceding <code>offset</code> are ranked last.
                 *
                 * @param node the node to compute the rank for
                 * @return the rank of the node with respect to the invocation offset
                 */
            private int rank(ASTNode node) {
                int relativeRank = node.getStartPosition() + node.getLength() - pos;
                if (relativeRank < 0)
                    return Integer.MAX_VALUE + relativeRank;
                else
                    return relativeRank;
            }
        });
        IDocument document = viewer.getDocument();
        LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
        for (int i = 0; i < sameNodes.length; i++) {
            ASTNode elem = sameNodes[i];
            RegionImpl region = new RegionImpl();
            region.setOffset(elem.getStartPosition());
            region.setLength(elem.getLength());
            group.addPositions(region);
        //				group.addPosition(new LinkedPosition(document, elem.getStartPosition(), elem.getLength(), i));
        }
        LinkedModeModelImpl model = new LinkedModeModelImpl();
        model.addGroups(group);
        //			model.forceInstall();
        model.setEscapePosition(offset);
        this.linkedModel = model;
        if (fContext instanceof AssistContext) {
        //				IEditorPart editor = ((AssistContext)fContext).getEditor();
        //				if (editor instanceof JavaEditor) {
        //					model.addLinkingListener(new EditorHighlightingSynchronizer((JavaEditor)editor));
        //				}
        }
        if (fValueSuggestion != null) {
            document.replace(nameNode.getStartPosition(), nameNode.getLength(), fValueSuggestion);
        //				IRegion selectedRegion = ui.getSelectedRegion();
        //				seletion = new Point(selectedRegion.getOffset(), fValueSuggestion.length());
        }
    //			viewer.setSelectedRange(seletion.x, seletion.y); // by default full word is selected, restore original selection
    } catch (BadLocationException e) {
        JavaPlugin.log(e);
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) SimpleName(org.eclipse.jdt.core.dom.SimpleName) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 2 with AssistContext

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

the class AssistQuickFixTest17 method testUnrollMultiCatch2.

@Test
public void testUnrollMultiCatch2() 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("    void foo() {\n");
    buf.append("        try {\n");
    buf.append("            System.out.println(\"foo\");\n");
    buf.append("        } catch (IllegalArgumentException | NullPointerException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (RuntimeException e) {\n");
    buf.append("            e.printStackTrace();\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("catch");
    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("    void foo() {\n");
    buf.append("        try {\n");
    buf.append("            System.out.println(\"foo\");\n");
    buf.append("        } catch (IllegalArgumentException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (NullPointerException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (RuntimeException e) {\n");
    buf.append("            e.printStackTrace();\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) List(java.util.List) Test(org.junit.Test)

Example 3 with AssistContext

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

the class AssistQuickFixTest17 method testPickoutTypeFromMulticatch2.

@Test
public void testPickoutTypeFromMulticatch2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | java.lang.NoSuchMethodException | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    String string = "MethodException";
    int offset = buf.toString().indexOf(string);
    AssistContext context = getCorrectionContext(cu, offset, 0);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 4);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected1 = buf.toString();
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() throws java.lang.NoSuchMethodException {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected2 = buf.toString();
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("import java.lang.reflect.InvocationTargetException;\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        try {\n");
    buf.append("            String.class.getConstructor().newInstance();\n");
    buf.append("        } catch (InstantiationException | IllegalAccessException\n");
    buf.append("                | IllegalArgumentException | InvocationTargetException\n");
    buf.append("                | SecurityException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        } catch (java.lang.NoSuchMethodException e) {\n");
    buf.append("            e.printStackTrace();\n");
    buf.append("        }\n");
    buf.append("    }\n");
    buf.append("}\n");
    String expected3 = buf.toString();
    assertExpectedExistInProposals(proposals, new String[] { expected1, expected2, expected3 });
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) List(java.util.List) Test(org.junit.Test)

Example 4 with AssistContext

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

the class AssistQuickFixTest18 method testConvertToAnonymousClassCreation1.

@Test
public void testConvertToAnonymousClassCreation1() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("interface I {\n");
    buf.append("    void method();\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(I i) {\n");
    buf.append("    }\n");
    buf.append("    void foo() {\n");
    buf.append("        bar(() -> {\n");
    buf.append("            System.out.println();\n");
    buf.append("            System.out.println();\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("->");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 4);
    assertCorrectLabels(proposals);
    buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("interface I {\n");
    buf.append("    void method();\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    void bar(I i) {\n");
    buf.append("    }\n");
    buf.append("    void foo() {\n");
    buf.append("        bar(new I() {\n");
    buf.append("            @Override\n");
    buf.append("            public void method() {\n");
    buf.append("                System.out.println();\n");
    buf.append("                System.out.println();\n");
    buf.append("            }\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) List(java.util.List) Test(org.junit.Test)

Example 5 with AssistContext

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

the class AssistQuickFixTest18 method testAssignParamToField2.

@Test
public void testAssignParamToField2() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package test1;\n");
    buf.append("public interface I {\n");
    buf.append("    static void bar(int x) {\n");
    buf.append("        System.out.println(x);\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("I.java", buf.toString(), false, null);
    int offset = buf.toString().indexOf("x");
    AssistContext context = getCorrectionContext(cu, offset, 0);
    assertNoErrors(context);
    List proposals = collectAssists(context, false);
    assertNumberOfProposals(proposals, 0);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AssistContext(org.eclipse.jdt.internal.ui.text.correction.AssistContext) 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