Example usage for org.eclipse.jdt.core CompletionProposal getKind

List of usage examples for org.eclipse.jdt.core CompletionProposal getKind

Introduction

In this page you can find the example usage for org.eclipse.jdt.core CompletionProposal getKind.

Prototype

public int getKind() 

Source Link

Document

Returns the kind of completion being proposed.

Usage

From source file:at.bestsolution.fxide.jdt.editor.JDTJavaDocSupport.java

License:Open Source License

public static HtmlString toHtml(CompletionProposal proposal, IJavaProject jProject) throws JavaModelException {
    String content = null;/* w  ww .j ava2  s  .c  o m*/
    if (proposal.getKind() == org.eclipse.jdt.core.CompletionProposal.FIELD_REF) {
        IType jType = getOwnerType(proposal, jProject);
        if (jType != null) {
            IField field = jType.getField(String.valueOf(proposal.getName()));
            if (field != null && field.exists()) {
                try {
                    content = JavadocContentAccess2.getHTMLContent(field, true);
                } catch (CoreException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    } else if (proposal.getKind() == org.eclipse.jdt.core.CompletionProposal.METHOD_REF) {
        IType jType = getOwnerType(proposal, jProject);
        if (jType != null) {
            MethodUtil m = new MethodUtil(proposal, jType);
            IMethod method = m.resolve();
            if (method != null && method.exists()) {
                try {
                    content = JavadocContentAccess2.getHTMLContent(method, true);
                    if (content != null) {
                        content = content.replace("<pre><code>",
                                "<pre class=\"prettyprint\"><code class=\"language-java\">");
                    }
                } catch (CoreException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    if (content != null) {
        return new HtmlString("<html><header><style>"
                + ("theme.dark".equals(themeMgr.getCurrentTheme().getId()) ? darkCSS : defaultCSS)
                + "</style><script>" + prettifyJS + "\n</script></header><body onload='PR.prettyPrint();'>"
                + content + "</body></html>");
    }

    return null;
}

From source file:at.bestsolution.javafx.ide.jdt.internal.JavaEditor.java

License:Open Source License

@Inject
public JavaEditor(BorderPane pane, IEditorInput input) {
    editor = new SourceEditor();
    pane.setCenter(editor);/*w w  w . ja va2  s. c om*/

    IResourceFileInput fsInput = (IResourceFileInput) input;
    try {
        unit = ((ICompilationUnit) JavaCore.create(fsInput.getFile()))
                .getWorkingCopy(new FXWorkingCopyOwner(new IProblemRequestor() {
                    private List<ProblemMarker> l = new ArrayList<>();

                    @Override
                    public boolean isActive() {
                        // TODO Auto-generated method stub
                        return true;
                    }

                    @Override
                    public void endReporting() {
                        setMarkers(l);
                    }

                    @Override
                    public void beginReporting() {
                        l.clear();
                    }

                    @Override
                    public void acceptProblem(IProblem problem) {
                        int linenumber = problem.getSourceLineNumber();
                        int startCol = problem.getSourceStart();
                        int endCol = problem.getSourceEnd();

                        if (endCol == startCol) {
                            endCol++;
                        }

                        String description = problem.getMessage();
                        ProblemMarker marker = new ProblemMarker(
                                problem.isError() ? at.bestsolution.javafx.ide.editor.ProblemMarker.Type.ERROR
                                        : at.bestsolution.javafx.ide.editor.ProblemMarker.Type.WARNING,
                                linenumber, startCol, endCol, description);
                        l.add(marker);
                    }
                }), new NullProgressMonitor());
    } catch (JavaModelException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    final Document doc = createDocument(unit);
    editor.setDocument(doc);
    editor.setContentProposalComputer(new ContentProposalComputer() {

        @Override
        public List<Proposal> computeProposals(String line, String prefix, int offset) {
            final List<Proposal> l = new ArrayList<ContentProposalComputer.Proposal>();

            try {
                unit.codeComplete(offset, new CompletionRequestor() {

                    @Override
                    public void accept(CompletionProposal proposal) {
                        String completion = new String(proposal.getCompletion());

                        if (!Flags.isPublic(proposal.getFlags())) {
                            return;
                        }

                        if (proposal.getKind() == CompletionProposal.METHOD_REF) {
                            String sig = Signature.toString(new String(proposal.getSignature()),
                                    new String(proposal.getName()), null, false, false);
                            StyledString s = new StyledString(sig + " : " + Signature.getSimpleName(Signature
                                    .toString(Signature.getReturnType(new String(proposal.getSignature())))));
                            s.appendString(
                                    " - " + Signature.getSignatureSimpleName(
                                            new String(proposal.getDeclarationSignature())),
                                    Style.colored("#AAAAAA"));

                            l.add(new Proposal(Type.METHOD, completion, s));
                        } else if (proposal.getKind() == CompletionProposal.FIELD_REF) {
                            StyledString s = new StyledString(
                                    completion + " : "
                                            + (proposal.getSignature() != null
                                                    ? Signature.getSignatureSimpleName(
                                                            new String(proposal.getSignature()))
                                                    : "<unknown>"));
                            s.appendString(
                                    " - " + (proposal.getDeclarationSignature() != null
                                            ? Signature.getSignatureSimpleName(
                                                    new String(proposal.getDeclarationSignature()))
                                            : "<unknown>"),
                                    Style.colored("#AAAAAA"));
                            l.add(new Proposal(Type.FIELD, completion, s));
                        } else if (proposal.getKind() == CompletionProposal.TYPE_REF) {
                            if (proposal.getAccessibility() == IAccessRule.K_NON_ACCESSIBLE) {
                                return;
                            }

                            StyledString s = new StyledString(
                                    Signature.getSignatureSimpleName(new String(proposal.getSignature())));
                            s.appendString(" - " + new String(proposal.getDeclarationSignature()),
                                    Style.colored("#AAAAAA"));
                            l.add(new Proposal(Type.TYPE, new String(proposal.getCompletion()), s));
                        } else {
                            System.err.println(proposal);
                        }
                    }
                });
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return l;
        }

    });
    editor.setSaveCallback(new Runnable() {

        @Override
        public void run() {
            try {
                unit.commitWorkingCopy(true, null);
            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

    try {
        unit.reconcile(ICompilationUnit.NO_AST, true, null, null);
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.amashchenko.eclipse.strutsclipse.java.SimpleJavaCompletionProposal.java

License:Apache License

public SimpleJavaCompletionProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context,
        Image img) {//ww w.j  ava2 s . c o  m
    if (CompletionProposal.METHOD_REF == proposal.getKind()) {
        this.replacementString = String.valueOf(proposal.getName());
    } else {
        this.replacementString = String.valueOf(proposal.getCompletion());
    }
    this.displayString = context.getLabelProvider().createLabel(proposal);
    this.relevance = proposal.getRelevance();
    this.image = img;
}

From source file:com.amashchenko.eclipse.strutsclipse.java.SimpleJavaProposalCollector.java

License:Apache License

@Override
protected IJavaCompletionProposal createJavaCompletionProposal(CompletionProposal proposal) {
    if (collectMethods) {
        if (CompletionProposal.METHOD_REF == proposal.getKind() && Flags.isPublic(proposal.getFlags())) {
            char[] sig = proposal.getSignature();
            char[] declSig = proposal.getDeclarationSignature();
            // collect methods suitable for action methods ignoring Object
            // methods
            if (sig != null && declSig != null && ACTION_METHOD_SIGNATURE.equals(String.valueOf(sig))
                    && !OBJECT_SIGNATURE.equals(String.valueOf(declSig))) {
                return new SimpleJavaCompletionProposal(proposal, getInvocationContext(),
                        getImage(getLabelProvider().createImageDescriptor(proposal)));
            }/*w  ww.  jav  a2  s  .c o  m*/
        }
    } else {
        // collect packages and classes suitable for actions
        if ((CompletionProposal.PACKAGE_REF == proposal.getKind()
                || CompletionProposal.TYPE_REF == proposal.getKind()) && !Flags.isAbstract(proposal.getFlags())
                && !Flags.isInterface(proposal.getFlags()) && !Flags.isEnum(proposal.getFlags())) {
            return new SimpleJavaCompletionProposal(proposal, getInvocationContext(),
                    getImage(getLabelProvider().createImageDescriptor(proposal)));
        }
    }
    return null;
}

From source file:com.google.gwt.eclipse.core.editors.java.contentassist.JsniCompletionProposal.java

License:Open Source License

/**
 * Return proposals only for references to a package, type, field, method or
 * constructor (and nothing else).//  ww w  .  j av a  2 s.  c o m
 */
public static IJavaCompletionProposal create(IJavaCompletionProposal jdtProposal,
        CompletionProposal wrappedProposal, IJavaProject javaProject, int replaceOffset, int replaceLength) {
    switch (wrappedProposal.getKind()) {
    case CompletionProposal.PACKAGE_REF:
    case CompletionProposal.TYPE_REF:
    case CompletionProposal.FIELD_REF:
    case CompletionProposal.METHOD_REF:
        return new JsniCompletionProposal(jdtProposal, wrappedProposal, javaProject, replaceOffset,
                replaceLength);
    default:
        return null;
    }
}

From source file:com.google.gwt.eclipse.core.editors.java.contentassist.JsniCompletionProposalCollector.java

License:Open Source License

@Override
protected IJavaCompletionProposal createJavaCompletionProposal(CompletionProposal proposal) {
    IJavaCompletionProposal defaultProposal = super.createJavaCompletionProposal(proposal);

    // For members of inner classes, there's a bug in the JDT which results in
    // the declaration signature of the proposal missing the outer classes, so
    // we have to manually set the signature instead.
    if (proposal.getKind() == CompletionProposal.METHOD_REF
            || proposal.getKind() == CompletionProposal.FIELD_REF) {
        char[] typeSignature = Signature.createTypeSignature(qualifiedTypeName, true).toCharArray();
        proposal.setDeclarationSignature(typeSignature);
    }//from   ww  w .j  a v a2 s .  c o  m

    return JsniCompletionProposal.create(defaultProposal, proposal, getCompilationUnit().getJavaProject(),
            refOffset, refLength);
}

From source file:com.google.gwt.eclipse.core.uibinder.contentassist.computers.ProposalGeneratingCompletionRequestor.java

License:Open Source License

protected ICompletionProposal createProposal(CompletionProposal javaProposal) {
    String completion = String.valueOf(javaProposal.getCompletion());
    int kind = javaProposal.getKind();
    if (kind == CompletionProposal.TYPE_REF) {
        // Make sure it is fully qualified
        completion = JavaContentAssistUtilities.getFullyQualifiedTypeName(javaProposal);
    }/*ww  w. j  av  a  2  s  . c o  m*/

    if (forceFullyQualifiedFieldNames
            && (kind == CompletionProposal.FIELD_IMPORT || kind == CompletionProposal.FIELD_REF)) {
        char[] decSig = javaProposal.getDeclarationSignature();
        if (decSig != null && decSig.length > 2) {
            // declaration signatures for objects are like Ljava.lang.String;, so lop off first
            // and last chars
            completion = new String(decSig, 1, decSig.length - 2) + "."
                    + new String(javaProposal.getCompletion());
            completion = completion.replace('$', '.');
        }
    }

    ICompletionProposal jdtCompletionProposal = JavaContentAssistUtilities
            .getJavaCompletionProposal(javaProposal, context, javaProject);
    return ReplacementCompletionProposal.fromExistingCompletionProposal(completion, replaceOffset,
            replaceLength, jdtCompletionProposal);
}

From source file:com.microsoft.javapkgsrv.JavaParser.java

License:MIT License

private AutocompleteResponse.Completion translateToCompletion(CompletionProposal proposal) {
    AutocompleteResponse.Completion.Builder builder = AutocompleteResponse.Completion.newBuilder()
            .setKind(AutocompleteResponse.Completion.CompletionKind.valueOf(proposal.getKind()))
            .setIsConstructor(proposal.isConstructor())
            .setCompletionText(String.copyValueOf(proposal.getCompletion())).setFlags(proposal.getFlags())
            .setRelevance(proposal.getRelevance()).setReplaceStart(proposal.getReplaceStart())
            .setReplaceEnd(proposal.getReplaceEnd());

    char[] sig = proposal.getSignature();

    if (sig != null) {
        if (proposal.getKind() == CompletionProposal.METHOD_REF
                || proposal.getKind() == CompletionProposal.JAVADOC_METHOD_REF)
            builder.setSignature(new String(Signature.toCharArray(sig, proposal.getName(), null, false, true)));
        else//  www  .  jav  a 2s  .c  o  m
            builder.setSignature(new String(Signature.toCharArray(sig)));
    }
    char[] name = proposal.getName();
    if (name == null)
        builder.setName(builder.getCompletionText());
    else
        builder.setName(String.copyValueOf(name));
    return builder.build();
}

From source file:com.microsoft.javapkgsrv.JavaParser.java

License:MIT License

private List<ParamHelpResponse.Signature> ParamHelp(ITypeRoot cu, int cursorPosition)
        throws JavaModelException {
    final List<ParamHelpResponse.Signature> proposals = new ArrayList<ParamHelpResponse.Signature>();
    cu.codeComplete(cursorPosition, new CompletionRequestor() {
        @Override//from w  w  w.j  a  v a2 s  .c o m
        public void accept(CompletionProposal proposal) {
            try {
                System.out.println(proposal.toString());
                if (proposal.getKind() == CompletionProposal.METHOD_REF) {
                    char[] javaSig = proposal.getSignature();

                    ParamHelpResponse.Signature.Builder sig = ParamHelpResponse.Signature.newBuilder()
                            .setName(new String(proposal.getName())).setReturnValue(
                                    new String(Signature.toCharArray(Signature.getReturnType(javaSig))));

                    char[][] javaParamTypes = Signature.getParameterTypes(javaSig);
                    for (char[] javaParamType : javaParamTypes) {
                        sig.addParameters(ParamHelpResponse.Parameter.newBuilder()
                                .setName(new String(Signature.toCharArray(javaParamType))).build());
                    }
                    proposals.add(sig.build());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    return proposals;
}

From source file:com.siteview.mde.internal.ui.editor.contentassist.TypePackageCompletionProcessor.java

License:Open Source License

private void generateProposals(String currentContent, IProject project, final Collection c,
        final int startOffset, final int length, final int typeScope) {

    class TypePackageCompletionRequestor extends CompletionRequestor {

        public TypePackageCompletionRequestor() {
            super(true);
            setIgnored(CompletionProposal.PACKAGE_REF, false);
            setIgnored(CompletionProposal.TYPE_REF, false);
        }/*from w  w  w  .  ja  v  a2 s . co m*/

        public void accept(CompletionProposal proposal) {
            if (proposal.getKind() == CompletionProposal.PACKAGE_REF) {
                String pkgName = new String(proposal.getCompletion());
                addProposalToCollection(c, startOffset, length, pkgName, pkgName,
                        MDEPluginImages.get(MDEPluginImages.OBJ_DESC_PACKAGE));
            } else {
                boolean isInterface = Flags.isInterface(proposal.getFlags());
                String completion = new String(proposal.getCompletion());
                if (isInterface && typeScope == IJavaSearchConstants.CLASS
                        || (!isInterface && typeScope == IJavaSearchConstants.INTERFACE)
                        || completion.equals("Dummy2")) //$NON-NLS-1$
                    // don't want Dummy class showing up as option.
                    return;
                int period = completion.lastIndexOf('.');
                String cName = null, pName = null;
                if (period == -1) {
                    cName = completion;
                } else {
                    cName = completion.substring(period + 1);
                    pName = completion.substring(0, period);
                }
                Image image = isInterface ? MDEPluginImages.get(MDEPluginImages.OBJ_DESC_GENERATE_INTERFACE)
                        : MDEPluginImages.get(MDEPluginImages.OBJ_DESC_GENERATE_CLASS);
                addProposalToCollection(c, startOffset, length, cName + " - " + pName, //$NON-NLS-1$
                        completion, image);
            }
        }

    }

    try {
        ICompilationUnit unit = getWorkingCopy(project);
        if (unit == null) {
            generateTypeProposals(currentContent, project, c, startOffset, length, 1);
            return;
        }
        IBuffer buff = unit.getBuffer();
        buff.setContents("class Dummy2 { " + currentContent); //$NON-NLS-1$

        CompletionRequestor req = new TypePackageCompletionRequestor();
        unit.codeComplete(15 + currentContent.length(), req);
        unit.discardWorkingCopy();
    } catch (JavaModelException e) {
    }
}