jdt.core CodeFormatter - 格式化注释块

jdt.core CodeFormatter - formatting blocks of comments

我正在使用 org.eclipse.jdt.core.formatter.CodeFormatter class。我在正确格式化评论方面遇到问题。 谁能告诉我应该使用 DefaultCodeFormatterConstants 中的哪些选项来正确格式化评论块?

我有这样的情况:

/**
Project: some-project
@author: author
@since: 2019-01-15
*/

我想将其格式化为

/**
 * Project: some-project
 * @author: author
 * @since: 2019-01-15
 */

甚至如何格式化评论块,在我的例子中,它们根本没有格式化。

好的,我发现它与 DefaultCodeFormatterConstants 选项无关,但在调用格式函数时与 CodeFormatter 'kind' 属性 相关。 所以如果你想格式化 class 和注释,你应该使用像这样的选项:

CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS
final TextEdit edit = codeFormatter.format(
                CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, // format a compilation unit
                contentToFormat, // source to format
                0, // starting position
                contentToFormat.length(), // length
                0, // initial indentation
                System.getProperty("line.separator") // line separator
        );