Search in sources :

Example 1 with JavaContentAssistInvocationContext

use of org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext in project che by eclipse.

the class CompletionJavadocTest method computeProposals.

private static List<ICompletionProposal> computeProposals(ICompilationUnit compilationUnit, int offset) throws JavaModelException {
    IBuffer buffer = compilationUnit.getBuffer();
    IDocument document;
    if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
        document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
    } else {
        document = new DocumentAdapter(buffer);
    }
    TextViewer viewer = new TextViewer(document, new Point(offset, 0));
    JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit);
    List<ICompletionProposal> proposals = new ArrayList<>();
    proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
    //        proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));
    Collections.sort(proposals, new RelevanceSorter());
    return proposals;
}
Also used : JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) Point(org.eclipse.swt.graphics.Point) RelevanceSorter(org.eclipse.jdt.internal.ui.text.java.RelevanceSorter) IBuffer(org.eclipse.jdt.core.IBuffer) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) JavaAllCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with JavaContentAssistInvocationContext

use of org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext in project che by eclipse.

the class AbstractTemplateCompletionProposalComputer method computeCompletionProposals.

/*
	 * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
	 */
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
    if (!(context instanceof JavaContentAssistInvocationContext))
        return Collections.emptyList();
    JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
    ICompilationUnit unit = javaContext.getCompilationUnit();
    if (unit == null)
        return Collections.emptyList();
    fEngine = computeCompletionEngine(javaContext);
    if (fEngine == null)
        return Collections.emptyList();
    fEngine.reset();
    fEngine.complete(javaContext.getViewer(), javaContext.getInvocationOffset(), unit);
    TemplateProposal[] templateProposals = fEngine.getResults();
    List<ICompletionProposal> result = new ArrayList<ICompletionProposal>(Arrays.asList(templateProposals));
    IJavaCompletionProposal[] keyWordResults = javaContext.getKeywordProposals();
    if (keyWordResults.length == 0)
        return result;
    /* Update relevance of template proposals that match with a keyword
		 * give those templates slightly more relevance than the keyword to
		 * sort them first.
		 */
    for (int k = 0; k < templateProposals.length; k++) {
        TemplateProposal curr = templateProposals[k];
        String name = curr.getTemplate().getPattern();
        for (int i = 0; i < keyWordResults.length; i++) {
            String keyword = keyWordResults[i].getDisplayString();
            if (name.startsWith(keyword)) {
                String content = curr.getTemplate().getPattern();
                if (content.startsWith(keyword)) {
                    curr.setRelevance(keyWordResults[i].getRelevance() + 1);
                    break;
                }
            }
        }
    }
    return result;
}
Also used : TemplateProposal(org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)

Example 3 with JavaContentAssistInvocationContext

use of org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext in project che by eclipse.

the class JavaTypeCompletionProposalComputer method computeCompletionProposals.

/*
	 * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposalComputer#computeCompletionProposals(org.eclipse.jface.text.contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) {
    List<ICompletionProposal> types = super.computeCompletionProposals(context, monitor);
    if (!(context instanceof JavaContentAssistInvocationContext))
        return types;
    JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
    CompletionContext coreContext = javaContext.getCoreContext();
    if (coreContext != null && coreContext.getTokenLocation() != CompletionContext.TL_CONSTRUCTOR_START)
        return types;
    try {
        if (types.size() > 0 && context.computeIdentifierPrefix().length() == 0) {
            IType expectedType = javaContext.getExpectedType();
            if (expectedType != null) {
                // empty prefix completion - insert LRU types if known, but prune if they already occur in the core list
                // compute minmimum relevance and already proposed list
                int relevance = Integer.MAX_VALUE;
                Set<String> proposed = new HashSet<String>();
                for (Iterator<ICompletionProposal> it = types.iterator(); it.hasNext(); ) {
                    AbstractJavaCompletionProposal p = (AbstractJavaCompletionProposal) it.next();
                    IJavaElement element = p.getJavaElement();
                    if (element instanceof IType)
                        proposed.add(((IType) element).getFullyQualifiedName());
                    relevance = Math.min(relevance, p.getRelevance());
                }
                // insert history types
                List<String> history = JavaPlugin.getDefault().getContentAssistHistory().getHistory(expectedType.getFullyQualifiedName()).getTypes();
                relevance -= history.size() + 1;
                for (Iterator<String> it = history.iterator(); it.hasNext(); ) {
                    String type = it.next();
                    if (proposed.contains(type))
                        continue;
                    IJavaCompletionProposal proposal = createTypeProposal(relevance, type, javaContext);
                    if (proposal != null)
                        types.add(proposal);
                    relevance++;
                }
            }
        }
    } catch (BadLocationException x) {
        // log & ignore
        JavaPlugin.log(x);
    } catch (JavaModelException x) {
        // log & ignore
        JavaPlugin.log(x);
    }
    return types;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) IType(org.eclipse.jdt.core.IType) CompletionContext(org.eclipse.jdt.core.CompletionContext) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) BadLocationException(org.eclipse.jface.text.BadLocationException) HashSet(java.util.HashSet)

Example 4 with JavaContentAssistInvocationContext

use of org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext in project che by eclipse.

the class CodeAssist method computeProposals.

public Proposals computeProposals(IJavaProject project, String fqn, int offset, final String content) throws JavaModelException {
    WorkingCopyOwner copyOwner = new WorkingCopyOwner() {

        @Override
        public IBuffer createBuffer(ICompilationUnit workingCopy) {
            return new org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter(workingCopy, workingCopy.getPath(), content);
        }
    };
    ICompilationUnit compilationUnit;
    IType type = project.findType(fqn);
    if (type == null) {
        return null;
    }
    if (type.isBinary()) {
        compilationUnit = type.getClassFile().getWorkingCopy(copyOwner, null);
    } else {
        compilationUnit = type.getCompilationUnit().getWorkingCopy(copyOwner, null);
    }
    IBuffer buffer = compilationUnit.getBuffer();
    IDocument document;
    if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
        document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
    } else {
        document = new DocumentAdapter(buffer);
    }
    TextViewer viewer = new TextViewer(document, new Point(offset, 0));
    JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit);
    List<ICompletionProposal> proposals = new ArrayList<>();
    proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
    proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));
    Collections.sort(proposals, new RelevanceSorter());
    return convertProposals(offset, compilationUnit, viewer, proposals);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) TemplateCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.TemplateCompletionProposalComputer) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) Point(org.eclipse.swt.graphics.Point) RelevanceSorter(org.eclipse.jdt.internal.ui.text.java.RelevanceSorter) IBuffer(org.eclipse.jdt.core.IBuffer) IType(org.eclipse.jdt.core.IType) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) JavaAllCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer) IDocument(org.eclipse.jface.text.IDocument)

Example 5 with JavaContentAssistInvocationContext

use of org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext in project che by eclipse.

the class JavaCompletionProposalComputer method computeContextInformation.

/*
     * @see org.eclipse.jface.text.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.jface.text
     * .contentassist.TextContentAssistInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
     */
public List<IContextInformation> computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) {
    if (context instanceof JavaContentAssistInvocationContext) {
        JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
        int contextInformationPosition = guessContextInformationPosition(javaContext);
        List<IContextInformation> result = addContextInformations(javaContext, contextInformationPosition);
        return result;
    }
    return Collections.emptyList();
}
Also used : JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) IContextInformation(org.eclipse.che.jface.text.contentassist.IContextInformation) Point(org.eclipse.swt.graphics.Point)

Aggregations

JavaContentAssistInvocationContext (org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext)7 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)4 ArrayList (java.util.ArrayList)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 Point (org.eclipse.swt.graphics.Point)3 TextViewer (org.eclipse.che.jdt.javaeditor.TextViewer)2 IBuffer (org.eclipse.jdt.core.IBuffer)2 IType (org.eclipse.jdt.core.IType)2 DocumentAdapter (org.eclipse.jdt.internal.core.DocumentAdapter)2 JavaAllCompletionProposalComputer (org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer)2 RelevanceSorter (org.eclipse.jdt.internal.ui.text.java.RelevanceSorter)2 IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 IDocument (org.eclipse.jface.text.IDocument)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 IContextInformation (org.eclipse.che.jface.text.contentassist.IContextInformation)1 CompletionContext (org.eclipse.jdt.core.CompletionContext)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1