以编程方式调用组织导入
我正在尝试以编程方式对我正在以编程方式编辑的文件执行“OrganizeImports”。 我的代码如下所示:
final ICommandService cmdService = (ICommandService)PlatformUI.getWorkbench().getService (ICommandService.class);
if (cmdService != null) {
final Command cmd = cmdService.getCommand(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
final ExecutionEvent execEvt = new ExecutionEvent(cmd, Collections.EMPTY_MAP, compileationUnit, null);
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
ResourcesPlugin.getWorkspace().
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
try {
//cmd.executeWithChecks(execEvt);
cmd.execute(execEvt);
} catch (Exception e) {
getLogger().severe("organize imports failed: " + e.getMessage());
}
}
我的问题是 OrganizeImportsAction 在当前选择上执行,该选择与我正在编辑的编译单元不同。我想在编译单元上以编程方式设置选择,但我不知道该怎么做。或者也许还有另一种方式来触发 OrganizeImports。
谢谢, 斯特凡
I'm trying to execute 'OrganizeImports' programmatically on files that I'm editing programmatically.
My code looks like this:
final ICommandService cmdService = (ICommandService)PlatformUI.getWorkbench().getService (ICommandService.class);
if (cmdService != null) {
final Command cmd = cmdService.getCommand(IJavaEditorActionDefinitionIds.ORGANIZE_IMPORTS);
final ExecutionEvent execEvt = new ExecutionEvent(cmd, Collections.EMPTY_MAP, compileationUnit, null);
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
ResourcesPlugin.getWorkspace().
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
try {
//cmd.executeWithChecks(execEvt);
cmd.execute(execEvt);
} catch (Exception e) {
getLogger().severe("organize imports failed: " + e.getMessage());
}
}
My problem is that OrganizeImportsAction executes on the current selection which is not the same as the compilation unit I'm editing. I would like to set the selection programmatically on the compilation unit but I don't know how to do that. Or maybe there is another way to trigger OrganizeImports.
thanks,
stefan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是 此测试
ui.org.eclipse.jdt.ui.tests.quickfix.CleanUpTest
类可以提供一些线索。它基于
enable
ui.org.eclipse.jdt.ui.tests.quickfix 中的方法。 CleanUpTestCase 和org.eclipse.ltk.core.refactoring.PerformChangeOperation
类。您可以看到
PerformChangeOperation
调用了类org.eclipse.ltk.ui.refactoring.RefactoringWizard
。May be this test
ui.org.eclipse.jdt.ui.tests.quickfix.CleanUpTest
class could offer some clue.It is based on the
enable
method inui.org.eclipse.jdt.ui.tests.quickfix.CleanUpTestCase
and on theorg.eclipse.ltk.core.refactoring.PerformChangeOperation
class.You can see
PerformChangeOperation
invoked ink the classorg.eclipse.ltk.ui.refactoring.RefactoringWizard
.