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

时间:2023-01-25 09:14:59

I noticed the eclipse indenter has support for the latest version of java, and it would be nice if I could use that class to indent generated java source code. Is there a way of integrating it ?

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

EDIT: I need to be able to include the code formatter in my code. No external calls.

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

EDIT2: I've managed to get it working. You can read the story here. Thanks VonC !

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

3 个解决方案

#1


You can try running the formatter as a standalone application (also detailed here).

您可以尝试将格式化程序作为独立应用程序运行(此处也详述)。

eclipse -vm <path to virtual machine> -application org.eclipse.jdt.core.JavaCodeFormatter [ OPTIONS ] <files>

Try first to define formatting settings with eclipse IDE in order to achieve the right result, then export those settings, and use that configuration file in the eclipse.exe parameters.
Or see also "Generating a Config File for the Formatter Application"

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

eclipse [...] -config <myExportedSettings> 

In a java program, you can try to directly format by:

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

  • Creating an instance of CodeFormatter
  • 创建CodeFormatter的实例

  • Using the method void format(aString) on this instance to format aString. It will return the formatted string.
  • 在此实例上使用方法void format(aString)来格式化aString。它将返回格式化的字符串。


Thanks to Geo himself and his report in his blog entry, I now know you need to use DefaultCodeFormatter

感谢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();
    }

Again, full details in the blog entry. Thank you Geo for that feedback!

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


Thorbjørn Ravn Andersen mentions in the comments:

ThorbjørnRavnAndersen在评论中提到:

Maven2 Java Formatter Plugin v0.4 describes a maven plugin that allows Maven to invoke the Eclipse formatter.
As of 0.4 it invokes Eclipse 3.5 which does not support Java 8.

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

#2


Actually, there is one problem with VonC's answer: DefaultCodeFormatter is in an 'internal' package, and therefore should not be be used by clients!

实际上,VonC的答案存在一个问题:DefaultCodeFormatter位于“内部”包中,因此不应被客户端使用!

I recently asked the same question here on *, and came up with the answer a little while later.

我最近在*上问了同样的问题,稍后提出了答案。

In short, you need to use ToolFactory, as in

简而言之,您需要使用ToolFactory,如

ToolFactory.createCodeFormatter(null);

#3


I was using the CodeFormatter in an Eclipse-independent project. The default options used by calling ToolFactory.createCodeFormatter(null); could not handle the source code - the result of the format() call being null.

我在一个独立于Eclipse的项目中使用CodeFormatter。调用ToolFactory.createCodeFormatter(null)时使用的默认选项;无法处理源代码 - format()调用的结果为null。

A minimal working options setup is the following:

最小的工作选项设置如下:

Hashtable<String, String> options = new Hashtable<>();
options.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.8");
options.put("org.eclipse.jdt.core.compiler.compliance", "1.8");
options.put("org.eclipse.jdt.core.compiler.source", "1.8");
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);

#1


You can try running the formatter as a standalone application (also detailed here).

您可以尝试将格式化程序作为独立应用程序运行(此处也详述)。

eclipse -vm <path to virtual machine> -application org.eclipse.jdt.core.JavaCodeFormatter [ OPTIONS ] <files>

Try first to define formatting settings with eclipse IDE in order to achieve the right result, then export those settings, and use that configuration file in the eclipse.exe parameters.
Or see also "Generating a Config File for the Formatter Application"

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

eclipse [...] -config <myExportedSettings> 

In a java program, you can try to directly format by:

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

  • Creating an instance of CodeFormatter
  • 创建CodeFormatter的实例

  • Using the method void format(aString) on this instance to format aString. It will return the formatted string.
  • 在此实例上使用方法void format(aString)来格式化aString。它将返回格式化的字符串。


Thanks to Geo himself and his report in his blog entry, I now know you need to use DefaultCodeFormatter

感谢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();
    }

Again, full details in the blog entry. Thank you Geo for that feedback!

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


Thorbjørn Ravn Andersen mentions in the comments:

ThorbjørnRavnAndersen在评论中提到:

Maven2 Java Formatter Plugin v0.4 describes a maven plugin that allows Maven to invoke the Eclipse formatter.
As of 0.4 it invokes Eclipse 3.5 which does not support Java 8.

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

#2


Actually, there is one problem with VonC's answer: DefaultCodeFormatter is in an 'internal' package, and therefore should not be be used by clients!

实际上,VonC的答案存在一个问题:DefaultCodeFormatter位于“内部”包中,因此不应被客户端使用!

I recently asked the same question here on *, and came up with the answer a little while later.

我最近在*上问了同样的问题,稍后提出了答案。

In short, you need to use ToolFactory, as in

简而言之,您需要使用ToolFactory,如

ToolFactory.createCodeFormatter(null);

#3


I was using the CodeFormatter in an Eclipse-independent project. The default options used by calling ToolFactory.createCodeFormatter(null); could not handle the source code - the result of the format() call being null.

我在一个独立于Eclipse的项目中使用CodeFormatter。调用ToolFactory.createCodeFormatter(null)时使用的默认选项;无法处理源代码 - format()调用的结果为null。

A minimal working options setup is the following:

最小的工作选项设置如下:

Hashtable<String, String> options = new Hashtable<>();
options.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.8");
options.put("org.eclipse.jdt.core.compiler.compliance", "1.8");
options.put("org.eclipse.jdt.core.compiler.source", "1.8");
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options);