← Back to DAGDA | Support | Source: https://github.com/ktlint/ktlint/pull/3369
Skip to content

Add KDoc comment format rule - #3369

Merged
paul-dingemans merged 12 commits into
ktlint:masterfrom
exijn:feature/kdoc-comment-format-rule
Aug 28, 2026
Merged

paul-dingemans merged 12 commits into
ktlint:masterfrom
exijn:feature/kdoc-comment-format-rule

Conversation

@exijn

@exijn exijn commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds standard:kdoc-delimiter, an experimental rule that checks whether a KDoc comment is delimited consistently.

  • A single-line KDoc comment starts with /** and ends with */ (exactly one space on each side).
  • A multi-line KDoc comment starts with a line containing only /**, ends with a line containing only */, and every other line starts with a leading asterisk indented one space more than the opening /**, followed by a single space and the line content (or by nothing for an otherwise empty line). The line directly after the opening delimiter, and the line directly before the closing delimiter, must not be empty.
  • An empty (or blank) KDoc comment (e.g. /***/, /** */, /**\n */) is not allowed.

The rule implements RuleV2.OfficialCodeStyle and RuleV2.Experimental, so by default it only runs when ktlint_experimental is enabled and ktlint_code_style is ktlint_official (or when enabled explicitly), per review discussion on this PR.

Autocorrect is deliberately conservative and only ever touches whitespace/delimiters, never the content:

  • Misaligned leading asterisks, a misaligned closing */, a missing space after a leading asterisk, trailing whitespace on an otherwise empty comment line, and an empty line directly after the opening or before the closing delimiter are autocorrected, since these are pure whitespace/delimiter fixes.
  • A continuation line missing its leading asterisk entirely is always reported without autocorrect, since inserting one could change the intended visual indentation of that content.
  • Content sharing a line with the opening /** or closing */ (including extra asterisks glued to the opening, e.g. /***) is always reported without autocorrect, since fixing it would mean rewriting/moving actual content.
  • A single-line KDoc's extra closing asterisk (e.g. /** foo**/) is reported without autocorrect: fixing it requires replacing the KDOC_END leaf, which conflicts with IndentationRule when both rules run in the same pass (see KDoc comment format #2982 discussion).

Tests

KdocDelimiterRuleTest covers well-formed single-line/multi-line KDoc (tags, markdown, links, code blocks, nested declarations, tab indentation), empty/blank KDoc, malformed openings/closings, single-line spacing (split start/end diagnostics), leading-asterisk alignment and spacing, missing leading asterisk (never autocorrected), empty lines directly after opening / before closing, running alongside kdoc-wrapping/kdoc/IndentationRule, autocorrect idempotency, a regression test for the IndentationRule conflict, and opt-in behaviour outside ktlint_official.

Verification

  • ./gradlew :ktlint-ruleset-standard:test --tests "io.github.ktlint.core.ruleset.standard.rules.KdocDelimiterRuleTest"
  • ./gradlew :ktlint-ruleset-standard:test (full module)
  • ./gradlew ktlintCheck build (full project, including the project's own self-check with the new rule enabled)

All pass.

Closes #2982

@paul-dingemans paul-dingemans left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution. I can see that you made a good attempt to comply with other Ktlint rules. I like that there are plenty of tests and explanatory comments.

Despite that I do have a bunch of remarks that need to be addressed. Please respond on every remark to let me know that it is fixed, or has become obsolete due to a refactoring by another review remark.

exijn and others added 2 commits August 21, 2026 01:13
Introduce standard:kdoc-comment-format, which checks that KDoc comments
follow a consistent shape: a single-line KDoc starts with `/** ` and
ends with ` */`, while a multi-line KDoc opens and closes on their own
line and has every continuation line start with a leading asterisk,
indented one space more than the opening `/**`.

The rule implements RuleV2.OfficialCodeStyle so it is enabled by
default only for the ktlint_official code style and remains opt-in for
intellij_idea and android_studio, matching the discussion on the issue.

Autocorrect is intentionally conservative: it only touches whitespace
and delimiters, never the actual KDoc content. A continuation line that
is missing its leading asterisk is only autocorrected when the line is
already indented at least as far as required (so the relative
indentation of that content is preserved) and no tabs are involved;
otherwise only a violation is reported. Content sharing a line with the
opening or closing delimiter is always reported without autocorrect,
since repositioning it would require rewriting the KDoc content.

Closes ktlint#2982

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MAtdJuhtNDUW995EkkqoQM
- Rename rule kdoc-comment-format to kdoc-delimiter and mark it
  RuleV2.Experimental (SinceKtlint EXPERIMENTAL instead of STABLE), per
  review discussion on naming and stability.
- Generalize the opening-delimiter check so that any character other
  than a space or newline directly after "/**" is reported, instead of
  special-casing "/***"; this also catches banner-style openings.
- Detect and report empty/blank KDoc comments (`/***/`, `/** */`,
  `/**\n */`, `/**\n *\n */`) with a dedicated message, without
  autocorrect.
- Split the single-line diagnostic into separate start/end delimiter
  messages and emissions instead of one combined message.
- Rework the multi-line traversal to walk leaves via sibling/leaf
  helpers instead of an index-based leaf list, and to handle KDoc
  comments with tags (multiple sibling KDOC_SECTION elements).
- Add checks for an empty line directly after the opening delimiter or
  directly before the closing delimiter, with autocorrect.
- Never autocorrect a missing leading asterisk, since inserting one can
  change the visual indentation of the continuation line.
- Stop autocorrecting a single-line KDoc's extra closing asterisk when
  it would require replacing the KDOC_END leaf, since IndentationRule
  can be tracking a reference to that leaf in the same pass.
- Fix "align with the first asterisk" wording and replace "star" with
  "asterisk" throughout messages and tests.
- Rewrite tests: use TAB test literal, parameterize the non-official
  code style test, add coverage for the new checks, and drop cases the
  rule no longer needs.
- Move the rule's documentation to experimental.md and remove the now
  empty class-level KDoc in EnumWrappingRule.kt that the new rule
  flags.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L4Y6B9GcCC81Ad6oe4JvkM
@exijn
exijn force-pushed the feature/kdoc-comment-format-rule branch from c1e4569 to 1462e0d Compare August 20, 2026 17:03
@exijn

exijn commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review! I've pushed a commit (1462e0d1) addressing all review threads and replied to each individually. Summary:

Rebase: pulled current master (picks up the exception-catching fix in RuleExecutionContext).

Naming / stability

  • Renamed the rule to standard:kdoc-delimiter (KdocDelimiterRule) throughout: class, test class, provider registration, API dump, docs, editorconfig examples.
  • Rule is now RuleV2.Experimental + RuleV2.OfficialCodeStyle and @SinceKtlint("2.0", EXPERIMENTAL). Doc moved from standard.md to experimental.md.

Rule behavior changes

  • Generalized the opening-delimiter check: any character other than a space/newline directly after /** is now reported via the existing "opening should not be followed by other text" (multi-line) or the normal spacing check (single-line), instead of a separate hardcoded /*** check. This also naturally catches banner-style openings like /** * * * *.
  • Added a dedicated "An empty KDoc comment is not allowed" check (no autocorrect) for /***/, /** */, /**\n */, and /**\n *\n */.
  • Split the single-line diagnostic into independent start/end messages+emissions.
  • Added checks for an empty line directly after the opening delimiter / before the closing delimiter (autocorrected by removing the line).
  • A missing leading asterisk is now always reported without autocorrect (dropped the previous "safe indent" special case), since inserting one can change the visual indentation of that content.
  • Fixed the "aligns with the first asterisk" wording, and replaced "star" with "asterisk" everywhere.

AST processing

  • Reworked the multi-line traversal to walk nextLeaf/nextSibling chains instead of indexing into a materialized leaf list. This also fixed a real bug: a KDoc with @tags is represented as multiple sibling KDOC_SECTION elements, which the old index-based code didn't handle correctly.

IndentationRule conflict (root-caused)
IndentationRule.visitKdoc caches a reference to the KDoc's last leaf (KDOC_END) for its own indent bookkeeping. The old single-line "extra closing asterisk" autocorrect replaced that exact leaf, leaving IndentationRule with a stale reference and triggering its "stack should be empty" exception when both rules run in the same pass. Fix: that one case (extra closing asterisk that needs to become content) is now reported without autocorrect, so KDOC_END is never replaced. While chasing this I also found and fixed the same class of bug in my own new "no empty line before/after delimiter" autocorrect (degenerate case: a KDoc whose entire body is one blank line), and a real, now-surfaced empty KDoc in this repo's own EnumWrappingRule.kt that I fixed as a minimal one-line drive-by since it's a direct, deterministic consequence of enabling this rule.

Tests
Rewrote KdocDelimiterRuleTest: uses $TAB from TestLiterals.kt, @Suppress("ktlint:standard:max-line-length") instead of wrapped assertion args, a @ParameterizedTest over CodeStyleValue for the non-ktlint_official case (flattened out of its single-test nested class), plus new coverage for: empty/blank KDoc (single- and multi-line), empty line after opening / before closing, the generalized opening check, running together with kdoc, kdoc-wrapping and IndentationRule, autocorrect idempotency, and a regression test for the IndentationRule conflict above.

Verification

  • ./gradlew :ktlint-ruleset-standard:test --tests "io.github.ktlint.core.ruleset.standard.rules.KdocDelimiterRuleTest" — 39/39 pass
  • ./gradlew :ktlint-ruleset-standard:test — full module, all pass
  • ./gradlew ktlintCheck build — full project, clean (had to fix the EnumWrappingRule.kt empty KDoc and 3 pre-existing "empty line before/after delimiter" occurrences elsewhere in the codebase for this to pass cleanly)

Open for your input (left unresolved)

  • The "extra asterisk in opening" thread and the "aligns with first asterisk" example: I kept a present-but-misaligned leading asterisk autocorrectable even when two consecutive lines share the same deep indentation (your option 1/option 2 example), since ktlint can't reliably distinguish accidental drift from intentional nesting there, and the simple-misalignment case elsewhere in the PR was already fine with you. Happy to make it report-only instead if you'd prefer.
  • For the three empty-KDoc test cases, I report at column 4 (right after /**) rather than the column 3 in your snippet, for consistency with the rest of the rule's positioning. Let me know if you want column 3 specifically.

Environment note: this sandbox only had JDK 25 available, while build-logic currently requires JDK 26 to configure at all (unrelated to this PR — reproduces on a clean master checkout too). I downloaded a local JDK 26 to run the build/tests; CI on this PR currently shows action_required on GitHub Actions (needs a maintainer to approve running workflows for this push).

exijn and others added 10 commits August 21, 2026 07:57
…lass

- documentation/snapshot/docs/rules/experimental.md: "can not" ->
  "will not" in the autocorrect note, and add the missing-leading-
  asterisk and misaligned-closing-delimiter examples requested in
  review.
- KdocDelimiterRuleTest.kt: flatten the single-test "code style is not
  ktlint_official" nested class into a top-level parameterized test,
  per review feedback that a nested class isn't needed for a single
  test.

These were part of the previous commit's intent but were not staged
before it was pushed.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L4Y6B9GcCC81Ad6oe4JvkM
…he intent of the developer can not be determined with certainty.
Rewrite rule to operate (more) on element passed in via `beforeVisitChildNodes` method instead of internally iterating on the KDOC element only.

This refactor revealed several new edge cases which are handled.

Fix offsets to position where change is to be made (when it would be done manually).

Reword some violation messages.
* Do not remove blank line in multiline Kdoc when entire Kdoc is empty
* Reduce consecutive blank lines
* Remove unneeded leading asterisk before closing delimiter on same line
* When opening delimiter is followed by text on same line and containing a KDOC_LPAR, KDOC_RPAR, KDOC_MARKDOWN_LINK
@paul-dingemans

Copy link
Copy Markdown
Member

@hej090224 Thanks for your contribution. Although I rewrote a big deal of the rule itself, your work was the basis for it. Also I have run the new version on my integration test base and discovered even some more edge cases which I all resolved.

@paul-dingemans
paul-dingemans merged commit 2d847ea into ktlint:master Aug 28, 2026
25 checks passed
@paul-dingemans paul-dingemans added this to the 2.0.0 milestone Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KDoc comment format

2 participants