问题
I'm writing an Eclipse plugin with a wizard (org.eclipse.jface.wizard.Wizard
) which creates a new file with a basic code template. To simplify the "piecing together" of the file contents, I plan to stuff everything into one long string, inject it into the file, and then call my custom Formatter (inherits org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter
) to clean up all the indentation and so on.
Question is, how do I go about calling the formatter programmatically?
In the wizard I call IDE.openEditor()
and get back a handle to an IEditorPart
. What can I do from here?
回答1:
Well, I have found my answer:
IEditorPart editor = IDE.openEditor(page, file, true);
XtextEditor xed = (XtextEditor)editor;
((SourceViewer)xed.getInternalSourceViewer()).doOperation(ISourceViewer.FORMAT);
Maybe that will help someone else looking for something similar!
来源:https://stackoverflow.com/questions/8923969/trigger-eclipses-code-formatter-programmatically-from-a-new-file-wizard