如何为 Java 代码的某些部分关闭 Eclipse 代码格式化程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1820908/
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
How to turn off the Eclipse code formatter for certain sections of Java code?
提问by Greg Mattes
I've got some Java code with SQL statements written as Java strings (please no OR/M flamewars, the embedded SQL is what it is - not my decision).
我有一些带有 SQL 语句的 Java 代码,这些 SQL 语句写为 Java 字符串(请不要使用 OR/M 火焰战,嵌入式 SQL 就是这样 - 不是我的决定)。
I've broken the SQL statements semantically into several concatenated strings over several lines of code for ease of maintenance. So instead of something like:
为了便于维护,我已将 SQL 语句从语义上分解为几行代码中的几个串联字符串。所以而不是像这样的东西:
String query = "SELECT FOO, BAR, BAZ FROM ABC WHERE BAR > 4";
I have something like:
我有类似的东西:
String query =
"SELECT FOO, BAR, BAZ" +
" FROM ABC " +
" WHERE BAR > 4 ";
This style makes the SQL much easier to read and maintain (IMHO), especially for larger queries. For example, I can put my editor into "overwrite" mode and modify the text in-place fairly easily.
这种风格使 SQL 更易于阅读和维护(恕我直言),尤其是对于较大的查询。例如,我可以将我的编辑器置于“覆盖”模式并相当轻松地就地修改文本。
Note that this issue generalizes beyond the particular example of SQL. Any code that is written with any vertical formatting, particularly tabular constructs, is susceptible to destruction by a pretty printer.
请注意,此问题超出了 SQL 的特定示例。任何以任何垂直格式编写的代码,尤其是表格结构,都容易被漂亮的打印机破坏。
Now, some project members use the Eclipse editor and the semantic formatting is often destroyed when they format an entire source file.
现在,一些项目成员使用 Eclipse 编辑器,当他们格式化整个源文件时,语义格式经常被破坏。
Is there a way to instruct Eclipse to ignore certain lines of source with respect to formatting?
有没有办法指示 Eclipse 忽略与格式有关的某些源代码行?
I'm looking for something like a special comment that toggles the Eclipse formatter. Ideally, such a comment could be configurable to be whatever we choose, and other formatters could be programmed to respect it as well:
我正在寻找诸如切换 Eclipse 格式化程序的特殊注释之类的东西。理想情况下,这样的注释可以配置为我们选择的任何内容,并且其他格式化程序也可以通过编程来尊重它:
// STOP-ECLIPSE-FORMATTING
String query =
"SELECT FOO, BAR, BAZ" +
" FROM ABC " +
" WHERE BAR > 4 ";
// START-ECLIPSE-FORMATTING
Obviously, one "solution" is to have our team members standardize on some external formatter like Jalopyor JIndent, but that's not what this question is about (also, not my decision on this project): I'm specifically looking for a way to avoid the Eclipse formatter on an ad-hoc basis.
显然,一个“解决方案”是让我们的团队成员标准化一些外部格式化程序,如Jalopy或JIndent,但这不是这个问题的内容(也不是我对这个项目的决定):我专门寻找一种方法临时避免使用 Eclipse 格式化程序。
Ideally, a solution will allow me to insert instructions for the Eclipse formatter without requiring team members using Eclipse to do any IDE reconfiguration(other than possibly choosing a formatter agnostic command comment: STOP-ECLIPSE-FORMATTING
→ STOP-FORMATTING
).
理想情况下,一个解决方案将允许我为 Eclipse 格式化程序插入指令,而无需团队成员使用 Eclipse 进行任何 IDE 重新配置(除了可能选择与格式化程序无关的命令注释:STOP-ECLIPSE-FORMATTING
→ STOP-FORMATTING
)。
采纳答案by xpmatteo
Eclipse 3.6 allows you to turn off formatting by placing a special comment, like
Eclipse 3.6 允许您通过放置特殊注释来关闭格式,例如
// @formatter:off
...
// @formatter:on
The on/off features have to be turned "on" in Eclipse preferences: Java > Code Style> Formatter. Click on Edit, Off/On Tags, enable Enable Off/On tags.
必须在 Eclipse 首选项中“打开”开/关功能:Java > Code Style> Formatter. 单击Edit,Off/On Tags,启用Enable Off/On tags。
It's also possible to change the magic strings in the preferences — check out the Eclipse 3.6 docs here.
还可以更改首选项中的魔法字符串 —请在此处查看 Eclipse 3.6 文档。
More Information
更多信息
Java > Code Style> Formatter> Edit> Off/On Tags
Java > Code Style> Formatter> Edit> Off/On Tags
This preference allows you to define one tag to disable and one tag to enable the formatter (see the Off/On Tags tab in your formatter profile):
此首选项允许您定义一个要禁用的标签和一个启用格式化程序的标签(请参阅格式化程序配置文件中的“关闭/打开标签”选项卡):
You also need to enable the flags from Java Formatting
您还需要从以下位置启用标志 Java Formatting
回答by Thomas Jung
This hack works:
这个黑客工作:
String x = "s" + //Formatter Hack
"a" + //
"c" + //
"d";
I would suggest not to use the formatter. Bad code should look bad not artificially good.Good code takes time. You cannot cheat on quality. Formatting is part of source code quality.
我建议不要使用格式化程序。糟糕的代码应该看起来很糟糕,而不是人为的好。好的代码需要时间。你不能欺骗质量。格式化是源代码质量的一部分。
回答by jitter
AFAIK from Eclipse 3.5 M4 on the formatter has an option "Never Join Lines" which preserves user lines breaks. Maybe that does what you want.
格式化程序上来自 Eclipse 3.5 M4 的 AFAIK 有一个选项“从不加入行”,可以保留用户换行符。也许这就是你想要的。
Else there is this ugly hack
否则有这个丑陋的黑客
String query = //
"SELECT FOO, BAR, BAZ" + //
" FROM ABC" + //
" WHERE BAR > 4";
回答by CPerkins
If you put the plus sign on the beginning of the line, it formats differently:
如果将加号放在行的开头,它的格式会有所不同:
String query =
"SELECT FOO, BAR, BAZ"
+ " FROM ABC"
+ " WHERE BAR > 4";
回答by Guus
I'm using fixed width string-parts (padded with whitespace) to avoid having the formatter mess up my SQL string indentation. This gives you mixed results, and won't work where whitespace is not ignored as it is in SQL, but can be helpful.
我使用固定宽度的字符串部分(用空格填充)以避免格式化程序弄乱我的 SQL 字符串缩进。这会给你混合的结果,并且不会像在 SQL 中那样忽略空格,但可能会有所帮助。
final String sql = "SELECT v.value FROM properties p "
+ "JOIN property_values v ON p.property_id = v.property_id "
+ "WHERE p.product_id = ? "
+ "AND v.value IS NOT NULL ";
回答by kmccoy
Alternative method: In Eclipse 3.6, under "Line Wrapping" then "General Settings" there is an option to "Never join already wrapped lines." This means the formatter will wrap long lines but not undo any wrapping you already have.
替代方法:在 Eclipse 3.6 中,在“换行”和“常规设置”下有一个“从不加入已换行的行”选项。这意味着格式化程序将换行长行但不会撤消您已有的任何换行。
回答by ZilWerks
You have to turn on the ability to add the formatter tags. In the menubar go to:
您必须打开添加格式化程序标签的功能。在菜单栏中转到:
Windows → Preferences Java → Code Style → Formatter
Windows → Preferences Java → Code Style → Formatter
Press the Editbutton. Choose the last tab. Notice the On/Off box and enable them with a checkbox.
按下Edit按钮。选择最后一个选项卡。注意 On/Off 框并使用复选框启用它们。
回答by Robin
@xpmatteo has the answer to disabling portions of code, but in addition to this, the default eclipse settings should be set to only format edited lines of code instead of the whole file.
@xpmatteo 有禁用部分代码的答案,但除此之外,默认 eclipse 设置应设置为仅格式化已编辑的代码行而不是整个文件。
Preferences->Java->Editor->Save Actions->Format Source Code->Format Edited Lines
This would have prevented it from happening in the first place since your coworkers are reformatting code they didn't actually change. This is a good practice to prevent mishaps that render diff on your source control useless (when an entire file is reformatted because of minor format setting differences).
这将首先阻止它发生,因为您的同事正在重新格式化他们实际上并未更改的代码。这是一个很好的做法,可以防止使源代码控制上的差异变得无用的事故(当整个文件由于格式设置差异而重新格式化时)。
It would also prevent the reformatting if the on/off tags option was turned off.
如果打开/关闭标签选项被关闭,它也会阻止重新格式化。
回答by Renaud
See this answer on SO.
请参阅SO 上的此答案。
There is another solution that you can use to suppress the formatting of specific block comments. Use /*-
(note the hyphen) at the beginning of the block comment, and the formatting won't be affected if you format the rest of the file.
您可以使用另一种解决方案来抑制特定块注释的格式。/*-
在块注释的开头使用(注意连字符),如果您格式化文件的其余部分,格式将不会受到影响。
/*-
* Here is a block comment with some very special
* formatting that I want indent(1) to ignore.
*
* one
* two
* three
*/
Source: Documentation at Oracle.
来源:Oracle 文档。
回答by ilinca
Instead of turning the formatting off, you can configure it not to join already wrapped lines. Similar to Jitter's response, here's for Eclipse STS:
您可以将其配置为不连接已换行的行,而不是关闭格式。与 Jitter 的响应类似,这里是 Eclipse STS:
Properties → Java Code Style → Formatter → Enable project specific settings OR Configure Workspace Settings → Edit → Line Wrapping (tab) → check "Never join already wrapped lines"
属性 → Java 代码样式 → 格式化程序 → 启用项目特定设置或配置工作区设置 → 编辑 → 换行(选项卡)→ 选中“从不加入已换行的行”
Save, apply.
保存,申请。