Search in sources :

Example 16 with ICompletionProposal

use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.

the class ParameterGuessingProposal method apply.

/*
     * @see ICompletionProposalExtension#apply(IDocument, char)
     */
@Override
public void apply(final IDocument document, char trigger, int offset) {
    super.apply(document, trigger, offset);
    int baseOffset = getReplacementOffset();
    String replacement = getReplacementString();
    if (fPositions != null && getTextViewer() != null) {
        LinkedModeModelImpl model = new LinkedModeModelImpl();
        for (int i = 0; i < fPositions.length; i++) {
            LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
            int positionOffset = fPositions[i].getOffset();
            int positionLength = fPositions[i].getLength();
            if (fChoices[i].length < 2) {
                RegionImpl region = new RegionImpl();
                region.setOffset(positionOffset);
                region.setLength(positionLength);
                //                        group.addPositions(new LinkedPosition(document, positionOffset, positionLength, LinkedPositionGroup.NO_STOP));
                group.addPositions(region);
            } else {
                //                        ensurePositionCategoryInstalled(document, model);
                //                        document.addPosition(getCategory(), fPositions[i]);
                RegionImpl region = new RegionImpl();
                region.setOffset(positionOffset);
                region.setLength(positionLength);
                //                        group.addPositions(
                //                                new ProposalPosition(document, positionOffset, positionLength, LinkedPositionGroup.NO_STOP, fChoices[i]));
                group.addPositions(region);
                LinkedDataImpl data = new LinkedDataImpl();
                for (ICompletionProposal proposal : fChoices[i]) {
                    data.addValues(proposal.getDisplayString());
                }
                group.setData(data);
            }
            model.addGroups(group);
        }
        model.setEscapePosition(baseOffset + replacement.length());
        this.linkedModel = model;
        //                model.forceInstall();
        //                JavaEditor editor = getJavaEditor();
        //                if (editor != null) {
        //                    model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
        //                }
        //                LinkedModeUI ui = new EditorLinkedModeUI(model, getTextViewer());
        //                ui.setExitPosition(getTextViewer(), baseOffset + replacement.length(), 0, Integer.MAX_VALUE);
        //                // exit character can be either ')' or ';'
        //                final char exitChar = replacement.charAt(replacement.length() - 1);
        //                ui.setExitPolicy(new ExitPolicy(exitChar, document) {
        //                    @Override
        //                    public ExitFlags doExit(LinkedModeModel model2, VerifyEvent event, int offset2, int length) {
        //                        if (event.character == ',') {
        //                            for (int i = 0; i < fPositions.length - 1; i++) { // not for the last one
        //                                Position position = fPositions[i];
        //                                if (position.offset <= offset2 && offset2 + length <= position.offset + position.length) {
        //                                    try {
        //                                        ITypedRegion partition = TextUtilities
        //                                                .getPartition(document, IJavaPartitions.JAVA_PARTITIONING, offset2 + length, false);
        //                                        if (IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
        //                                            || offset2 + length == partition.getOffset() + partition.getLength()) {
        //                                            event.character = '\t';
        //                                            event.keyCode = SWT.TAB;
        //                                            return null;
        //                                        }
        //                                    } catch (BadLocationException e) {
        //                                        // continue; not serious enough to log
        //                                    }
        //                                }
        //                            }
        //                        } else if (event.character == ')' && exitChar != ')') {
        //                            // exit from link mode when user is in the last ')' position.
        //                            Position position = fPositions[fPositions.length - 1];
        //                            if (position.offset <= offset2 && offset2 + length <= position.offset + position.length) {
        //								return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false);
        //							}
        //						}
        //						return super.doExit(model2, event, offset2, length);
        //					}
        //				});
        //				ui.setCyclingMode(LinkedModeUI.CYCLE_WHEN_NO_PARENT);
        //				ui.setDoContextInfo(true);
        //				ui.enter();
        fSelectedRegion = new Region(baseOffset + replacement.length(), 0);
    } else {
        fSelectedRegion = new Region(baseOffset + replacement.length(), 0);
    }
}
Also used : LinkedPositionGroupImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) LinkedModeModelImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl) RegionImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl) LinkedDataImpl(org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl) Point(org.eclipse.swt.graphics.Point)

Example 17 with ICompletionProposal

use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.

the class CompletionJavadocTest method testJavadoc.

@Test
public void testJavadoc() 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("    /**\n");
    buf.append("     * Test JavaDoc.\n");
    buf.append("     */\n");
    buf.append("    public void foo(int i) {\n");
    buf.append("        foo(10);");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    List<ICompletionProposal> proposals = computeProposals(cu, buf.indexOf("    foo") + "    foo".length());
    Assertions.assertThat(proposals).hasSize(1);
    ICompletionProposal proposal = proposals.get(0);
    String result;
    if (proposal instanceof ICompletionProposalExtension5) {
        result = ((ICompletionProposalExtension5) proposal).getAdditionalProposalInfo(null).toString();
    } else {
        result = proposal.getAdditionalProposalInfo();
    }
    Assertions.assertThat(result).contains("Test JavaDoc.");
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) ICompletionProposalExtension5(org.eclipse.che.jface.text.contentassist.ICompletionProposalExtension5) QuickFixTest(org.eclipse.che.plugin.java.server.jdt.quickfix.QuickFixTest) Test(org.junit.Test)

Example 18 with ICompletionProposal

use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.

the class TypeMismatchQuickFixTests method testTypeMismatchPrimitiveTypes.

@Test
public void testTypeMismatchPrimitiveTypes() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("pack", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(long o) {\n");
    buf.append("        int i= o;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertCorrectLabels(proposals);
    assertNumberOfProposals(proposals, 3);
    ICompletionProposal proposal = (ICompletionProposal) proposals.get(0);
    assertTrue(proposal.getDisplayString().indexOf("Integer") == -1);
    ICompletionProposal proposal2 = (ICompletionProposal) proposals.get(1);
    assertTrue(proposal2.getDisplayString().indexOf("Integer") == -1);
    ICompletionProposal proposal3 = (ICompletionProposal) proposals.get(2);
    assertTrue(proposal3.getDisplayString().indexOf("Integer") == -1);
    String[] expected = new String[3];
    buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(long o) {\n");
    buf.append("        int i= (int) o;\n");
    buf.append("    }\n");
    buf.append("}\n");
    expected[0] = buf.toString();
    buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(int o) {\n");
    buf.append("        int i= o;\n");
    buf.append("    }\n");
    buf.append("}\n");
    expected[1] = buf.toString();
    buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public void foo(long o) {\n");
    buf.append("        long i= o;\n");
    buf.append("    }\n");
    buf.append("}\n");
    expected[2] = buf.toString();
    assertExpectedExistInProposals(proposals, expected);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 19 with ICompletionProposal

use of org.eclipse.che.jface.text.contentassist.ICompletionProposal in project che by eclipse.

the class TypeMismatchQuickFixTests method testTypeMismatchObjectAndPrimitiveType.

@Test
public void testTypeMismatchObjectAndPrimitiveType() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("pack", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        Object o= new Object();\n");
    buf.append("        int i= o;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    CompilationUnit astRoot = getASTRoot(cu);
    ArrayList proposals = collectCorrections(cu, astRoot);
    assertCorrectLabels(proposals);
    assertNumberOfProposals(proposals, 3);
    ICompletionProposal proposal = (ICompletionProposal) proposals.get(0);
    assertTrue(proposal.getDisplayString().indexOf("Integer") != -1);
    String[] expected = new String[3];
    buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        Object o= new Object();\n");
    buf.append("        int i= (Integer) o;\n");
    buf.append("    }\n");
    buf.append("}\n");
    expected[0] = buf.toString();
    buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        int o= new Object();\n");
    buf.append("        int i= o;\n");
    buf.append("    }\n");
    buf.append("}\n");
    expected[1] = buf.toString();
    buf = new StringBuffer();
    buf.append("package pack;\n");
    buf.append("\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public void foo() {\n");
    buf.append("        Object o= new Object();\n");
    buf.append("        Object i= o;\n");
    buf.append("    }\n");
    buf.append("}\n");
    expected[2] = buf.toString();
    assertExpectedExistInProposals(proposals, expected);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)19 Point (org.eclipse.swt.graphics.Point)9 ArrayList (java.util.ArrayList)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 JavaContentAssistInvocationContext (org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext)4 IDocument (org.eclipse.jface.text.IDocument)4 Position (org.eclipse.jface.text.Position)3 Test (org.junit.Test)3 TextViewer (org.eclipse.che.jdt.javaeditor.TextViewer)2 ICompletionProposalExtension5 (org.eclipse.che.jface.text.contentassist.ICompletionProposalExtension5)2 LinkedDataImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedDataImpl)2 LinkedModeModelImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedModeModelImpl)2 LinkedPositionGroupImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.LinkedPositionGroupImpl)2 RegionImpl (org.eclipse.che.plugin.java.server.dto.DtoServerImpls.RegionImpl)2 QuickFixTest (org.eclipse.che.plugin.java.server.jdt.quickfix.QuickFixTest)2 IBuffer (org.eclipse.jdt.core.IBuffer)2 IType (org.eclipse.jdt.core.IType)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2