Search in sources :

Example 1 with CompletionContext

use of org.eclipse.jdt.core.CompletionContext in project che by eclipse.

the class JavaContentAssistInvocationContext method getRHSHistory.

/**
	 * Returns the content assist type history for the expected type.
	 *
	 * @return the content assist type history for the expected type
	 */
private RHSHistory getRHSHistory() {
    if (fRHSHistory == null) {
        CompletionContext context = getCoreContext();
        if (context != null) {
            char[][] expectedTypes = context.getExpectedTypesSignatures();
            if (expectedTypes != null && expectedTypes.length > 0) {
                String expected = SignatureUtil.stripSignatureToFQN(String.valueOf(expectedTypes[0]));
                fRHSHistory = JavaPlugin.getDefault().getContentAssistHistory().getHistory(expected);
            }
        }
        if (fRHSHistory == null)
            fRHSHistory = JavaPlugin.getDefault().getContentAssistHistory().getHistory(null);
    }
    return fRHSHistory;
}
Also used : CompletionContext(org.eclipse.jdt.core.CompletionContext)

Example 2 with CompletionContext

use of org.eclipse.jdt.core.CompletionContext 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 CompletionContext

use of org.eclipse.jdt.core.CompletionContext in project jbosstools-hibernate by jbosstools.

the class FieldPropertyHandler method attributeCompletionProposals.

public ICompletionProposal[] attributeCompletionProposals(IJavaProject project, Node node, String attributeName, String start, int offset) {
    // TODO: should also try to find properties getXXX()
    if (project != null) {
        Node parentNode = node.getParentNode();
        String typename = null;
        typename = this.extractor.getNearestType(project, parentNode);
        if (typename == null) {
            // could not locate type
            return new IJavaCompletionProposal[0];
        }
        HibernateResultCollector rc = null;
        try {
            IType type = project.findType(typename);
            // nothing to look for then
            if (type == null)
                return new ICompletionProposal[0];
            rc = new HibernateResultCollector(project);
            rc.acceptContext(new CompletionContext());
            // TODO: only handle properties ?
            rc.setAccepts(false, false, false, false, true, false);
            // rc.reset(offset, javaProject, null);
            type.codeComplete(start.toCharArray(), -1, start.length(), new char[0][0], new char[0][0], new int[0], false, rc);
        } catch (JavaModelException jme) {
        // TODO: report
        }
        IJavaCompletionProposal[] results = rc.getJavaCompletionProposals();
        CompletionHelper.transpose(start, offset, results);
        return results;
    }
    return new ICompletionProposal[0];
}
Also used : CompletionContext(org.eclipse.jdt.core.CompletionContext) JavaModelException(org.eclipse.jdt.core.JavaModelException) Node(org.w3c.dom.Node) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal) HibernateResultCollector(org.hibernate.eclipse.hqleditor.HibernateResultCollector) IType(org.eclipse.jdt.core.IType)

Example 4 with CompletionContext

use of org.eclipse.jdt.core.CompletionContext in project jbosstools-hibernate by jbosstools.

the class CompletionHelper method completeOnJavaTypes.

public static ICompletionProposal[] completeOnJavaTypes(IJavaProject javaProject, Settings settings, String packageName, String start, int offset) {
    if (javaProject != null) {
        IEvaluationContext context = javaProject.newEvaluationContext();
        if (packageName != null) {
            context.setPackageName(packageName);
        }
        HibernateResultCollector rc = new HibernateResultCollector(javaProject);
        rc.acceptContext(new CompletionContext());
        // rc.reset(offset, javaProject, null);
        rc.setAccepts(settings);
        try {
            // cannot send in my own document as it won't compile as
            // java - so we just send in
            // the smallest snippet possible
            context.codeComplete(start, start.length(), rc);
        } catch (JavaModelException jme) {
            HibernateConsolePlugin.getDefault().logErrorMessage(HibernateConsoleMessages.CompletionHelper_could_not_complete_java_types, jme);
        }
        IJavaCompletionProposal[] results = rc.getJavaCompletionProposals();
        transpose(start, offset, results);
        return results;
    }
    return new ICompletionProposal[0];
}
Also used : CompletionContext(org.eclipse.jdt.core.CompletionContext) JavaModelException(org.eclipse.jdt.core.JavaModelException) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IEvaluationContext(org.eclipse.jdt.core.eval.IEvaluationContext) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)

Example 5 with CompletionContext

use of org.eclipse.jdt.core.CompletionContext in project jbosstools-hibernate by jbosstools.

the class JavaCompletionProcessor method computeCompletionProposals.

public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int position) {
    try {
        setErrorMessage(null);
        if (editor.getConsoleConfiguration() == null) {
            setErrorMessage(HibernateConsoleMessages.JavaCompletionProcessor_no_console_configuration_found);
            return new ICompletionProposal[0];
        }
        // has to do this because of https://bugs.eclipse.org/bugs/show_bug.cgi?id=141518
        String prefix = HibernateConsoleMessages.JavaCompletionProcessor_session_session;
        IJavaCompletionProposal[] results = new IJavaCompletionProposal[0];
        IJavaProject[] projects = null;
        // try {
        projects = ProjectUtils.findJavaProjects(editor.getConsoleConfiguration());
        /*} catch (RuntimeException e){
				String mess = NLS.bind(HibernateConsoleMessages.JavaCompletionProcessor_error_find_project,
						editor.getConsoleConfiguration().getName());
				HibernateConsolePlugin.getDefault().logErrorMessage(mess, e);
			}*/
        for (int i = 0; projects != null && i < projects.length && results.length <= 0; i++) {
            IJavaProject javaProject = projects[i];
            collector = new CompletionProposalCollector(javaProject);
            collector.acceptContext(new CompletionContext());
            try {
                editor.codeComplete(prefix, collector, position, javaProject);
            } catch (JavaModelException x) {
                Shell shell = viewer.getTextWidget().getShell();
                ErrorDialog.openError(shell, HibernateConsoleMessages.JavaCompletionProcessor_error, HibernateConsoleMessages.JavaCompletionProcessor_error_while_performing_code_completion, x.getStatus());
                HibernateConsolePlugin.getDefault().log(x);
            }
            results = collector.getJavaCompletionProposals();
        }
        CompletionHelper.transpose(null, -prefix.length(), results);
        return results;
    } finally {
        if (collector != null) {
            setErrorMessage(collector.getErrorMessage());
            collector = null;
        }
    }
}
Also used : CompletionContext(org.eclipse.jdt.core.CompletionContext) JavaModelException(org.eclipse.jdt.core.JavaModelException) Shell(org.eclipse.swt.widgets.Shell) IJavaProject(org.eclipse.jdt.core.IJavaProject) CompletionProposalCollector(org.eclipse.jdt.ui.text.java.CompletionProposalCollector) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IJavaCompletionProposal(org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)

Aggregations

CompletionContext (org.eclipse.jdt.core.CompletionContext)6 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 IJavaCompletionProposal (org.eclipse.jdt.ui.text.java.IJavaCompletionProposal)4 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)3 IType (org.eclipse.jdt.core.IType)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ICompletionProposal (org.eclipse.che.jface.text.contentassist.ICompletionProposal)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 CompletionProposal (org.eclipse.jdt.core.CompletionProposal)1 CompletionRequestor (org.eclipse.jdt.core.CompletionRequestor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IEvaluationContext (org.eclipse.jdt.core.eval.IEvaluationContext)1 CompletionProposalCollector (org.eclipse.jdt.ui.text.java.CompletionProposalCollector)1 JavaContentAssistInvocationContext (org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Shell (org.eclipse.swt.widgets.Shell)1 HibernateResultCollector (org.hibernate.eclipse.hqleditor.HibernateResultCollector)1