Skip to content

[JEP 467] Add support for MarkdownComments#4899

Merged
jlerbsc merged 2 commits intojavaparser:masterfrom
johannescoetzee:johannes/markdown-rebuild-final
Nov 20, 2025
Merged

[JEP 467] Add support for MarkdownComments#4899
jlerbsc merged 2 commits intojavaparser:masterfrom
johannescoetzee:johannes/markdown-rebuild-final

Conversation

@johannescoetzee
Copy link
Copy Markdown
Collaborator

Originally opened as #4875, but split into #4885 and johannescoetzee#3 (the latter opened and reviewed on my fork to see the diff compared to a local version of #4885)

Original description

This PR will replace #4875.

This PR adds support for Markdown comments as described in JEP 467. I ran into a few issues while implementing this, so the end result is a compromise between the ideal design and what is doable in a reasonable amount of time and without rewriting all of the comment handling (and requiring users to do the same).

MarkdownComment node

As it is, this PR adds the MarkdownComment node, which is distinct from JavadocComment. I considered merging these under a common class, but in my opinion the benefits to doing so is far outweighed by the complexity of the change required. The content field of the MarkdownComment contains the raw text, including the /// and leading spaces after the first line, which is somewhat consistent with how block comments are handled. Keeping this information is necessary for the pretty printer and LPP.

It also contains a method getMarkdownContent which strips the leading whitespace, /// and indents the remaining content consistent with the description provided in JEP 467. I suspect that this will be more useful to users than using the raw content.

Parser changes

I initially tried updating the grammar to parse markdown comments as a single token, but ran into some difficulties doing this. From what I can see it looks like it should be possible, but would require manually manipulating the tokenizer input stream and I wasn't confident enough that this would work out to spend too much time on it. Instead, the MarkdownComment consists of a range of tokens starting with the first SINGLE_LINE_COMMENT, including all leading and trailing whitespace for the body and ending with the last SINGLE_LINE_COMMENT. The handling of all this is done in the CommonTokenAction method defined in java.jj which is added to the GeneratedJavaParserTokenManager.

Pretty printer and LPP

Support in the pretty printer is mostly a copy-paste of block comments are handled with only minor tweaks to account for the lack of /** and */. Support for the LPP is more complicated since the assumption was made that a comment will always consist of a single token. I've updated the code handling adding, removing, and replacing comments to account for the possibility of having multiple tokens corresponding to the comment.

Note on the implementation

I did consider implementing Markdown comment support as a PostProcessing pass as well, but thought it would end up being more complex. The main problem is that all but the last line comment before a method end up as orphan comments, so, to correctly reconstruct the markdown comments, it would necessary to connect these line comments by looking at token ranges and the tokens between them. While separating this logic from the parser would bring some benefit, I think it would be harder to reason about than the approach implemented in this PR where the token stream is being processed directly. It would also only work if tokens are saved (as far as I understand), which is a significant downside.

@codecov
Copy link
Copy Markdown

codecov bot commented Nov 20, 2025

Codecov Report

❌ Patch coverage is 60.35714% with 111 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.368%. Comparing base (7fc897c) to head (f3f0f33).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
.../lexicalpreservation/LexicalPreservingPrinter.java 66.292% 25 Missing and 5 partials ⚠️
.../github/javaparser/printer/PrettyPrintVisitor.java 0.000% 11 Missing ⚠️
...ithub/javaparser/ast/comments/MarkdownComment.java 83.928% 8 Missing and 1 partial ⚠️
...avaparser/GeneratedJavaParserTokenManagerBase.java 59.090% 4 Missing and 5 partials ⚠️
...om/github/javaparser/ast/visitor/CloneVisitor.java 0.000% 6 Missing ⚠️
...va/com/github/javaparser/ast/comments/Comment.java 0.000% 5 Missing ⚠️
...aparser/ast/visitor/GenericListVisitorAdapter.java 0.000% 5 Missing ⚠️
.../javaparser/ast/visitor/GenericVisitorAdapter.java 0.000% 4 Missing ⚠️
...vaparser/printer/lexicalpreservation/NodeText.java 0.000% 4 Missing ⚠️
...github/javaparser/ast/visitor/ModifierVisitor.java 0.000% 3 Missing ⚠️
... and 15 more
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##              master     #4899   +/-   ##
===========================================
  Coverage     58.368%   58.368%           
  Complexity      2534      2534           
===========================================
  Files            681       681           
  Lines          39302     39302           
  Branches        7134      7134           
===========================================
  Hits           22940     22940           
  Misses         13448     13448           
  Partials        2914      2914           
Flag Coverage Δ
AlsoSlowTests 58.368% <60.357%> (ø)
javaparser-core 58.368% <60.357%> (ø)
javaparser-symbol-solver 58.368% <60.357%> (ø)
jdk-10 57.934% <60.573%> (-0.003%) ⬇️
jdk-11 57.936% <60.573%> (+0.002%) ⬆️
jdk-12 57.936% <60.573%> (ø)
jdk-13 57.936% <60.573%> (ø)
jdk-14 58.173% <60.573%> (+0.002%) ⬆️
jdk-15 58.170% <60.573%> (-0.003%) ⬇️
jdk-16 58.147% <60.573%> (+0.002%) ⬆️
jdk-17 58.300% <60.573%> (ø)
jdk-18 58.300% <60.573%> (ø)
jdk-8 57.937% <60.573%> (ø)
jdk-9 57.932% <60.573%> (ø)
macos-latest 58.360% <60.357%> (ø)
ubuntu-latest 58.355% <60.357%> (ø)
windows-latest 58.350% <60.357%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...n/java/com/github/javaparser/StaticJavaParser.java 70.238% <100.000%> (ø)
...parser/ast/comments/TraditionalJavadocComment.java 70.000% <100.000%> (ø)
...hub/javaparser/ast/visitor/VoidVisitorAdapter.java 99.065% <100.000%> (ø)
...thub/javaparser/metamodel/JavaParserMetaModel.java 99.707% <100.000%> (ø)
...javaparser/metamodel/MarkdownCommentMetaModel.java 100.000% <100.000%> (ø)
.../java/com/github/javaparser/JavaParserAdapter.java 97.297% <0.000%> (ø)
...github/javaparser/ast/comments/JavadocComment.java 26.666% <0.000%> (ø)
...parser/ast/visitor/GenericVisitorWithDefaults.java 96.226% <0.000%> (ø)
...vaparser/ast/visitor/NoCommentHashCodeVisitor.java 78.714% <0.000%> (ø)
...arser/ast/visitor/ObjectIdentityEqualsVisitor.java 97.169% <0.000%> (ø)
... and 20 more

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ab71988...f3f0f33. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jlerbsc jlerbsc merged commit abe40d5 into javaparser:master Nov 20, 2025
35 checks passed
@jlerbsc jlerbsc added this to the next release milestone Nov 20, 2025
@jlerbsc jlerbsc added the PR: Added A PR that introduces new behaviour (e.g. functionality, tests) label Nov 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Added A PR that introduces new behaviour (e.g. functionality, tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants