简体   繁体   English

使用自己程序中的Eclipse代码格式化程序

[英]Using the Eclipse code formatter from own program

Recently I wrote some tools that help me generate Java code for what would otherwise be long and tedious tasks. 最近我写了一些工具,帮助我生成Java代码,否则这将是漫长而乏味的任务。 I use freemarker to write up templates. 我使用freemarker来编写模板。 However, all whitespace in the templates is preserved in the output, resulting in quite messy code. 但是,模板中的所有空格都保留在输出中,导致代码非常混乱。 I could remove indentation from my templates to fix this, but that makes my templates rather unmaintainable. 我可以从我的模板中删除缩进来修复此问题,但这会使我的模板无法维护。

Consider this simple example template: 考虑这个简单的示例模板:

public class MyTestClass
{
    <#list properties as p>
        private String ${p.name};
    </#list>
}

My template is nicely formatted, but my code comes out as this: 我的模板格式很好,但我的代码是这样的:

public class MyTestClass
{
        private String prop1;
        private String prop2;
        private String prop3;
}

Indentation is a bit too much, it should be: 缩进有点太多了,它应该是:

public class MyTestClass
{
    private String prop1;
    private String prop2;
    private String prop3;
}

To accomplish this, I have to remove indentation from my template like this: 要做到这一点,我必须从我的模板中删除缩进,如下所示:

public class MyTestClass
{
    <#list properties as p>
    private String ${p.name};
    </#list>
}

For this simple case it is not really a problem to remove indentation from my template, but you could imagine complex templates to become quite unreadable. 对于这个简单的情况,从模板中删除缩进并不是一个真正的问题,但是您可以想象复杂的模板变得非常难以理解。

On the one side I really want my code to be nicely formatted, but on the other side I'd like my templates to be nicely formatted as well. 一方面我真的希望我的代码格式很好,但另一方面我希望我的模板格式也很好。 I use Eclipse as IDE, with is built-in formatter fully customized to my (and my team's) wishes. 我使用Eclipse作为IDE,内置格式化器完全根据我(和我的团队)的愿望进行定制。 It would be great if I could somehow generate code from freemarker templates and as a post processing step format its output with Eclipse's formatter. 如果我能以某种方式从freemarker模板生成代码并作为后处理步骤格式化其输出与Eclipse的格式化器,那将是很好的。

I can of course run the formatter manually after generating my code, but I really want to automate this process. 我当然可以在生成代码后手动运行格式化程序,但我真的想自动执行此过程。

So, long story short, does anyone know how I can use Eclipse's code formatter within my own Java code? 所以,长话短说,有谁知道如何在我自己的Java代码中使用Eclipse的代码格式化程序?

If you want to use the Eclipse formatter from your own java code I suggest you take a look at the maven java formatter plugin . 如果你想从你自己的java代码中使用Eclipse格式化程序,我建议你看一下maven java formatter插件

It's a maven plugin that can be used to format source code based on Eclipse code formatting settings files. 它是一个maven插件,可用于根据Eclipse代码格式设置文件格式化源代码。

If you don't want to use maven but want to embed the formatting code in your own code, take a look at the FormatterMojo . 如果您不想使用maven但想在自己的代码中嵌入格式代码,请查看FormatterMojo It contains the code that launches the Eclipse Code Formatter (using the Eclipse libraries) 它包含启动Eclipse Code Formatter的代码(使用Eclipse库)

It's all free and open-source. 这都是免费和开源的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM