如何为 Java 代码的某些部分关闭 Eclipse 代码格式化程序? [英] How to turn off the Eclipse code formatter for certain sections of Java code?

查看:28
本文介绍了如何为 Java 代码的某些部分关闭 Eclipse 代码格式化程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 Java 代码,其中包含用 Java 字符串编写的 SQL 语句(请不要使用 OR/M 火焰战,嵌入式 SQL 就是这样 - 不是我的决定).

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 语句从语义上分解为几行代码中的几个串联字符串.所以,而不是像这样的:

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:

String query = "SELECT FOO, BAR, BAZ FROM ABC WHERE BAR > 4";

我有类似的东西:

String query =
    "SELECT FOO, BAR, BAZ" +
    "  FROM ABC          " +
    " WHERE BAR > 4      ";

这种风格使 SQL 更易于阅读和维护(恕我直言),尤其是对于较大的查询.例如,我可以将我的编辑器置于覆盖"模式并相当轻松地就地修改文本.

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 的特定示例.任何以任何垂直格式编写的代码,尤其是表格结构,都容易被漂亮的打印机破坏.

现在,一些项目成员使用 Eclipse 编辑器,当他们格式化整个源文件时,语义格式经常被破坏.

Now, some project members use the Eclipse editor and the semantic formatting is often destroyed when they format an entire source file.

有没有办法指示 Eclipse 忽略与格式相关的某些源代码行?

我正在寻找诸如切换 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:

// STOP-ECLIPSE-FORMATTING
String query =
    "SELECT FOO, BAR, BAZ" +
    "  FROM ABC          " +
    " WHERE BAR > 4      ";
// START-ECLIPSE-FORMATTING

显然,一个解决方案"是让我们的团队成员标准化一些外部格式化程序,例如 JalopyJIndent,但这不是这个问题的内容(也不是我对这个项目的决定):我正在专门寻找一种方法来临时避免 Eclipse 格式化程序.

Obviously, one "solution" is to have our team members standardize on some external formatter like Jalopy or 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.

理想情况下,一个解决方案将允许我为 Eclipse 格式化程序插入指令而无需团队成员使用 Eclipse 进行任何 IDE 重新配置(除了可能选择与格式化程序无关的命令注释:STOP-ECLIPSE-FORMATTINGSTOP-FORMATTING).

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-FORMATTINGSTOP-FORMATTING).

推荐答案

Eclipse 3.6 允许您通过放置特殊注释来关闭格式,例如

Eclipse 3.6 allows you to turn off formatting by placing a special comment, like

// @formatter:off
...
// @formatter:on

必须在 Eclipse 首选项中打开"开/关功能:Java > 代码样式 > 格式化程序.点击EditOff/On Tags,启用Enable Off/On tags.

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 3.6 文档.

It's also possible to change the magic strings in the preferences — check out the Eclipse 3.6 docs here.

更多信息

Java > 代码风格 > 格式化程序 > 编辑 > 关/开标签

此首选项允许您定义一个要禁用的标签和一个启用格式化程序的标签(请参阅格式化程序配置文件中的关闭/打开标签选项卡):

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):

您还需要从 Java Formatting

这篇关于如何为 Java 代码的某些部分关闭 Eclipse 代码格式化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆