我对一系列合格调用的 Eclipse 格式规则(即 Builder 模式样式)感到非常沮丧。例如,对于创建新的 Apache Commons CLI Options
对象的某些代码,这是我的首选格式:
Options options = new Options()
.addOption(OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
.addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
"print version and exit")
.addOption(OptionBuilder.withLongOpt(OPTION_PROPERTIES)
.hasArg()
.withArgName("FILE")
.withType(File.class)
.withDescription("specify a user properties file")
.create());
即,如果需要,参数将被包装和缩进,并且除第一个之外的所有合格调用,除非必要,如果有多个,则被包装和缩进。如果参数列表包装在合格的调用中,则调用应首先包装。
Eclipse 中的默认格式(“仅在必要时包装”参数和调用)会产生以下困惑:
Options options = new Options().addOption(
OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
.addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
"print version and exit").addOption(
OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(
"FILE").withType(File.class).withDescription(
"specify a user properties file").create());
进入“Java 代码样式 -> 格式化程序 -> 换行”并将换行设置为“换行所有元素,如果不需要,则除第一个元素之外”以进行调用:
Options options = new Options().addOption(
OPTION_HELP_SHORT, OPTION_HELP, false, "print usage information")
.addOption(OPTION_VERSION_SHORT, OPTION_VERSION, false,
"print version and exit")
.addOption(
OptionBuilder.withLongOpt(OPTION_PROPERTIES).hasArg().withArgName(
"FILE").withType(File.class).withDescription(
"specify a user properties file").create());
我不喜欢 OptionBuilder
表达式没有被包装,或者 "FILE"
被包装而没有包装 withArgName
.
将缩进更改为“列上缩进”会产生:
Options options = new Options().addOption(OPTION_HELP_SHORT, OPTION_HELP,
false, "print usage information")
.addOption(OPTION_VERSION_SHORT,
OPTION_VERSION, false,
"print version and exit")
.addOption(
OptionBuilder.withLongOpt(
OPTION_PROPERTIES)
.hasArg()
.withArgName("FILE")
.withType(File.class)
.withDescription(
"specify a user properties file")
.create());
这打破了我更喜欢的界限,但把事情推到了右边太远了。
有什么方法可以说服 Eclipse 应用我喜欢的格式样式或比上述任何方法更接近它的格式?
最佳答案
关闭带有注释的格式,或者插入行注释太乏味了。
最好的方法描述here :
... or you can select "Line Wrapping > Never join already wrapped lines" globally. Then, you can break it manually and the formatter will only format inside lines (or add additional line breaks if necessary).
使用此设置,Eclipse 格式化程序将停止破坏您的构建器语句。
关于java - 构建器模式的 Eclipse 格式化程序设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2105024/