Java 最好的 Eclipse 代码格式化程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3754405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best Eclipse Code Formatters?
提问by Sheehan Alam
The default Eclipse formatter formats my Java code in a really funny way.
默认的 Eclipse 格式化程序以一种非常有趣的方式格式化我的 Java 代码。
For example: hello.show().x().y()
would oddly be formatted as x()
and .y()
, and be placed on a separate line.
例如:hello.show().x().y()
会奇怪地格式化为x()
and .y()
,并放在单独的行上。
Are there any other basic formatters that can do a better job?
有没有其他基本的格式化程序可以做得更好?
Examples and links are very welcome.
非常欢迎示例和链接。
采纳答案by nanda
I always change the formatter in Eclipse to Java ConventionAND change the tab policy to always use space instead of tab or mixing of tab and space. Sometime I change also the line width to be 100 or 120 (80 characters is just too narrow in my monitor).
我总是将 Eclipse 中的格式化程序更改为Java 约定,并将制表符策略更改为始终使用空格而不是制表符或制表符和空格的混合。有时我也会将线宽更改为 100 或 120(80 个字符在我的显示器中太窄了)。
Regarding your specific request: yes you can change this behavior. Go to the Formatter preference and create your own profile. Modify it on tab Line Wrapping and uncheck Prefer wrapping outer expressions (keep nested expression on one line)
.
关于您的具体要求:是的,您可以更改此行为。转到格式化程序首选项并创建您自己的配置文件。在选项卡 Line Wrapping 上修改它并取消选中Prefer wrapping outer expressions (keep nested expression on one line)
。
More information on: https://bugs.eclipse.org/bugs/show_bug.cgi?id=313524
回答by Thorbj?rn Ravn Andersen
Have you considered looking at the preferences for the formatter? You can basically configure it to do anything you want.
您是否考虑过查看格式化程序的首选项?您基本上可以将其配置为做任何您想做的事情。
I would, however, strongly consider the standard Eclipse formatting. It helps a lot to just use the standard, and then adjust your coding style to work well with that.
但是,我会强烈考虑标准的 Eclipse 格式。仅使用标准会很有帮助,然后调整您的编码风格以适应它。
回答by Vineet Reynolds
In addition to the ability to customize the formatter, as indicated by Thorbj?rn, it is also possible to import coding conventions from specific projects, especially the ones from frameworks and libraries that you are working on.
除了Thorbj?rn 指出的自定义格式化程序的能力之外,还可以从特定项目中导入编码约定,尤其是您正在处理的框架和库中的那些。
For instance, the Spring framework coding conventionshave been made available, as is the case with Android. Eventually, you're better off starting with the Sun coding conventions (than a completely hand-crafted one), with a few tweaks done to suit your organization's coding style and readability requirements.
例如,Spring 框架编码约定已经可用,Android就是这种情况。最终,您最好从 Sun 编码约定开始(而不是完全手工制作的约定),并进行一些调整以适应您组织的编码风格和可读性要求。
回答by Anupam Saini
I would highly recommend the google's style guide, it's a collection of coding style guide for commonly used programming languages.
我强烈推荐 谷歌的风格指南,它是常用编程语言的编码风格指南的集合。
The eclipse configurable Java guide can be found here. Installing the guide is quite simple, Save the style xml to a file and import it using the following dialogue box:
可以在此处找到 Eclipse 可配置 Java 指南。安装指南非常简单,将样式 xml 保存到文件并使用以下对话框导入:
Window -> Preferences -> Java -> Code Style -> Formatter
窗口 -> 首选项 -> Java -> 代码样式 -> 格式化程序
Finally select the active profile as GoogleStyle.
最后选择活动配置文件作为GoogleStyle。