Search in sources :

Example 1 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal 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 2 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal 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 3 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project lombok by rzwitserloot.

the class PatchExtensionMethodCompletionProposal method getJavaCompletionProposals.

public static IJavaCompletionProposal[] getJavaCompletionProposals(IJavaCompletionProposal[] javaCompletionProposals, CompletionProposalCollector completionProposalCollector) {
    List<IJavaCompletionProposal> proposals = new ArrayList<IJavaCompletionProposal>(Arrays.asList(javaCompletionProposals));
    if (canExtendCodeAssist(proposals)) {
        IJavaCompletionProposal firstProposal = proposals.get(0);
        int replacementOffset = getReplacementOffset(firstProposal);
        for (Extension extension : getExtensionMethods(completionProposalCollector)) {
            for (MethodBinding method : extension.extensionMethods) {
                ExtensionMethodCompletionProposal newProposal = new ExtensionMethodCompletionProposal(replacementOffset);
                copyNameLookupAndCompletionEngine(completionProposalCollector, firstProposal, newProposal);
                ASTNode node = getAssistNode(completionProposalCollector);
                newProposal.setMethodBinding(method, node);
                createAndAddJavaCompletionProposal(completionProposalCollector, newProposal, proposals);
            }
        }
    }
    return proposals.toArray(new IJavaCompletionProposal[proposals.size()]);
}
Also used : Extension(lombok.eclipse.agent.PatchExtensionMethod.Extension) ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)

Example 4 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project liferay-ide by liferay.

the class LiferayDependencyQuickFix method _processImportNotFoundProposals.

private List<IJavaCompletionProposal> _processImportNotFoundProposals(IInvocationContext context, IProblemLocation problem) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return null;
    }
    ImportDeclaration importDeclaration = (ImportDeclaration) ASTNodes.getParent(selectedNode, ASTNode.IMPORT_DECLARATION);
    if (importDeclaration == null) {
        return null;
    }
    String importName = importDeclaration.getName().toString();
    List<String> serviceWrapperList;
    List<String> servicesList;
    List<IJavaCompletionProposal> proposals = new ArrayList<>();
    try {
        serviceWrapperList = TargetPlatformUtil.getServiceWrapperList().getServiceList();
        servicesList = TargetPlatformUtil.getServicesList().getServiceList();
        if (serviceWrapperList.contains(importName)) {
            ServiceContainer bundle = TargetPlatformUtil.getServiceWrapperBundle(importName);
            proposals.add(_createDepProposal(context, bundle));
        }
        if (servicesList.contains(importName)) {
            ServiceContainer bundle = TargetPlatformUtil.getServiceBundle(importName);
            proposals.add(_createDepProposal(context, bundle));
        }
        if (TargetPlatformUtil.getThirdPartyBundleList(importName) != null) {
            ServiceContainer bundle = TargetPlatformUtil.getThirdPartyBundleList(importName);
            proposals.add(_createDepProposal(context, bundle));
        }
    } catch (Exception e) {
        ProjectCore.logError("Error processing import not found proposals", e);
    }
    return proposals;
}
Also used : ServiceContainer(com.liferay.ide.project.core.modules.ServiceContainer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ArrayList(java.util.ArrayList) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) CoreException(org.eclipse.core.runtime.CoreException)

Example 5 with IJavaCompletionProposal

use of org.eclipse.jdt.ui.text.java.IJavaCompletionProposal in project liferay-ide by liferay.

the class LiferayDependencyQuickFix method _processUndefinedTypeProposals.

private List<IJavaCompletionProposal> _processUndefinedTypeProposals(IInvocationContext context, IProblemLocation problem) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    String fullyQualifiedName = null;
    if (selectedNode instanceof Name) {
        Name node = (Name) selectedNode;
        fullyQualifiedName = node.getFullyQualifiedName();
    }
    List<String> serviceWrapperList;
    List<String> servicesList;
    boolean depWrapperCanFixed = false;
    List<IJavaCompletionProposal> proposals = new ArrayList<>();
    try {
        serviceWrapperList = TargetPlatformUtil.getServiceWrapperList().getServiceList();
        servicesList = TargetPlatformUtil.getServicesList().getServiceList();
        for (String wrapper : serviceWrapperList) {
            if (wrapper.endsWith(fullyQualifiedName)) {
                ServiceContainer bundle = TargetPlatformUtil.getServiceWrapperBundle(wrapper);
                proposals.add(_createDepProposal(context, bundle));
            }
        }
        if (!depWrapperCanFixed) {
            for (String service : servicesList) {
                if (service.endsWith(fullyQualifiedName)) {
                    ServiceContainer bundle = TargetPlatformUtil.getServiceBundle(service);
                    proposals.add(_createDepProposal(context, bundle));
                }
            }
        }
    } catch (Exception e) {
        ProjectCore.logError("Error processing undefined type proposals", e);
    }
    return proposals;
}
Also used : ServiceContainer(com.liferay.ide.project.core.modules.ServiceContainer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) CoreException(org.eclipse.core.runtime.CoreException) Name(org.eclipse.jdt.core.dom.Name)

Aggregations

IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)22 ArrayList (java.util.ArrayList)9 CompletionContext (org.eclipse.jdt.core.CompletionContext)4 JavaModelException (org.eclipse.jdt.core.JavaModelException)4 IProblemLocation (org.eclipse.jdt.ui.text.java.IProblemLocation)4 IContextInformation (org.eclipse.jface.text.contentassist.IContextInformation)4 Image (org.eclipse.swt.graphics.Image)4 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)3 CoreException (org.eclipse.core.runtime.CoreException)3 IType (org.eclipse.jdt.core.IType)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 IDocument (org.eclipse.jface.text.IDocument)3 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)3 Point (org.eclipse.swt.graphics.Point)3 ServiceContainer (com.liferay.ide.project.core.modules.ServiceContainer)2 List (java.util.List)2 IResource (org.eclipse.core.resources.IResource)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 JavaContentAssistInvocationContext (org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext)2