Search in sources :

Example 1 with TemplateProposal

use of org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal 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)

Aggregations

ArrayList (java.util.ArrayList)1 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 TemplateProposal (org.eclipse.jdt.internal.ui.text.template.contentassist.TemplateProposal)1 IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)1 JavaContentAssistInvocationContext (org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext)1