简体   繁体   English

如何修改 Eclipse 代码格式?

[英]How do I modify Eclipse code formatting?

When I reformat code with Eclipse, it turns method headers like this:当我用 Eclipse 重新格式化代码时,它会变成这样的方法头:

public void myMethod(String param) {

into method headers like this:进入这样的方法标题:

public void myMethod(
                     String param) {

When I was brought on here I'd never used Eclipse before, and I imported project settings provided by someone else.当我被带到这里时,我以前从未使用过 Eclipse,而且我导入了其他人提供的项目设置。 I have seen that on small new projects I've worked on Eclipse does not do this, so it must be in the settings I've imported.我已经看到我在 Eclipse 上工作的小型新项目没有这样做,所以它必须在我导入的设置中。 But I've gone through every panel I can find, as well as every hidden file I can find in the workspace, and I can't figure out what is causing this.但是我已经浏览了我能找到的每个面板,以及我能在工作区中找到的每个隐藏文件,但我无法弄清楚是什么导致了这种情况。

How do I turn it off?我该如何关闭它? I don't want a newline before parameters in my method signatures, and I can't imagine why anyone would!我不希望我的方法签名中的参数前有换行符,我无法想象为什么有人会!

Have a look at Window > Preferences > Java > Code Style > Formatter .看看Window > Preferences > Java > Code Style > Formatter

There you can configure almost everything.在那里您几乎可以配置所有内容。 Your case is found at您的案例位于

Line Wrapping > Method Declarations > Parameters .换行>方法声明>参数

In my version of Eclipse, I found the option under Window -> Preferences -> Java -> Code Style -> Formatter , then clicked the Edit.. button.在我的 Eclipse 版本中,我找到了Window -> Preferences -> Java -> Code Style -> Formatter下的选项,然后单击Edit..按钮。

In the new window, go to the Line Wrapping tab and find Method Declarations .在新窗口中,转到Line Wrapping选项卡并找到Method Declarations You want to change the Line wrapping policy .您想更改换行策略 The Force split check box seems to do the same. Force split复选框似乎也做同样的事情。

Go into preferences: Java -> Code Style -> Formatter进入首选项: Java -> Code Style -> Formatter

Restore Defaults or edit what's there.恢复默认值或编辑那里的内容。

Additionally, Code Style -> Code Templates will allow you to define generate code formatting.此外,代码样式->代码模板将允许您定义生成代码格式。

Note that this is for Eclipse Workspace as a whole, the same Preferences can be accessed under the project preferences if you want to get more fine grained at a per project level.请注意,这是针对 Eclipse Workspace 整体而言的,如果您想在每个项目级别获得更精细的粒度,可以在项目首选项下访问相同的首选项

In eclipse you can define your code formatting use Code Style ,在 Eclipse 中,您可以使用Code Style定义代码格式,

for MAC System :--> Eclipse --> Preference -- > java --> code Style对于 MAC 系统:--> Eclipse -->首选项--> java -->代码样式

for Window System :--> Window -> Preferences -> Java -> Code Style -> Formatter对于窗口系统:--> Window -> Preferences -> Java -> Code Style -> Formatter

The best way to change your format is to go to Window-> Preferences-> Java-> Code Style-> Formatter.更改格式的最佳方法是转到 Window-> Preferences-> Java-> Code Style-> Formatter。 Inside the formatter window, eclipse has a couple of built in styles, but the best way is create a new profile of your own by pressing on New.在格式化程序窗口中,eclipse 有几个内置样式,但最好的方法是通过按 New 创建您自己的新配置文件。 You can change the format as you please on the edit tab您可以在编辑选项卡上随意更改格式

如图所示

You can change parentheses and curly braces positioning along with other stuff.您可以更改括号和花括号的位置以及其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM