Skip to content

chore(comfort): Add semantic line-break formatter for doc comments#663

Open
JeanMertz wants to merge 9 commits into
mainfrom
prr230
Open

chore(comfort): Add semantic line-break formatter for doc comments#663
JeanMertz wants to merge 9 commits into
mainfrom
prr230

Conversation

@JeanMertz
Copy link
Copy Markdown
Collaborator

@JeanMertz JeanMertz commented May 20, 2026

Introduce comfort, a new contrib tool that reformats Rust doc-comment blocks (/// and //!) using semantic line breaks — one sentence per line — with an optional --max-width safety net.

The tool ships as two binaries that share a single entry-point: comfort (invoked directly) and cargo-comfort (a cargo subcommand). Direct invocation without paths defaults to stdin/stdout; cargo comfort defaults to walking the current workspace.

The pipeline runs in three layers:

  • extract: tokenises Rust source via ra-ap-rustc_lexer, finding maximal runs of consecutive /// / //! lines and collecting their indentation, style, and body text.
  • format: parses each block's body with comrak, identifies top-level paragraph spans, and hands their text to the reflow step. All other markdown structure — reference link definitions, block quotes, lists, code blocks, headings — is preserved verbatim.
  • sentence: splits paragraphs into individual sentences using UAX#29 (unicode-segmentation), with abbreviation-aware merging and inline-token protection (URLs, code spans, markdown links) via regex placeholders, then width-wraps each sentence with textwrap.

A fmt-comments-ci just task is added to the CI pipeline:

comfort --check --workspace

It exits non-zero and prints a unified diff for any file that would be reformatted, integrating with the existing CI matrix.

Introduce `comfort`, a new contrib tool that reformats Rust doc-comment
blocks (`///` and `//!`) using semantic line breaks — one sentence per
line — with an optional `--max-width` safety net.

The tool ships as two binaries that share a single entry-point:
`comfort` (invoked directly) and `cargo-comfort` (a cargo subcommand).
Direct invocation without paths defaults to stdin/stdout; `cargo
comfort` defaults to walking the current workspace.

The pipeline runs in three layers:

- **extract**: tokenises Rust source via `ra-ap-rustc_lexer`, finding
  maximal runs of consecutive `///` / `//!` lines and collecting their
  indentation, style, and body text.
- **format**: parses each block's body with `comrak`, identifies
  top-level paragraph spans, and hands their text to the reflow step.
  All other markdown structure — reference link definitions, block
  quotes, lists, code blocks, headings — is preserved verbatim.
- **sentence**: splits paragraphs into individual sentences using UAX
  #29 (`unicode-segmentation`), with abbreviation-aware merging and
  inline-token protection (URLs, code spans, markdown links) via regex
  placeholders, then width-wraps each sentence with `textwrap`.

A `fmt-comments-ci` just task is added to the CI pipeline:

```
comfort --check --workspace
```

It exits non-zero and prints a unified diff for any file that would be
reformatted, integrating with the existing CI matrix.

Signed-off-by: Jean Mertz <git@jeanmertz.com>
@JeanMertz JeanMertz changed the title build(comfort): Add comfort doc-comment formatter crate chore(comfort): Add semantic line-break formatter for doc comments May 22, 2026
JeanMertz added 8 commits May 22, 2026 11:00
…ents

Signed-off-by: Jean Mertz <git@jeanmertz.com>
…ents

Signed-off-by: Jean Mertz <git@jeanmertz.com>
…ents

Signed-off-by: Jean Mertz <git@jeanmertz.com>
…ents

Signed-off-by: Jean Mertz <git@jeanmertz.com>
…ents

Signed-off-by: Jean Mertz <git@jeanmertz.com>
…ents

Signed-off-by: Jean Mertz <git@jeanmertz.com>
…ents

Signed-off-by: Jean Mertz <git@jeanmertz.com>
…ents

Signed-off-by: Jean Mertz <git@jeanmertz.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant