如何让 Eclipse 的自动格式化程序忽略一段代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/352599/
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 can I make eclipse's autoformatter ignore a section of code?
提问by Paul Adamson
i remember there being a way of marking a section of code in eclipse (special comment or annotation?) which made the autoformatter ignore that section. Or I may have drempt this...
我记得有一种方法可以在 Eclipse 中标记一段代码(特殊注释或注释?),这使得自动格式化程序忽略了该部分。或者我可能已经喝了这个......
Used mainly when I have strings which wrap onto several lines and i don't want the autoformatter to rearrange this.
主要用于当我有包含在多行上的字符串并且我不希望自动格式化程序重新排列它时。
回答by stm
Since eclipse 3.5 (or 3.6) this is possible: - Go to project properties -- Java Code Style -- Formatter -- Edit... - choose the tab marked "Off/On Tags", - include the tags in comments in your source code, like
由于 Eclipse 3.5(或 3.6),这是可能的: - 转到项目属性 - Java 代码样式 - 格式化程序 - 编辑... - 选择标记为“关闭/打开标签”的选项卡, - 在您的评论中包含标签源代码,比如
/* @formatter:on */
回答by matt burns
You can wrap the code you don't want auto-formatted between these special comments:
您可以在这些特殊注释之间包装您不想自动格式化的代码:
normal code
/* @formatter:off */
strangely laid out code
/* @formatter:on */
normal code
Here's a basic usage example that makes a json string (slightly) more readable:
这是一个基本用法示例,它使 json 字符串(稍微)更具可读性:
public class SomeTest {
@Test
public void can_deserialize_json() {
/* @formatter:off */
String json = "" +
"{" +
"???\"id\" : 123," +
"???\"address1\" : blah," +
"???\"shippingInfo\" : {" +
"??????\"trackingUrl\" : null," +
"??????\"price\" : 350" +
"???}," +
"???\"errorMessage\" : null" +
"}";
/* @formatter:on */
MyClass.deserializeJson(json);
}
}
回答by Joachim Sauer
I only know the answer for comments:
我只知道评论的答案:
Eclipse is smart enough to only re-format the comments where the generated JavaDoc wouldn't change (i.e. where whitespace doesn't matter):
Eclipse 足够聪明,只在生成的 JavaDoc 不会改变的地方(即空格无关紧要)重新格式化注释:
/**
* foo <i>
* bar </i>
*
* <pre>
* foo
* bar
* </pre>
*/
will be reformatted into
将被重新格式化为
/**
* foo <i> bar </i>
*
* <pre>
* foo
* bar
* </pre>
*/
Note how the content of the <pre>
tags is not reformatted.
请注意<pre>
标签的内容是如何不被重新格式化的。
For long String literals, I'd suggest that they might be an indication that you should be externalizing some of them.
对于长字符串文字,我建议它们可能表明您应该将其中的一些外部化。
回答by Joachim Sauer
I am not sure about the exact feature you're referring to, but you can change the line wrap policy of expressions, which may help you with your strings problem. See:
我不确定您所指的确切功能,但您可以更改表达式的换行策略,这可能会帮助您解决字符串问题。看:
Window->Preferences->Java->Code Style->Formatter
窗口->首选项->Java->代码样式->格式化程序
Click "Edit..." Button
单击“编辑...”按钮
Click "Line Wrapping" Tab
单击“换行”选项卡
In the tree, choose Expressions->Assignments, then change the indent policy at the bottom of the window.
在树中,选择 Expressions->Assignments,然后更改窗口底部的缩进策略。
Of course there are myriad other options inside the formatter, you may find many of those useful as well for other rules, such as where to put your braces, or how to indent control blocks.
当然,格式化程序中还有无数其他选项,您可能会发现其中许多选项对其他规则也很有用,例如将大括号放在哪里,或如何缩进控制块。
回答by guerda
I just found this question because I'm also annoyed by the lack of this feature.
我刚刚发现这个问题,因为我也对缺少此功能感到恼火。
A small search showed this question and the following answer: Stop eclipse from line wrapping?
一个小的搜索显示了这个问题和以下答案: Stop eclipse from line wrapping?
Eclipse 3.5 supports this. (It hit release candidate a a few days ago, so could be worth checking out)
Eclipse 3.5 支持这一点。(几天前它击中了候选版本,所以值得一试)
Good luck with this! :)
祝你好运!:)
回答by arieltools
I stumbled upon this, 'cause I'd love to see section coloring in Eclipse. Imagine you could simply give some background color to sections within your code. Wouldn't it make it more accessible, especially when you're faded at 4 a.m.? :)
我偶然发现了这一点,因为我很想在 Eclipse 中看到部分着色。想象一下,您可以简单地为代码中的部分提供一些背景颜色。它不会使它更易于访问,尤其是当您在凌晨 4 点褪色时?:)
回答by powtac
You can mark the code you want to format and selct format with right mouse click in this section. This is a "whitlist" solution, perhaps it helps...
您可以在此部分中单击鼠标右键标记要格式化的代码并选择格式。这是一个“白名单”解决方案,也许它有帮助......