* @see org.eclipse.jface.action.IAction#run()
*/
@Override
public void run() {
final ScriptSourceViewer viewer = (ScriptSourceViewer) getSourceViewer();
if (viewer.isEditable()) {
final Point selection = viewer.rememberSelection();
try {
viewer.setRedraw(false);
final String type = TextUtilities.getContentType(viewer.getDocument(),
RutaPartitions.RUTA_PARTITIONING, selection.x, true);
if (type.equals(IDocument.DEFAULT_CONTENT_TYPE) && selection.y == 0) {
try {
final IModelElement element = getElementAt(selection.x, true);
if (element != null && element.exists()) {
final int kind = element.getElementType();
if (kind == IModelElement.TYPE || kind == IModelElement.METHOD) {
final ISourceReference reference = (ISourceReference) element;
final ISourceRange range = reference.getSourceRange();
if (range != null) {
viewer.setSelectedRange(range.getOffset(), range.getLength());
viewer.doOperation(ISourceViewer.FORMAT);
}
}
}
} catch (ModelException exception) {
// Should not happen
}
} else {
viewer.setSelectedRange(selection.x, 1);
viewer.doOperation(ISourceViewer.FORMAT);
}
} catch (BadLocationException exception) {
// Can not happen
} finally {
viewer.setRedraw(true);
viewer.restoreSelection();
}
}
}