public class IndentHandler extends ErlangAbstractHandler {
@Override
protected void doAction(final ISelection sel, final ITextEditor textEditor) {
final IDocument document = textEditor.getDocumentProvider().getDocument(
textEditor.getEditorInput());
final ITextSelection selection = extendSelectionToWholeLines(document,
(ITextSelection) sel);
final ITextSelection getSelection = getTextSelection(document, selection,
textEditor);
String text;
OtpErlangObject r1 = null;
try {
text = document.get(getSelection.getOffset(), getSelection.getLength());
// call erlang, with selection within text
r1 = callErlang(selection.getOffset() - getSelection.getOffset(),
selection.getLength(), text);
} catch (final Exception e) {
ErlLogger.error(e);
}
final String newText = Util.stringValue(r1);
if (newText == null) {
final String msg = "call to " + getClass().getSimpleName()
+ " timed out; try a smaller selection.";
final Status status = new Status(IStatus.ERROR, ErlangCore.PLUGIN_ID,
ErlangStatus.INTERNAL_ERROR.getValue(), msg, null);
ErlLogger.error("INTERNAL ERROR: " + msg);
ErrorDialog.openError(textEditor.getSite().getShell(),
ActionMessages.IndentAction_error_message, "Internal error", status);
return;
}
final Display display = textEditor.getEditorSite().getShell().getDisplay();
display.syncExec(new Runnable() {
@Override
public void run() {
final IRewriteTarget target = (IRewriteTarget) textEditor
.getAdapter(IRewriteTarget.class);
if (target != null) {
target.beginCompoundChange();
target.setRedraw(false);
}
try {
if (!document.get(selection.getOffset(), selection.getLength())
.equals(newText)) {
document.replace(selection.getOffset(), selection.getLength(),
newText);
}
} catch (final BadLocationException e) {
ErlLogger.warn(e);
}