Java Eclipse 中的 AutoIndent 可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1220719/
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
AutoIndent in Eclipse possible?
提问by
I have been wracking my brain trying to figure this out. For the first time I used jEdit the other day and I was pleasantly surprised that it auto indented my code (meaning that I'd put in the following code:
我一直在绞尽脑汁想弄清楚这一点。前几天我第一次使用 jEdit,我惊喜地发现它自动缩进了我的代码(这意味着我输入了以下代码:
int method () {
_ //<-- and it put me here automatically
I've tried to get the same thing working with eclipse but with no success. I got into the code formatter but I don't see how to make that happen.
我试图用 eclipse 做同样的事情,但没有成功。我进入了代码格式化程序,但我不知道如何实现。
Is it possible to do this? Also while I'm here, is there a such thing as a eclipse plugin that will allow you to search the methods and classes of the standard java library?
是否有可能做到这一点?另外,当我在这里时,是否有诸如 eclipse 插件之类的东西可以让您搜索标准 Java 库的方法和类?
Thanks
谢谢
采纳答案by Ben S
My clean eclipse install does this by default.
我的全新 Eclipse 安装默认执行此操作。
Have you changed any options? Make sure the file you are editing has the .java
file extension. The preference options that control the typing automations are under Java -> Editor -> Typing
in the Window -> Preferences
menu.
你有没有改变任何选项?确保您正在编辑的.java
文件具有文件扩展名。控制打字自动化的首选项选项Java -> Editor -> Typing
位于Window -> Preferences
菜单下方。
Also, I find that the auto-indenting, and most of the other auto-complete functions of eclipse do not function well if the file I am editing has errors in it which prevent compilation. Make sure that your curly-braces are matched correctly, this is the main one that I've noticed blocks auto-indent.
此外,我发现如果我正在编辑的文件中存在阻止编译的错误,则 eclipse 的自动缩进和大多数其他自动完成功能无法正常运行。确保您的花括号正确匹配,这是我注意到块自动缩进的主要花括号。
Regarding searching through the standard Java libraries, use the Search -> Java..
menu option, and check the JRE libraries checkbox, then search away. You can also use the Hierarchy view to see how the classes relate. Also, in the Package and Project views you can expand the JRE System Library, and then expand rt.jar
which holds pretty much all the standard Java pacakges.
关于搜索标准 Java 库,请使用Search -> Java..
菜单选项,并选中 JRE 库复选框,然后搜索。您还可以使用 Hierarchy 视图查看类之间的关系。此外,在 Package 和 Project 视图中,您可以展开 JRE System Library,然后展开rt.jar
其中包含几乎所有标准 Java pacakges。
回答by Macarse
回答by Newtopian
Personally all I use for this is the format options Window->preferences under Java->Code Style ->Formatter.
就我个人而言,我使用的所有格式选项都是 Java->Code Style ->Formatter 下的 Window->preferences。
I once took the time to tweek how I like my code to look like when I work and exported the whole thing. After that I just code without too much bother on what it looks like. When I find the code looks messy by pressing the combination ctrl+shift+f and the whole class becomes pretty again, comments and all.
我曾经花时间调整我喜欢我的代码在我工作时的样子并导出整个东西。在那之后,我只是编码,而不会太在意它的样子。当我通过按 ctrl+shift+f 组合键发现代码看起来很乱时,整个班级又变得漂亮了,评论等等。
After a while it pretty much became a reflex...
过了一会儿,它几乎变成了一种反射......
code code code
代码代码代码
ctrl-s, ctrl-b (cause I disable auto build sometimes), ctrl-shift-f
ctrl-s, ctrl-b (因为我有时禁用自动构建), ctrl-shift-f
code some more etc...
代码更多等等...
Once I got used to this I never really cared how it presented the code as i was typing because I knew it would look all pretty as soon as the loop/if/switch/method etc is finished
一旦我习惯了这一点,我就从不关心它在我输入时如何呈现代码,因为我知道只要循环/if/switch/method 等完成,它就会看起来很漂亮
回答by Chad Okere
Eclipse has always done this for me by default.
默认情况下,Eclipse 总是为我做这件事。
One really cool thing about eclipse is that you can search preference pages. Just right click and go to prefrences. Go to the "Window" menu, and click "Prefrences". Then at the top of the tree view there's a text box that says "type filter text". Replace that with "indent" and it should bring up the page where the indent option is.
关于 eclipse 的一件非常酷的事情是您可以搜索首选项页面。只需右键单击并转到首选项。转到“窗口”菜单,然后单击“首选项”。然后在树视图的顶部有一个文本框,上面写着“类型过滤器文本”。用“缩进”替换它,它应该显示缩进选项所在的页面。
Make sure that eclipse recognizes your file as a java file, that you're using the Java distribution, the latest version, etc.
确保 eclipse 将您的文件识别为 java 文件,确保您使用的是 Java 发行版、最新版本等。
回答by Eugene Kerner
Iv been trying to work around the eclipse indenting and other supposed features for years, and it seems that the bottom line is this ... It only works for the programming style of the authors, so to use it you need to modify your style to comply. This would be OK except that the authors of eclipse have some very strange ideas about common shortcut keys. One horrid example is the search features, eg when did Ctrl+K become "Find Next occurrence" and why doesnt F3 or n work? That all being said I use eclipse because if you have the time to wait around while it starts up - or never close it - and you can modify everything youve learned about using an editor - why why why - then it will certainly increase your efficiency. Please note that there is a preference setting for indenting, it can be set for a project, a workspace, or globally, but no matter how you set it eclipse will still chuck tab characters in where you dont want them. In fact its indent crazy, like it wants to indent everything, even if its already indented. Like I said Iv been using it for years and it STILL drives me nuts with its random behavior.
多年来,我一直试图解决 eclipse 缩进和其他假设的功能,似乎底线是这样的......它只适用于作者的编程风格,所以要使用它,你需要修改你的风格遵守。这没什么,只是 eclipse 的作者对常用快捷键有一些非常奇怪的想法。一个可怕的例子是搜索功能,例如 Ctrl+K 什么时候变成“查找下一个事件”,为什么 F3 或 n 不起作用?话虽如此,我使用 eclipse 是因为如果您有时间在它启动时等待 - 或者从不关闭它 - 并且您可以修改关于使用编辑器的所有知识 - 为什么为什么 - 那么它肯定会提高您的效率。请注意,缩进有一个首选项设置,可以为项目、工作区或全局设置,但无论你如何设置它,eclipse 仍然会在你不想要的地方丢弃制表符。事实上它的缩进很疯狂,就像它想要缩进所有东西一样,即使它已经缩进了。就像我说的,我已经使用它多年了,但它的随机行为仍然让我发疯。
回答by HARSH PATHAK
Follow these steps for Eclipse:
对 Eclipse 执行以下步骤:
Select all text: ctrl+A
Correct indentation: ctrl+I
选择所有文本:ctrl+A
正确缩进:ctrl+I