【发布时间】:2011-01-07 11:33:50
【问题描述】:
我对一系列合格调用的 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 应用我喜欢的格式样式或比上述任何方法更接近它的格式?
【问题讨论】:
-
+1 好问题。我也喜欢这个,经常使用 Google Protobufs。
标签: java eclipse code-formatting