Search code examples
javaeclipse-jdtformatter

jdt.core CodeFormatter - formatting blocks of comments


I am using org.eclipse.jdt.core.formatter.CodeFormatter class. I have problem with formatting properly comments. Can anyone tell what options from DefaultCodeFormatterConstants should I use to format properly comment blocks?

I have such situation:

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

I'd like to format it to

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

Or even how to format blocks of comments, as in my case those are not formatted at all.


Solution

  • Ok I found it is not related to DefaultCodeFormatterConstants options but to CodeFormatter 'kind' property when calling format function. So if you want to format class with comments as well you should use options like for example this:

    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
            );