以编程方式从新文件向导触发Eclipse的代码格式化程序

Joh*_*eri 2 formatting eclipse-plugin xtext

我正在编写一个带有向导(org.eclipse.jface.wizard.Wizard)的Eclipse插件,该向导创建一个带有基本代码模板的新文件.为了简化文件内容的"拼凑",我计划将所有内容填充到一个长字符串中,将其插入到文件中,然后调用我的自定义Formatter(继承org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter)来清理所有缩进等等.

问题是,如何以编程方式调用格式化程序?在向导中,我调用IDE.openEditor()并获取一个句柄IEditorPart.我能从这里做什么?

Joh*_*eri 5

好吧,我找到了答案:

IEditorPart editor = IDE.openEditor(page, file, true);
XtextEditor xed = (XtextEditor)editor;
((SourceViewer)xed.getInternalSourceViewer()).doOperation(ISourceViewer.FORMAT);
Run Code Online (Sandbox Code Playgroud)

也许这会帮助其他人寻找类似的东西!