/* (non-Javadoc) * Method declared on SelectionDispatchAction. */ @Override public void run(ITextSelection selection) { IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput()); if (doc != null && unit != null) { DefaultGroovyFormatter formatter = new DefaultGroovyFormatter( selection, doc, new FormatterPreferences(unit), kind == FormatKind.INDENT_ONLY); TextEdit edit = formatter.format(); try { unit.applyTextEdit(edit, new NullProgressMonitor()); } catch (MalformedTreeException e) { GroovyCore.logException("Exception when formatting", e); } catch (JavaModelException e) { GroovyCore.logException("Exception when formatting", e); } } }
@Override public ICleanUpFix createFix(CleanUpContext context) throws CoreException { ICompilationUnit unit = context.getCompilationUnit(); if (!(unit instanceof GroovyCompilationUnit)) { return null; } GroovyCompilationUnit gunit = (GroovyCompilationUnit) unit; char[] contents = gunit.getContents(); ITextSelection sel = new TextSelection(0, contents.length); IDocument doc = new Document(new String(contents)); boolean isIndentOnly = kind == FormatKind.INDENT_ONLY; IFormatterPreferences preferences = new FormatterPreferences(gunit); DefaultGroovyFormatter formatter = new DefaultGroovyFormatter(sel, doc, preferences, isIndentOnly); TextEdit edit = formatter.format(); return new TextEditFix(edit, gunit, "Format groovy source code."); }