我对Eclipse针对一系列限定调用(即生成器模式样式)的格式规则感到非常沮丧。例如,下面是我对创建新的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 应用我喜欢的格式化风格,或者更接近它而不是上面提到的任何一个?