Trigger Eclipse's code formatter programmatically from a new file wizard

谁说我不能喝 提交于 2019-12-11 01:35:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!