Search in sources :

Example 6 with ISourceReference

use of org.erlide.engine.model.erlang.ISourceReference in project erlide_eclipse by erlang.

the class ErlangAbstractHandler method getTextSelection.

/**
 * Provide the text selection that is needed to execute the command. Default
 * implementation, extend to Erlang elements selected.
 *
 * @param document
 *            text {@link IDocument}
 * @param selection
 *            selection affected by command (extended by extendSelection)
 * @return new {@link ITextSelection} with all text up to selection
 */
protected ITextSelection getTextSelection(final IDocument document, final ITextSelection selection, final ITextEditor editor) {
    if (editor instanceof ErlangEditor) {
        final AbstractErlangEditor erlangEditor = (AbstractErlangEditor) editor;
        final IErlModule module = erlangEditor.getModule();
        if (module != null) {
            final int offset1 = selection.getOffset();
            final int offset2 = offset1 + selection.getLength();
            try {
                final IErlElement e1 = module.getElementAt(offset1);
                final IErlElement e2 = module.getElementAt(offset2);
                if (e1 instanceof ISourceReference) {
                    final ISourceReference ref1 = (ISourceReference) e1;
                    final ISourceRange r1 = ref1.getSourceRange();
                    final int offset = r1.getOffset();
                    int length = r1.getLength();
                    if (e1 == e2) {
                        final int docLength = document.getLength();
                        if (offset + length > docLength) {
                            length = docLength - offset;
                        }
                        return ErlangAbstractHandler.extendSelectionToWholeLines(document, new TextSelection(document, offset, length));
                    } else if (e2 == null) {
                        return ErlangAbstractHandler.extendSelectionToWholeLines(document, new TextSelection(document, offset, selection.getLength() + selection.getOffset() - offset));
                    } else if (e2 instanceof ISourceReference) {
                        final ISourceReference ref2 = (ISourceReference) e2;
                        final ISourceRange r2 = ref2.getSourceRange();
                        return ErlangAbstractHandler.extendSelectionToWholeLines(document, new TextSelection(document, offset, r2.getOffset() - offset + r2.getLength()));
                    }
                }
            } catch (final ErlModelException e) {
            }
        }
    }
    return ErlangAbstractHandler.extendSelectionToWholeLines(document, selection);
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) ErlModelException(org.erlide.engine.model.ErlModelException) ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) IErlModule(org.erlide.engine.model.root.IErlModule) ISourceReference(org.erlide.engine.model.erlang.ISourceReference) AbstractErlangEditor(org.erlide.ui.editors.erl.AbstractErlangEditor) ErlangEditor(org.erlide.ui.editors.erl.ErlangEditor) AbstractErlangEditor(org.erlide.ui.editors.erl.AbstractErlangEditor) ISourceRange(org.erlide.engine.model.erlang.ISourceRange)

Example 7 with ISourceReference

use of org.erlide.engine.model.erlang.ISourceReference in project erlide_eclipse by erlang.

the class ErlangEditor method doSelectionChanged.

/**
 * Called from org.erlide.ui.editors.erl.outline.ErlangOutlinePage.createControl
 * (...).new OpenAndLinkWithEditorHelper() {...}.linkToEditor(ISelection)
 *
 * @param selection
 */
public void doSelectionChanged(final ISelection selection) {
    ISourceReference reference = null;
    if (selection instanceof IStructuredSelection) {
        final IStructuredSelection ss = (IStructuredSelection) selection;
        for (final Object o : ss.toArray()) {
            if (o instanceof ISourceReference) {
                reference = (ISourceReference) o;
                break;
            }
        }
    }
    if (!isActivePart() && ErlideUIPlugin.getActivePage() != null) {
        ErlideUIPlugin.getActivePage().bringToTop(this);
    }
    setSelection(reference, true);
}
Also used : IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ISourceReference(org.erlide.engine.model.erlang.ISourceReference)

Example 8 with ISourceReference

use of org.erlide.engine.model.erlang.ISourceReference 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)

Example 9 with ISourceReference

use of org.erlide.engine.model.erlang.ISourceReference in project erlide_eclipse by erlang.

the class AbstractErlContentAssistProcessor method getBefore.

String getBefore(final ITextViewer viewer, final IDocument doc, final int offset) {
    try {
        if (module != null) {
            try {
                final IErlElement element = module.getElementAt(offset);
                if (element instanceof ISourceReference) {
                    final ISourceReference sr = (ISourceReference) element;
                    final int start = sr.getSourceRange().getOffset();
                    if (start <= offset) {
                        return doc.get(start, offset - start);
                    }
                }
            } catch (final ErlModelException e) {
            }
        }
        for (int n = offset - 1; n >= 0; --n) {
            final char c = doc.getChar(n);
            final int type = Character.getType(c);
            if (type == Character.LINE_SEPARATOR || type == Character.PARAGRAPH_SEPARATOR || type == Character.CONTROL) {
                return doc.get(n + 1, offset - n - 1);
            }
        }
        return doc.get(0, offset);
    } catch (final BadLocationException e) {
        ErlLogger.warn(e);
    }
    return "";
}
Also used : IErlElement(org.erlide.engine.model.IErlElement) ErlModelException(org.erlide.engine.model.ErlModelException) ISourceReference(org.erlide.engine.model.erlang.ISourceReference) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 10 with ISourceReference

use of org.erlide.engine.model.erlang.ISourceReference in project erlide_eclipse by erlang.

the class OpenUtils method findOpenResult.

public Object findOpenResult(final ITextEditor editor, final IErlModule module, final IErlProject project, final OpenResult openResult, final IErlElement element) throws CoreException, ErlModelException, OtpErlangRangeException, RpcException, BadLocationException {
    final IErlElementLocator.Scope scope = NavigationPreferencePage.getCheckAllProjects() ? IErlElementLocator.Scope.ALL_PROJECTS : IErlElementLocator.Scope.REFERENCED_PROJECTS;
    final IErlElementLocator model = ErlangEngine.getInstance().getModel();
    Object found = null;
    if (openResult.isExternalCall()) {
        found = findExternalCallOrType(module, openResult, project, element, scope);
    } else if (openResult.isInclude()) {
        found = modelFindService.findInclude(model, project, module, openResult.getName(), openResult.getPath());
    } else if (openResult.isLocalCall()) {
        found = findLocalCall(module, project, openResult, element, scope);
    } else if (openResult.isVariable() && element instanceof ISourceReference) {
        final ISourceReference sref = (ISourceReference) element;
        final ISourceRange range = sref.getSourceRange();
        final String elementText = editor.getDocumentProvider().getDocument(editor.getEditorInput()).get(range.getOffset(), range.getLength());
        found = modelFindService.findVariable(range, openResult.getName(), elementText);
    } else if (openResult.isRecord() || openResult.isMacro()) {
        final ErlElementKind kind = openResult.isMacro() ? ErlElementKind.MACRO_DEF : ErlElementKind.RECORD_DEF;
        found = modelFindService.findPreprocessorDef(module, openResult.getName(), kind);
    } else if (openResult.isField()) {
        final IErlRecordDef def = (IErlRecordDef) modelFindService.findPreprocessorDef(module, openResult.getFun(), ErlElementKind.RECORD_DEF);
        if (def != null) {
            found = def.getFieldNamed(openResult.getName());
        }
    }
    return found;
}
Also used : ErlElementKind(org.erlide.engine.model.ErlElementKind) IErlRecordDef(org.erlide.engine.model.erlang.IErlRecordDef) IErlElementLocator(org.erlide.engine.model.root.IErlElementLocator) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) ISourceReference(org.erlide.engine.model.erlang.ISourceReference) ISourceRange(org.erlide.engine.model.erlang.ISourceRange)

Aggregations

ISourceReference (org.erlide.engine.model.erlang.ISourceReference)10 ISourceRange (org.erlide.engine.model.erlang.ISourceRange)5 IErlElement (org.erlide.engine.model.IErlElement)4 ErlModelException (org.erlide.engine.model.ErlModelException)3 IErlModule (org.erlide.engine.model.root.IErlModule)3 BadLocationException (org.eclipse.jface.text.BadLocationException)2 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)1 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)1 Function (com.google.common.base.Function)1 IDocument (org.eclipse.jface.text.IDocument)1 IRegion (org.eclipse.jface.text.IRegion)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)1 Region (org.eclipse.jface.text.Region)1 TextSelection (org.eclipse.jface.text.TextSelection)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 StyledText (org.eclipse.swt.custom.StyledText)1 Point (org.eclipse.swt.graphics.Point)1