eclipse 制表符和空格转换

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

Tabs and spaces conversion

eclipseformatting

提问by Robse

I would like to automatically convert between tabs and spaces for indentation when I commit/update code to/from our repository. I have found the AnyEdit plugin for eclipse, which can convert directories of files. Not bad for a start, but does anybody have more expierience on how to handle this? Or maybe know of an Ant script or something else?

当我向/从我们的存储库提交/更新代码时,我想在制表符和空格之间自动转换以进行缩进。我找到了 eclipse 的 AnyEdit 插件,它可以转换文件目录。一开始还不错,但是有人对如何处理这个问题有更多的经验吗?或者也许知道 Ant 脚本或其他什么?

采纳答案by Sietse

Why not just use the code formatter and/or cleanup function? It has settings that take care of that stuff for you. You can even have it run automatically on save.

为什么不直接使用代码格式化程序和/或清理功能?它具有为您处理这些事情的设置。您甚至可以让它在保存时自动运行。

Edit:As Peter Perhá? points out in the comments, this only answers half the question. I don't have any practical experience, but you could try the Maven Eclipse Format Pluginto format from a Maven build.

编辑:作为彼得佩尔哈?在评论中指出,这只能回答一半的问题。我没有任何实际经验,但您可以尝试使用Maven Eclipse Format Plugin从 Maven 构建中进行格式化。

Unfortunately, that's Maven only, and I know of no light-weight command line formatter. But if you happen to use Maven, you can bind the format goal to the proper phase, and if you set Eclipse to auto-build, it would format on update.

不幸的是,这只是 Maven,我知道没有轻量级的命令行格式化程序。但是如果您碰巧使用 Maven,您可以将格式目标绑定到适当的阶段,如果您将 Eclipse 设置为自动构建,它会在更新时进行格式化。

Depending on the SCM tool (git, svn, etc), you could also create a hook that runs the build (but it might be a bit too heavy-weight for that).

根据 SCM 工具(git、svn 等),您还可以创建一个运行构建的钩子(但它可能有点太重了)。

回答by pwaterz

  1. Make sure you have your editor set to use spaces instead of tabs.
  2. Select all text and hit CMD+ Ion mac or CTRL+ Ion windows.
  1. 确保您的编辑器设置为使用空格而不是制表符
  2. 选择所有文字和命中CMD+ IMac或CTRL+I上的窗口。

回答by rjray

I use the AnyEdit plugin to auto-convert tabs to spaces on the save of a file. I also configure the base text editor (from which pretty much all the others derive) to insert spaces instead of tabs. This sounds redundant, but what it does is ensure that I don't insert any tabs, and any file that I edit that already has tabs will be converted as soon as I save it.

我使用 AnyEdit 插件在保存文件时将制表符自动转换为空格。我还配置了基本文本编辑器(几乎所有其他编辑器都来自该编辑器)以插入空格而不是制表符。这听起来多余,但它的作用是确保我不插入任何选项卡,并且我编辑的任何已经有选项卡的文件将在我保存后立即转换。

Tabs have no place in source code. If someone else looks at the file with their tab-stops set to a different value, they lose most alignment/formatting anyway.

标签在源代码中没有位置。如果其他人在将制表位设置为不同值的情况下查看文件,他们无论如何都会丢失大部分对齐/格式。

(Of course, if you have Makefiles that you edit directly, you'll want to make sure their tabs are retained. But in my projects, if make is used at all the Makefile is derived from a different source, such as a Makefile.PL in Perl.)

(当然,如果您有直接编辑的 Makefile,您需要确保保留它们的选项卡。但在我的项目中,如果完全使用 make,则 Makefile 源自不同的来源,例如 Makefile。 Perl 中的 PL。)

回答by Chris Charabaruk

A bit overkill, and only something to attempt with certain repository products that can handle it, but a hook script to call indent or astylecould do the trick. It'll format everyone's code the same way for every file, depending how you write the hook script, and it'd have to be pre-commit of course.

有点矫枉过正,只能尝试使用某些可以处理它的存储库产品,但是调用 indent 或astyle的钩子脚本可以做到这一点。它将以相同的方式为每个文件格式化每个人的代码,具体取决于您编写钩子脚本的方式,当然必须预先提交。

回答by Andrew

You may lose alignment/formatting by using tabs instead of spaces if and only if the tabs are not at the beginning of the line. Never use tabs insides lines, always use tabs at the front of lines. This allows you to use your editor to adjust to your desired indent level without impacting your co-workers view of the file. Challenge: Find an example where tabs at the front of the line loses alignment.

当且仅当制表符不在行的开头时,您可能会因使用制表符而不是空格而失去对齐/格式。切勿在行内使用制表符,始终在行前使用制表符。这使您可以使用编辑器调整到所需的缩进级别,而不会影响同事对文件的看法。挑战:找出一行前面的制表符失去对齐的示例。

回答by Dave

I use Kedit for just this thing. It also natively converts text files from Macintosh, UNIX and MS-Dos. Since it's an older editor, I use one of it's scripts to handle unicode files. You might also want to look at some of the other smart editors.

我只用 Kedit 做这件事。它还可以本地转换来自 Macintosh、UNIX 和 MS-Dos 的文本文件。由于它是一个较旧的编辑器,我使用它的一个脚本来处理 unicode 文件。您可能还想查看其他一些智能编辑器。

回答by Do Nhu Vy

I use Eclipse for Java EE developer 4.6.0 Neon. I use http://marketplace.eclipse.org/content/anyedit-tools

我使用 Eclipse for Java EE developer 4.6.0 Neon。我使用http://marketplace.eclipse.org/content/anyedit-tools