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

Add absolute_comment_width setting #2076

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/config.rs
Expand Up @@ -618,6 +618,8 @@ create_config! {
wrap_comments: bool, false, false, "Break comments to fit on the line";
comment_width: usize, 80, false,
"Maximum length of comments. No effect unless wrap_comments = true";
absolute_comment_width: bool, false, false,
"Treat column_width as a fixed column number to wrap at";
normalize_comments: bool, false, false, "Convert /* */ comments to // comments where possible";
wrap_match_arms: bool, true, false, "Wrap the body of arms in blocks when it does not fit on \
the same line with the pattern of arms";
Expand Down
9 changes: 8 additions & 1 deletion src/missed_spans.rs
Expand Up @@ -174,7 +174,14 @@ impl<'a> FmtVisitor<'a> {
}

let comment_width = ::std::cmp::min(
self.config.comment_width(),
if self.config.absolute_comment_width() {
self.config
.comment_width()
.checked_sub(self.block_indent.width())
.unwrap_or(0)
} else {
self.config.comment_width()
},
self.config.max_width() - self.block_indent.width(),
);
let comment_indent = Indent::from_width(self.config, self.buffer.cur_offset());
Expand Down
14 changes: 14 additions & 0 deletions tests/source/configs-absolute_comment_width.rs
@@ -0,0 +1,14 @@
// rustfmt-wrap_comments: true
// rustfmt-comment_width: 72
// rustfmt-absolute_comment_width: true
impl MyStruct {
fn my_fun() {
if foo {
// Please wrap this comment at column number 72 exactly, rather than column 84
}

fn bar() {
//! Please wrap this comment at column number 72 exactly, rather than column 84
}
}
}
16 changes: 16 additions & 0 deletions tests/target/configs-absolute_comment_width.rs
@@ -0,0 +1,16 @@
// rustfmt-wrap_comments: true
// rustfmt-comment_width: 72
// rustfmt-absolute_comment_width: true
impl MyStruct {
fn my_fun() {
if foo {
// Please wrap this comment at column number 72 exactly,
// rather than column 84
}

fn bar() {
//! Please wrap this comment at column number 72 exactly,
//! rather than column 84
}
}
}