Search in sources :

Example 1 with STPIndenter

use of org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPIndenter in project linuxtools by eclipse.

the class IndentHandler method execute.

@Override
public Object execute(ExecutionEvent event) {
    // the framework
    if (!isEnabled())
        return null;
    ITextEditor editor = (ITextEditor) HandlerUtil.getActiveEditor(event);
    if (editor == null || !editor.isEditable()) {
        return null;
    }
    ITextSelection selection = getSelection(editor);
    final IDocument document = getDocument(editor);
    if (document != null) {
        final int offset = selection.getOffset();
        final int length = selection.getLength();
        final Position end = new Position(offset + length);
        final int firstLine, nLines;
        fCaretOffset = -1;
        try {
            firstLine = document.getLineOfOffset(offset);
            // check for marginal (zero-length) lines
            int minusOne = length == 0 ? 0 : 1;
            nLines = document.getLineOfOffset(offset + length - minusOne) - firstLine + 1;
            document.addPosition(end);
        } catch (BadLocationException e) {
            // will only happen on concurrent modification
            // $NON-NLS-1$
            IDEPlugin.log(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, IStatus.OK, "", e));
            return null;
        }
        Runnable runnable = () -> {
            IRewriteTarget target = editor.getAdapter(IRewriteTarget.class);
            if (target != null) {
                target.beginCompoundChange();
            }
            try {
                STPHeuristicScanner scanner = new STPHeuristicScanner(document);
                STPIndenter indenter = new STPIndenter(document, scanner, getProject(editor));
                final boolean multiLine = nLines > 1;
                boolean hasChanged = false;
                for (int i = 0; i < nLines; i++) {
                    hasChanged |= indentLine(document, firstLine + i, offset, indenter, scanner, multiLine);
                }
                // update caret position: move to new position when
                // indenting just one line
                // keep selection when indenting multiple
                int newOffset, newLength;
                if (!fIsTabAction && multiLine) {
                    newOffset = offset;
                    newLength = end.getOffset() - offset;
                } else {
                    newOffset = fCaretOffset;
                    newLength = 0;
                }
                // but not when we had a single line non-tab invocation
                if (newOffset != -1 && (hasChanged || newOffset != offset || newLength != length))
                    selectAndReveal(editor, newOffset, newLength);
            } catch (BadLocationException e) {
                // will only happen on concurrent modification
                IDEPlugin.log(new Status(IStatus.ERROR, IDEPlugin.PLUGIN_ID, IStatus.OK, "ConcurrentModification in IndentAction", // $NON-NLS-1$
                e));
            } finally {
                document.removePosition(end);
                if (target != null) {
                    target.endCompoundChange();
                }
            }
        };
        if (nLines > 50) {
            Display display = editor.getEditorSite().getWorkbenchWindow().getShell().getDisplay();
            BusyIndicator.showWhile(display, runnable);
        } else {
            runnable.run();
        }
    }
    return null;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) STPIndenter(org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPIndenter) Position(org.eclipse.jface.text.Position) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) STPHeuristicScanner(org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPHeuristicScanner) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) Display(org.eclipse.swt.widgets.Display)

Aggregations

IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 IRewriteTarget (org.eclipse.jface.text.IRewriteTarget)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 Position (org.eclipse.jface.text.Position)1 STPHeuristicScanner (org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPHeuristicScanner)1 STPIndenter (org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPIndenter)1 Display (org.eclipse.swt.widgets.Display)1 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)1