Java 如何使用EclipseJDT代码格式化程序在方法和类型注释之后插入新行?
我正在使用EclipseJDTAPI格式化生成的java源文件。 使用哪些选项,我可以强制格式化程序生成如下输出:Java 如何使用EclipseJDT代码格式化程序在方法和类型注释之后插入新行?,java,eclipse,eclipse-jdt,Java,Eclipse,Eclipse Jdt,我正在使用EclipseJDTAPI格式化生成的java源文件。 使用哪些选项,我可以强制格式化程序生成如下输出: @Annotation1 @Annotation2 @Annotation3 @Annotation4 public class TheClass { private static final int PAGE_SIZE = 10; @Annotation5 private Object o1; @Annotation5 pri
@Annotation1
@Annotation2
@Annotation3
@Annotation4
public class TheClass {
private static final int PAGE_SIZE = 10;
@Annotation5
private Object o1;
@Annotation5
private Object o2;
@Annotation6
@Annotation7
public void doSomething(@Annotation8 @Annotation Object dto) {
// some works
}
}
据我所知,DefaultCodeFormatterOptions
具有insert\u new\u line\u after\u annotation
字段,该字段在所有注释(甚至方法参数注释)之后添加新行。但我想在类型方法或类型注释之后添加新行
编辑:
这是格式化程序代码:
public String format(String code)
throws MalformedTreeException, BadLocationException {
Map options = new java.util.HashMap();
options.put(JavaCore.COMPILER_SOURCE, "1.5");
options.put(JavaCore.COMPILER_COMPLIANCE, "1.5");
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.5");
DefaultCodeFormatterOptions cfOptions =
DefaultCodeFormatterOptions.getDefaultSettings();
cfOptions.insert_new_line_after_annotation = false;
cfOptions.comment_insert_new_line_for_parameter = true;
cfOptions.blank_lines_before_method = 1;
cfOptions.number_of_empty_lines_to_preserve= 1;
cfOptions.tab_char = DefaultCodeFormatterOptions.SPACE;
CodeFormatter cf = new DefaultCodeFormatter(cfOptions, options);
TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0,
code.length(), 0, null);
IDocument dc = new Document(code);
te.apply(dc);
return dc.get();
}
对于注释的每个可能上下文,注释后的新行都有单独的字段
public boolean insert_new_line_after_annotation_on_type;
public boolean insert_new_line_after_annotation_on_field;
public boolean insert_new_line_after_annotation_on_method;
public boolean insert_new_line_after_annotation_on_package;
public boolean insert_new_line_after_annotation_on_parameter;
public boolean insert_new_line_after_annotation_on_local_variable;
所以我猜在注释之后插入新的行\u在类型上就是答案。。。我已经检查了IDE本身而不是API的行为是否符合预期
这是2014年2月添加的相对较新的修正。它可以在eclipse 4.4.0版中使用,我正在使用API。不是日食,是的。我已经将依赖关系从org.eclipse.jdt>core>3.3.0-v_771
更改为org.eclipse.jdt>org.eclipse.jdt.core>3.7.1
。现在我可以看到前面提到的字段属性您使用的是什么版本的eclipse?