【问题标题】:How to programmatically call Eclipse format on a specific java file within a plugin handler?如何以编程方式在插件处理程序中的特定 java 文件上调用 Eclipse 格式?
【发布时间】:2022-01-05 20:49:28
【问题描述】:

我正在编写一个 Eclipse 插件,它根据所选 java 文件的内容(右键单击 -> 自定义菜单项)重写 java 文件(可能是多个文件)。

一切都按预期工作,但文件的重写并不能管理内容格式,因此在使用插件后,我必须使用 Eclipse 格式操作手动格式化每个受影响的 java 文件。

我能够以编程方式对选定的 java 文件调用 Eclipse 格式操作,但我还想格式化所有其他文件。

这是我现在所拥有的(应该更改的部分在 for 循环内):

@Override
public Object execute(ExecutionEvent event) throws ExecutionException
{
    var workbench = PlatformUI.getWorkbench();
    var activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
    var selection = activeWorkbenchWindow.getActivePage().getSelection();

    List<String> generatedClasses = null;

    if ((selection instanceof IStructuredSelection structuredSelection)
            && (structuredSelection.getFirstElement() instanceof ICompilationUnit compilationUnit))
    {
        try
        {

            var projectClassLoader = getProjectClassLoader(compilationUnit.getJavaProject());
            generatedClasses = bindingGenerator.generateBindings(compilationUnit, projectClassLoader);
        }
        catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | IOException
                | IllegalClassFormatException | JavaModelException e)
        {
            // TODO pop an error message dialog?
            return null;
        }

        for (String generatedClass : generatedClasses)
            try
            {
                // I can find the IType of each affected java file here if it should help?
                var type = compilationUnit.getJavaProject().findType(generatedClass);

                // This is where the formating should be made. The code below currently format the selected file only.
                var commandId = IJavaEditorActionDefinitionIds.FORMAT;
                var handlerService = workbench.getService(IHandlerService.class);
                try
                {
                    handlerService.executeCommand(commandId, null);
                }
                catch (Exception e1)
                {
                    e1.printStackTrace();
                }
            }
            catch (JavaModelException e)
            {
                e.printStackTrace();
            }

    }

    MessageDialog.openInformation(activeWorkbenchWindow.getShell(), "Generated bindings on files:", String.join(", ", generatedClasses));

    return null;
}

【问题讨论】:

  • 看来您可以调用org.eclipse.jdt.core.ToolFactory.createCodeFormatter 来获取org.eclipse.jdt.core.formatter.CodeFormatter 的实例来进行格式化。
  • @greg-449 谢谢,有了 ToolFactory.createCodeFormatter,我实际上可以将格式化程序传递给我的 bindingGenerator 类,以便在修改代码的同时完成格式化。我喜欢这样,但有一个问题。 (见下一条评论)
  • @greg-449 使用上面的代码,插件实际上是在调用 Eclipse 的“格式化”插件处理程序,这与在编辑菜单中按“格式化”或在选定的文件。它实际上使用了项目的格式化配置。我需要它。使用 ToolFactory.createCodeFormatter 时,我需要指定格式化选项,因此我似乎需要手动将项目的格式化配置复制到我的格式化程序实例中。除非有办法使用项目的格式化配置?
  • 如果options 参数为空,ToolFactory.createCodeFormatter 似乎使用JavaCore.getOptions() 加载当前选项
  • @greg-449 我尝试过使用 null 但它似乎使用默认的 Eclipse 选项而不是项目的选项。我现在正在查看 IJavaProject.getOptions(boolean inheritJavaCoreOptions) 也许我可以将项目的选项传递给 Formatter...

标签: java eclipse eclipse-plugin


【解决方案1】:

感谢 @greg-449 向我指出 ToolFactory.createCodeFormatter。

我只需要使用项目的选项创建一个格式化程序,如下所示:

ToolFactory.createCodeFormatter(compilationUnit.getJavaProject().getOptions(true));

然后在文件源上使用格式化程序。

【讨论】:

    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多