如何让 Eclipse 为 Java 内容辅助插入选项卡而不是空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2636589/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:20:10  来源:igfitidea点击:

How can I get Eclipse to insert tabs instead of spaces for Java content assist?

eclipsecontent-assist

提问by Simon Nickerson

Is there any way to persuade Eclipse to use tabs instead of spaces for indenting its built-in Java content assist proposals (such as when creating an empty method which overrides a method in the parent class)?

有什么方法可以说服 Eclipse 使用制表符而不是空格来缩进其内置的 Java 内容辅助建议(例如在创建覆盖父类中方法的空方法时)?

The only setting I could find that looked relevant is in Windows/Preferences/General/Editors/Text Editors/Insert spaces for tabs, and this setting is unchecked.

我能找到的唯一看起来相关的设置是在 Windows/Preferences/General/Editors/Text Editors/Insert space for tabs 中,并且未选中此设置。

(EDIT: Several posters have pointed out Java / Code Style / Formatter / Indentation / Tab policy, which is set to "Tabs only")

(编辑:几张海报指出了 Java/代码样式/格式化程序/缩进/制表符策略,设置为“仅制表符”)

At the moment I have to remember to select them and re-format with <Ctrl>+<Shift>+<F>.

目前我必须记住选择它们并使用<Ctrl>+<Shift>+<F>.

采纳答案by VonC

It seems to be currently a bug (not fixed in the 3.5 train):

目前似乎是一个错误(在 3.5 火车中未修复):

bug 44755(for comments, but also valid for other refactoring JDT operations)

错误 44755(用于评论,但也适用于其他重构 JDT 操作)

Also linked to bug 231832, involving a bug in the AST rewrite...

还链接到错误 231832,涉及 AST 重写中的错误...

I believe this is a problem with ASTRewrite, not with the client of ASTRewrite. We have to make sure that code formatter settings are available for the rewrite part.
Moving to JDT/Corefor investigation.

我相信这是 的问题ASTRewrite,而不是ASTRewrite. 我们必须确保代码格式化程序设置可用于重写部分。
移至JDT/Core调查。

Detected in late 3.4, still not fixed in 3.5

在 3.4 后期检测到,在 3.5 中仍未修复

回答by Jacob

I found the solution, thanks to Simon Nickerson's comment above, which is what the original poster was referring to in his edit. After having found the setting, I see what they are talking about. However, the way to it was given a few steps from the beginning of the path, so it fails to give a clear answer to people who are new to Eclipse or have been away from it for a few years, like me. so I'll give the full path here:

我找到了解决方案,多亏了 Simon Nickerson 上面的评论,这就是原始海报在他的编辑中所指的内容。找到设置后,我明白了他们在说什么。然而,它的方法从路径的开始就给出了几步,因此它无法给像我这样的Eclipse新手或已经离开它几年的人给出明确的答案。所以我会在这里给出完整的路径:

  1. Go to Project->Properties.
  2. In the resulting box, go to "Java Code Style"->Formatter.
  3. Click the "Configure Workspace Settings" link in the upper right of the box.
  4. Click the Edit button next to the name.
  5. Change "Tab policy:" drop down box to "Spaces only" because tabs are evil and inevitably result in poorly formatted code. >:-( This is also where you can actually affect the indentation size and tab size in your files.
  6. You will need to change the "Profile name" at the top of the box to save your changes.
  7. Thankfully, once you hit "OK", Eclipse will use your profile for your current project, so you're done after this step.
  1. 转到项目-> 属性。
  2. 在结果框中,转到“Java 代码样式”->“格式化程序”。
  3. 单击框右上角的“配置工作区设置”链接。
  4. 单击名称旁边的编辑按钮。
  5. 将“标签策略:”下拉框更改为“仅空格”,因为标签是邪恶的,不可避免地会导致代码格式错误。>:-( 这也是您可以实际影响文件中的缩进大小和制表符大小的地方。
  6. 您需要更改框顶部的“配置文件名称”以保存更改。
  7. 幸运的是,一旦您点击“确定”,Eclipse 将使用您当前项目的配置文件,因此您在这一步之后就完成了。

Now, what's really broken is the fact that these settings override the more up-front interface for changing similar-sounding settings in "Windows/Preferences/General/Editors/Text Editors/Insert spaces for tabs". To someone relatively new to the editor, this would seem like the place to set the default tabs = spaces setting. After finding the real answer, it's clear that this is just for general text editing, and language-specific settings need to be set up in the profiles for the languages at hand. This should be made a bit clearer for noobs, though. ;-)

现在,真正坏的是这些设置覆盖了更前面的界面,用于更改“Windows/Preferences/General/Editors/Text Editors/Insert space for tabs”中的类似声音设置。对于编辑器相对较新的人来说,这似乎是设置默认制表符 = 空格设置的地方。找到真正的答案后,很明显这仅适用于一般的文本编辑,并且需要在手头语言的配置文件中设置特定于语言的设置。不过,对于新手来说,这应该更清楚一些。;-)

回答by Stroboskop

Have a look in the preferences at Java / Code Style / Formatter
Create your own profile and set "Indentation" / "Tab policy" to "Tabs only"

查看 Java / Code Style / Formatter 中的首选项
创建您自己的配置文件并将“缩进”/“选项卡策略”设置为“仅选项卡”

回答by Chris Dolan

Preferences > Java > Code Style > Formatter > Edit > Indentation > Use tabs only for leading indentation checkbox

Preferences > Java > Code Style > Formatter > Edit > Indentation > Use tabs only for leading indentation 复选框

回答by rep_movsd

The way I have it setup is to enable the "Format source code" in the Editor->Save Actions preferences.

我设置它的方法是在 Editor->Save Actions 首选项中启用“格式源代码”。

That way it reformats the source code on save.

这样它在保存时重新格式化源代码。

回答by user437068

I also had this problem. The issue turned out to be some configuration that was buried somewhere in my workspace. I created a new workspace and reimported my project and my tabs were not converted to spaces any more.

我也有这个问题。事实证明,问题是一些配置隐藏在我的工作区中的某个地方。我创建了一个新工作区并重新导入了我的项目,并且我的选项卡不再转换为空格。

回答by meddlingwithfire

I made sure that Edit>Smart Insert Mode was NOT checked, and that seemed to help me when configuration the indentation and formatting settings did not work. Running Eclipse 3.7.0.

我确保未选中“编辑”>“智能插入模式”,当配置缩进和格式设置不起作用时,这似乎对我有所帮助。运行 Eclipse 3.7.0。

回答by jfcole

Even after setting Insert spaces for tabs option in Text Editor preferences AND setting my Formatter profile Tab policy to Spaces only Eclipse was still inserting tab characters. Only after removing all the tabs from the file did it properly insert spaces for tabs. After discovering this, I determined the Insert spaces for tabs option in Text Editor preferences doesn't seem to have any effect, and it's the change to the Formatter profile Tab policy that achieves the desired results.

即使在文本编辑器首选项中设置了为制表符插入空格选项并将我的格式化程序配置文件制表符策略设置为空格后,只有 Eclipse 仍在插入制表符。只有在从文件中删除所有制表符后,它才能正确地为制表符插入空格。发现这一点后,我确定“文本编辑器”首选项中的“为选项卡插入空格”选项似乎没有任何效果,正是对格式化程序配置文件选项卡策略的更改才能达到预期的效果。

回答by Jonathan Gawrych

Yet another solution that "worked for me" even if the other solutions didn't work (Version: 3.8.1):

即使其他解决方案不起作用,另一个“对我有用”的解决方案(版本:3.8.1):

Preferences > Java > Code Style > Formatter > Edit > Indentation > Use spaces to indent wrapped lines - Set unchecked

首选项 > Java > 代码样式 > 格式化程序 > 编辑 > 缩进 > 使用空格缩进换行 - 设置未选中

This is essentially the "smart tabs" option. It works for the formatter, but it incorrectly sets code generation into using spaces only. Annoyingly, the solution breaks the formatter.

这本质上是“智能标签”选项。它适用于格式化程序,但它错误地将代码生成设置为仅使用空格。令人讨厌的是,该解决方案破坏了格式化程序。