Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect white-space auto-formatting inside Javadoc code tags #3340

Open
yoshi-sys opened this issue Nov 23, 2024 · 3 comments
Open

Incorrect white-space auto-formatting inside Javadoc code tags #3340

yoshi-sys opened this issue Nov 23, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@yoshi-sys
Copy link

yoshi-sys commented Nov 23, 2024

Consider the following code that auto-formats a Javadoc comment, represented as a String:

public static void main(String[] args) throws Exception {
  System.out.println(format("/**<pre><code>a\n b</code></pre>*/"));
  System.out.println(); System.out.println(); System.out.println();
  System.out.println(format("/**\n<pre><code>a\n b</code></pre>*/"));
}

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:

/**
 * <pre>
 * <code>a
b</code>
 * </pre>
 */



/**
 * <pre>
 * <code>a
 b</code>
 * </pre>
 */

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.

@yoshi-sys
Copy link
Author

After some debugging, it looks like part of the issue is with the method TokenManager#getLength(int, int, int), which is called by findCommentLineIndent.

I do not fully understand why this is the case, but when handling the pre tag, for the first case, it calculates that length to be 0 because there is a new line right before the pre. However, for the second case, this length is calculated as 1, which I think, in turns, means that the single space before the b is considered "indent" and left out of the formatted text. In a debug session, if I force this length to be 0, then the 2nd output is printed for both cases.

This is tested with JDK 21.

@jukzi jukzi added the bug Something isn't working label Nov 25, 2024
@jukzi
Copy link
Contributor

jukzi commented Nov 25, 2024

Please rewrite your testcase as Junit Test for JDT and provide a PR.

yoshi-sys pushed a commit to yoshi-sys/eclipse.jdt.core that referenced this issue Nov 29, 2024
yoshi-sys pushed a commit to yoshi-sys/eclipse.jdt.core that referenced this issue Nov 29, 2024
yoshi-sys added a commit to yoshi-sys/eclipse.jdt.core that referenced this issue Nov 29, 2024
@yoshi-sys
Copy link
Author

Hello,
I have added a JUnit test case demonstrating the incorrect behaviour with #3371

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants