Search in sources :

Example 81 with ITextSelection

use of org.eclipse.jface.text.ITextSelection 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.equals(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 82 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.

the class ErlangAbstractHandler method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final ITextEditor textEditor = (ITextEditor) HandlerUtil.getActiveEditor(event);
    if (!validateEditorInputState(textEditor)) {
        return null;
    }
    final ISelection sel = textEditor.getSelectionProvider().getSelection();
    if (sel == null || sel.isEmpty() || !(sel instanceof ITextSelection)) {
        return null;
    }
    ErlideEventTracer.getInstance().traceOperationStart(this);
    try {
        final IRunnableWithProgress myRunnableWithProgress = monitor0 -> {
            final IProgressMonitor monitor = monitor0 != null ? monitor0 : new NullProgressMonitor();
            try {
                monitor.beginTask("Processing " + textEditor.getEditorInput().getName(), IProgressMonitor.UNKNOWN);
                doAction(sel, textEditor);
            } finally {
                monitor.done();
            }
        };
        try {
            PlatformUI.getWorkbench().getProgressService().busyCursorWhile(myRunnableWithProgress);
        } catch (final InvocationTargetException e) {
        } catch (final InterruptedException e) {
        }
    } finally {
        ErlideEventTracer.getInstance().traceOperationEnd(this);
    }
    return null;
}
Also used : ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) ITextEditorExtension2(org.eclipse.ui.texteditor.ITextEditorExtension2) ITextEditorExtension(org.eclipse.ui.texteditor.ITextEditorExtension) ISourceRange(org.erlide.engine.model.erlang.ISourceRange) IErlModule(org.erlide.engine.model.root.IErlModule) AbstractErlangEditor(org.erlide.ui.editors.erl.AbstractErlangEditor) HandlerUtil(org.eclipse.ui.handlers.HandlerUtil) ErlModelException(org.erlide.engine.model.ErlModelException) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ISourceReference(org.erlide.engine.model.erlang.ISourceReference) PlatformUI(org.eclipse.ui.PlatformUI) ExecutionException(org.eclipse.core.commands.ExecutionException) ITextSelection(org.eclipse.jface.text.ITextSelection) ErlLogger(org.erlide.util.ErlLogger) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IErlElement(org.erlide.engine.model.IErlElement) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) ErlangEditor(org.erlide.ui.editors.erl.ErlangEditor) AbstractHandler(org.eclipse.core.commands.AbstractHandler) ErlideEventTracer(org.erlide.util.event_tracer.ErlideEventTracer) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 83 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.

the class FindAction method run.

/*
     * Method declared on SelectionChangedAction.
     */
@Override
public void run(final ITextSelection selection) {
    try {
        final IErlModule module = fEditor.getModule();
        if (module == null) {
            return;
        }
        final ISelection sel = getSelection();
        final ITextSelection textSel = (ITextSelection) sel;
        final int offset = textSel.getOffset();
        final OpenResult res = ErlangEngine.getInstance().getOpenService().open(module.getScannerName(), offset, ErlangEngine.getInstance().getModelUtilService().getImportsAsList(module), "", ErlangEngine.getInstance().getModel().getPathVars());
        ErlLogger.debug("find " + res);
        final ErlangSearchPattern ref = SearchUtil.getSearchPatternFromOpenResultAndLimitTo(module, offset, res, getLimitTo(), true);
        if (ref != null) {
            SearchUtil.runQuery(ref, getScope(), getScopeDescription(), getShell());
        }
    } catch (final Exception e) {
        handleException(e);
    }
}
Also used : OpenResult(org.erlide.engine.services.search.OpenResult) IErlModule(org.erlide.engine.model.root.IErlModule) ISelection(org.eclipse.jface.viewers.ISelection) ErlangSearchPattern(org.erlide.engine.services.search.ErlangSearchPattern) ITextSelection(org.eclipse.jface.text.ITextSelection) CoreException(org.eclipse.core.runtime.CoreException) ErlModelException(org.erlide.engine.model.ErlModelException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 84 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.

the class ErlideSelection method getSelectedText.

/**
 * @return the Selected text
 */
public String getSelectedText() {
    final ITextSelection txtSel = getTextSelection();
    final int start = txtSel.getOffset();
    final int len = txtSel.getLength();
    try {
        return doc.get(start, len);
    } catch (final BadLocationException e) {
        throw new RuntimeException(e);
    }
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 85 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project erlide_eclipse by erlang.

the class ErlangSearchPage method initSelections.

private void initSelections() throws ErlModelException {
    final ISelection sel = getContainer().getSelection();
    SearchPatternData initData = null;
    if (sel instanceof IStructuredSelection) {
        initData = tryStructuredSelection((IStructuredSelection) sel);
    } else if (sel instanceof ITextSelection) {
        final IEditorPart activePart = getActiveEditor();
        if (activePart instanceof ErlangEditor) {
            initData = tryErlangTextSelection(initData, activePart);
        }
        if (initData == null) {
            initData = trySimpleTextSelection((ITextSelection) sel);
        }
    }
    if (initData == null) {
        initData = getDefaultInitValues();
    }
    fInitialData = initData;
    fPattern.setText(initData.getPattern());
    setSearchFor(initData.getSearchFor());
}
Also used : ISelection(org.eclipse.jface.viewers.ISelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IEditorPart(org.eclipse.ui.IEditorPart) ITextSelection(org.eclipse.jface.text.ITextSelection) AbstractErlangEditor(org.erlide.ui.editors.erl.AbstractErlangEditor) ErlangEditor(org.erlide.ui.editors.erl.ErlangEditor)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)205 ISelection (org.eclipse.jface.viewers.ISelection)65 IDocument (org.eclipse.jface.text.IDocument)52 BadLocationException (org.eclipse.jface.text.BadLocationException)46 IRegion (org.eclipse.jface.text.IRegion)34 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 Region (org.eclipse.jface.text.Region)31 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)17 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 Point (org.eclipse.swt.graphics.Point)13 IResource (org.eclipse.core.resources.IResource)12 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)12 IFile (org.eclipse.core.resources.IFile)11 StyledText (org.eclipse.swt.custom.StyledText)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 Template (org.eclipse.jface.text.templates.Template)10