英文:
How to stop Eclipse from indenting extra spaces in this specific scenario?
问题
当我编写匿名类时,我希望我的匿名类的格式如下:
SaleTodayOnly sale = new SaleTodayOnly() // 第1行
{ // 第2行
一些实现
}
但是当我在第1行输入完后按下 enter
键,Eclipse 会自动将光标定位到第2行的 |
处:
SaleTodayOnly sale = new SaleTodayOnly() // 第1行
| // 第2行
一些实现
并且当我通过回退键回退到开头并输入 {
时,Eclipse 会将这个 {
重新定位到:
SaleTodayOnly sale = new SaleTodayOnly() // 第1行
{ // 第2行
一些实现
我该如何设置自己的缩进偏好(仅针对此特定情况)?
编辑:我已将我的 匿名类
设置为 下一行
。这可能是一个 换行
问题。
编辑2:我放弃了。我将使用 Java 的惯例,将 {
放在与匿名类声明相同的行上...
编辑3:在 Preference
窗口四处查找后,切换效果不大 + 看到如何使用“格式”可以产生正确的输出,但问题仍然存在 - 我同意这可能是一个 bug,我会在有时间时提交报告。
英文:
When I am writing anonymous classes, I want my anonymous class to look like:
SaleTodayOnly sale = new SaleTodayOnly() // line 1
{ // line 2
some implementation
}
But when I hit enter
after line 1, Eclipse will automatically position my cursor at |
on line 2:
SaleTodayOnly sale = new SaleTodayOnly() // line 1
| // line 2
some implementation
And when I backspace my way to the front and write {
, Eclipse will reposition this {
to:
SaleTodayOnly sale = new SaleTodayOnly() // line 1
{ // line 2
some implementation
How can I set my own indentation preferences (for this specific scenario only)?
edit: I have my anonymous class
set to next line
. It's probably a wrapping
issue.
edit2: I give up. I'll just use java conventions of {
on the same line as the anonymous class declaration...
edit3: after hunting around the Preference
window, toggling without much effect + seeing how Format produces the right output whereas the problem described still persists -- I'd agree that this is probably a bug and I will file a report when I have time.
答案1
得分: 0
进入偏好设置。(窗口 -> 首选项,可能是这样的;在 Mac 上,它将位于最左边的菜单选项下('Eclipse'))- 在筛选器中输入 'formatter',以找到条目 Java > Code Style > Formatter
。
你目前看到的行为是非标准的,所以你必须已经定义了一个格式;你选择了这种缩进行为,或者是有人设置了这个作为默认的格式化程序。
编辑这个格式。或者,检查一下你的项目是否有自定义的格式化规则,如果是这样的话,同样的答案适用,但是通过你项目的属性进行,然后在那里更新格式化规则。
你要寻找的具体规则是 Brace positions
,Anonymous class declaration
。你将其设置为 Next line indented
。将它设置为其他值。听起来你想要 Next line
(不缩进)。
英文:
Go into your preferences. (Window -> Preferences, probably; on mac it'll be under the leftmost menu option ('Eclipse')) - in the filter type 'formatter' to find the entry Java > Code Style > Formatter
.
The behaviour you are witnessing is non-standard so you must already have a format defined; you picked this indent behavior, or somebody did who set this as default formatter.
edit this format. Alternatively, check if your project has a custom formatting rule in which case, this same answer applies, but instead go via your project's properties and update the formatting rules there.
The specific rule you are looking for is Brace positions
, Anonymous class declaration
. You have this set to Next line indented
. Set it to something else. It sounds like you want Next line
(not indented).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论