use of org.eclipse.linuxtools.internal.systemtap.ui.ide.editors.stp.STPHeuristicScanner 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;
}
Aggregations