3 releases (stable)
Uses new Rust 2024
| 1.1.0 | Mar 8, 2026 |
|---|---|
| 1.0.0 | Mar 8, 2026 |
| 0.1.0 | Mar 6, 2026 |
#86 in Cargo plugins
38KB
768 lines
cargo-wrap-comments
A Rust command-line tool that formats source code comments to wrap long lines at a specified width. This does what rustfmt should do.
Features
- Wraps long comments to a configurable width (default: 100 characters)
- Reads
max_widthfrom.rustfmt.tomlorrustfmt.tomlif--max-widthis not specified - Combines consecutive comment lines before wrapping
- Supports Rust comment styles:
//,///,//! - Preserves indentation, comment markers, and blank comment lines
- Respects hierarchical markers (
#,*,-,1.,A.,a.), back-tick quoted text, and code blocks - Glob pattern support for processing multiple files
- Stdin/stdout mode when no files are specified
- Check mode for previewing changes without modifying files
Installation
cargo install cargo-wrap-comments
From source
cargo build --release
The binary will be at target/release/cargo-wrap-comments.
Usage
The tool can be run as a standalone command or as a Cargo subcommand:
# As a cargo subcommand
cargo wrap-comments src/main.rs
# As a standalone command
cargo-wrap-comments src/main.rs
# Format comments in multiple files with glob patterns
cargo wrap-comments "src/**/*.rs"
# Set custom width
cargo wrap-comments --max-width 80 src/main.rs
# Preview changes without modifying files
cargo wrap-comments --check src/main.rs
# Read from stdin, write to stdout
cat src/main.rs | cargo wrap-comments
cargo wrap-comments < src/main.rs
Options
| Option | Description | Default |
|---|---|---|
-w, --max-width N |
Maximum line width | 100 |
--check |
Preview mode, don't modify files | |
-v, --verbose |
Print verbose output | |
-q, --quiet |
Print less output | |
-V, --version |
Show version information | |
-h, --help |
Show help |
Comment Combining Rules
Consecutive comment lines are combined before wrapping when ALL of these are true:
- Same comment marker (
//,///, or//!) - Same indentation level
- Neither line is blank
- The next line does not start with a hierarchical marker:
#,*,-,```,1.,A.,a. - The current line does not contain a code fence marker (
```) - The current line does not start with
#(markdown heading lines are not combined with the text that follows)
Width Resolution
The line width is resolved in this order:
--max-width Non the command linemax_widthfrom.rustfmt.tomlorrustfmt.toml, searching from the current directory up to$HOME- Default: 100
The search stops at the first config file found. If it exists but has no max_width, the default is used.
Exit Codes
0— success1— one or more errors occurred (missing files, processing failures)
Dependencies
~1.3–2MB
~38K SLoC