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

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

问题描述

我有一些Java代码,SQL语句写成Java字符串(请勿使用OR / M flamewars,嵌入式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容易阅读和维护(IMHO),特别是对于较大的查询。例如,我可以将我的编辑器放在覆盖模式中,并且很容易地修改文本。

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

显然,一个解决方案是让我们团队成员对某些外部格式化程序进行标准化,如 Jalopy JIndent ,但这不是这个问题(也不是我对这个项目的决定):我正在寻找一种方法来避免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-FORMATTING STOP-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 > 代码样式 > Formatter 。点击编辑关/开标签,启用启用关/开标签

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
>
代码样式
>
Formatter
>
编辑
>
关闭/开启标签

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

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格式化

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

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