eclipse-jdt / eclipse-jdt/eclipse.jdt.core
Incorrect white-space auto-formatting inside Javadoc code tags
- Dominant language
- Java
- Stars
- 237
- Forks
- 195
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 47
Description
Consider the following code that auto-formats a Javadoc comment, represented as a String:
```java
public static void main(String[] args) throws Exception {
System.out.println(format("/**
a\n b*/"));
System.out.println(); System.out.println(); System.out.println();
System.out.println(format("/**\n
a\n b*/"));
}
private static String format(String code) throws Exception {
var options = DefaultCodeFormatterOptions.getEclipseDefaultSettings().getMap();
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_21);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_21);
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_21);
var edit = ToolFactory.createCodeFormatter(options).format(CodeFormatter.K_JAVA_DOC, code, 0, code.length(), 0, "\n");
var document = new Document(code);
edit.apply(document);
return document.get();
}
```
I would expect that the result of the two `format` calls be the same. However, the actual output is:
```java
/**
*
*a
b
*
*/
/**
*
*a
b
*
*/
```
The only difference between the first and the second output is that the first one does not contain a space before `b`, while the second one does.
It seems to me that the second output is the correct one, because I don't think text inside pre/code blocks should be formatted, and there is a space before the `b` in the source text. In any case, I don't see why adding a line break before the `pre` tag should change the output.
This was tested with version 3.39.0 of the Maven artifact `org.eclipse.jdt:org.eclipse.jdt.core`.
Contributor guide
Assessment
This issue has not been assessed yet.