Search in sources :

Example 1 with ErlTemplateCompletionProcessor

use of org.erlide.ui.templates.ErlTemplateCompletionProcessor in project erlide_eclipse by erlang.

the class AbstractErlContentAssistProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer, final int offset) {
    final String id = Integer.toHexString(viewer.hashCode()) + "@" + offset;
    try {
        ErlideEventTracer.getInstance().traceOperationStart("completion", id);
        try {
            final IDocument doc = viewer.getDocument();
            final String before = getBefore(viewer, doc, offset);
            String elementBefore;
            final IErlElement el = getElementAt(offset);
            if (el instanceof ISourceReference) {
                final ISourceRange r = ((ISourceReference) el).getSourceRange();
                final int o = r.getOffset();
                elementBefore = doc.get(o, offset - o);
            } else {
                elementBefore = null;
            }
            if (restarted && offset > 0) {
                final char last = doc.get(offset - 1, 1).charAt(0);
                if (last == ',' || last == '.' || last == ';' || last == ')' || last == '(') {
                    return null;
                }
            }
            if (Objects.equal(oldDoc, doc) && oldBefore != null && before.startsWith(oldBefore) && oldSuggestions == 0) {
                return getNoCompletion(offset);
            }
            oldDoc = doc;
            oldBefore = before;
            final CompletionService completionService = ErlangEngine.getInstance().getCompletionService();
            List<ICompletionProposal> result = Lists.newArrayList();
            if (project != null) {
                final IOtpRpc backend = BackendCore.getBuildBackend(project);
                final List<CompletionData> resultData = completionService.computeCompletions(backend, project, module, elementBefore, offset, before, isInString());
                result = Lists.transform(resultData, new Function<CompletionData, ICompletionProposal>() {

                    @Override
                    public ICompletionProposal apply(final CompletionData data) {
                        return toProposal(data);
                    }
                });
            }
            final ErlTemplateCompletionProcessor t = new ErlTemplateCompletionProcessor(doc, offset - before.length(), before.length());
            result.addAll(Arrays.asList(t.computeCompletionProposals(viewer, offset)));
            oldSuggestions = result.size();
            if (result.isEmpty()) {
                ErlLogger.debug("no results");
                return getNoCompletion(offset);
            }
            // ErlLogger.debug("%d results", result.size());
            return result.toArray(new ICompletionProposal[result.size()]);
        } catch (final Exception e) {
            ErlLogger.warn(e);
            return null;
        }
    } finally {
        ErlideEventTracer.getInstance().traceOperationEnd("completion", id);
    }
}
Also used : ErlTemplateCompletionProcessor(org.erlide.ui.templates.ErlTemplateCompletionProcessor) ErlModelException(org.erlide.engine.model.ErlModelException) BadLocationException(org.eclipse.jface.text.BadLocationException) IOtpRpc(org.erlide.runtime.rpc.IOtpRpc) IErlElement(org.erlide.engine.model.IErlElement) CompletionData(org.erlide.engine.services.codeassist.CompletionData) FunctionCompletionData(org.erlide.engine.services.codeassist.FunctionCompletionData) Function(com.google.common.base.Function) CompletionService(org.erlide.engine.services.codeassist.CompletionService) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ISourceReference(org.erlide.engine.model.erlang.ISourceReference) IDocument(org.eclipse.jface.text.IDocument) ISourceRange(org.erlide.engine.model.erlang.ISourceRange)

Aggregations

Function (com.google.common.base.Function)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 ErlModelException (org.erlide.engine.model.ErlModelException)1 IErlElement (org.erlide.engine.model.IErlElement)1 ISourceRange (org.erlide.engine.model.erlang.ISourceRange)1 ISourceReference (org.erlide.engine.model.erlang.ISourceReference)1 CompletionData (org.erlide.engine.services.codeassist.CompletionData)1 CompletionService (org.erlide.engine.services.codeassist.CompletionService)1 FunctionCompletionData (org.erlide.engine.services.codeassist.FunctionCompletionData)1 IOtpRpc (org.erlide.runtime.rpc.IOtpRpc)1 ErlTemplateCompletionProcessor (org.erlide.ui.templates.ErlTemplateCompletionProcessor)1