在 Eclipse for Java 中的单独一行上包装链式方法调用

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

Wrapping chained method calls on a separate line in Eclipse for Java

eclipseline

提问by Johann

I haven't been successful in figuring out how to wrap each method call in Eclipse. For example, I have this:

我还没有成功弄清楚如何将每个方法调用包装在Eclipse. 例如,我有这个:

someObject.A().B().C();

But I really want this:

但我真的想要这个:

someObject.A()
          .B()
          .C();

Nothing under the "Line Wrapping" section in Eclipseseems to give me this result.

“换行”部分下的任何内容Eclipse似乎都没有给我这个结果。

回答by Christian García

Complementing Deepak Azad's answer, what you exactly need is the following:

补充 Deepak Azad 的回答,您真正需要的是以下内容:

Windows: Window → Preferences → Java → Code Style
→ Formatter → Edit → Line wrapping (tab)

Mac OS: ADT → Preferences → Java → Code Style
→ Formatter → Edit → Line wrapping (tab)

Windows:窗口 → 首选项 → Java → 代码样式
→ 格式化程序 → 编辑 → 换行(选项卡)

Mac OS:ADT → 首选项 → Java → 代码样式
→ 格式化程序 → 编辑 → 换行(选项卡)

Then, in the list at the left, select:

然后,在左侧的列表中,选择:

Function Calls → Qualified invocations

函数调用 → 限定调用

Now below this list, set Line wrapping policyto:

现在在此列表下方,将换行策略设置为:

Wrap all elements, except first element if not necessary

包装所有元素,如果不需要,除了第一个元素

Check:

检查

Force split, even if line shorter than maximum line width

强制拆分,即使线短于最大线宽

Finally, set Indentation policyto (thanks @Turbo):

最后,将缩进策略设置为(感谢@Turbo):

Indent on column

列缩进

It should give you the exact behavior you asked for.

它应该为您提供您要求的确切行为。



BONUS: Android Studio / IntelliJ Idea:

奖励:Android Studio / IntelliJ 理念:

Mac OS: Android Studio → Preferences (Cmd +,) → Editor → Code Style → Java → Wrapping And Bracestab → Chained method calls

Mac OS:Android Studio → Preferences ( Cmd +,) → Editor → Code Style → Java → Wrapping And Bracestab → Chained method calls

select

选择

Wrap always

总是换行

and check

并检查

Align when multiline

多行时对齐

回答by Op De Cirkel

Window → Preferences → Java → Code Style → Formater → Edit → Line wrapping → Never join already wrapped lines

窗口 → 首选项 → Java → 代码样式 → 格式化程序 → 编辑 → 换行 → 永远不要加入已经换行的行

回答by Deepak Azad

Window > Preferences > Java > Code Style > Formater > Edit > Line wrapping > Function Calls, set the 'Line wrapping policy' as 'Wrap all elements, every element on a new line'.

Window > Preferences > Java > Code Style > Formater > Edit > Line wrapping > Function Calls,将'Line wrapping policy'设置为'Wrap all element, every element on a new line'。

回答by Andy Guibert

For those who have eclipse code formatting settings checked into source control (for consistent code formatting across the whole team), the .settingsfolder is checked in.

对于那些将 Eclipse 代码格式设置签入源代码管理(为了在整个团队中保持代码格式一致)的人,将.settings签入该文件夹。

The property that will give you this formatting behavior is in the .settings/org.eclipse.jdt.core.prefsfile.

为您提供这种格式化行为的属性在.settings/org.eclipse.jdt.core.prefs文件中。

The property is:

该物业是:

org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=80

回答by Stim

If you are willing to do the formatting of those segments yourself, you can prevent eclipse from reformatting those segments by using:

如果您愿意自己格式化这些段,则可以使用以下方法防止 Eclipse 重新格式化这些段:

// @formatter:off
...
// @formatter:on

You might need to enable this in your preferences: http://archive.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/eclipse-news-part2.html#JavaFormatter

您可能需要在您的首选项中启用它:http: //archive.eclipse.org/eclipse/downloads/drops/R-3.6-201006080911/eclipse-news-part2.html#JavaFormatter

See also: How to turn off the Eclipse code formatter for certain sections of Java code?

另请参阅: 如何为 Java 代码的某些部分关闭 Eclipse 代码格式化程序?

回答by Shiv Krishna Jaiswal

in: Window->Preferences->Java->Code Style->Formater->Edit->Line wrapping->Function call-> Qualified invocation

在:Window->Preferences->Java->Code Style->Formater->Edit->Line wrapping->Function call->Qualified invocation

select Indentation policy as indent on column

选择缩进策略作为列上的缩进

回答by Jajikanth pydimarla

Customize your eclipse formatter in both java and javascript. increase the max line with that fits your screen. Windows>preferences

在 java 和 javascript 中自定义您的 eclipse 格式化程序。增加适合您屏幕的最大线。窗口>首选项

Edit

编辑