如何从我的代码中使用eclipse压头?

Geo*_*Geo 3 java eclipse

我注意到eclipse indenter支持最新版本的java,如果我可以使用该类来缩进生成的java源代码,那将会很好.有没有一种方法来整合它?

编辑:我需要能够在我的代码中包含代码格式化程序.没有外部电话.

编辑2:我设法让它工作.你可以在这里阅读故事.谢谢VonC!

Von*_*onC 5

您可以尝试将格式化程序作为独立应用程序运行(此处也会详细介绍).

eclipse -vm <path to virtual machine> -application org.eclipse.jdt.core.JavaCodeFormatter [ OPTIONS ] <files>
Run Code Online (Sandbox Code Playgroud)

首先尝试使用eclipse IDE定义格式设置以获得正确的结果,然后导出这些设置,并在eclipse.exe参数中使用该配置文件.
或者另请参阅"为Formatter应用程序生成配置文件"

eclipse [...] -config <myExportedSettings> 
Run Code Online (Sandbox Code Playgroud)

在java程序中,您可以尝试直接格式化:

  • 创建一个实例 CodeFormatter
  • format(aString)在此实例上使用void方法来格式化aString.它将返回格式化的字符串.

感谢Geo本人和他在博客文章中的报告,我现在知道你需要使用它DefaultCodeFormatter

    String code = "public class geo{public static void main(String[] args){System.out.println(\"geo\");}}";
    CodeFormatter cf = new DefaultCodeFormatter();

    TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0,code.length(),0,null);
    IDocument dc = new Document(code);
    try {
        te.apply(dc);
        System.out.println(dc.get());
    } catch (MalformedTreeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

同样,博客条目中的完整详细信息.谢谢Geo的反馈!


ThorbjørnRavnAndersen 在评论中提到:

Maven2 Java Formatter Plugin v0.4描述了一个允许Maven调用Eclipse格式化程序的maven插件.
从0.4开始,它调用Eclipse 3.5,它不支持Java 8.