Skip to content

Commit

Permalink
Add absolute_comment_width setting
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-mcallister committed Oct 21, 2017
1 parent 9754bcb commit f3188c4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ create_config! {
hard_tabs: bool, false, "Use tab characters for indentation, spaces for alignment";
wrap_comments: bool, false, "Break comments to fit on the line";
comment_width: usize, 80, "Maximum length of comments. No effect unless wrap_comments = true";
absolute_comment_width: bool, false, "Treat column_width as a fixed column number to wrap at";
normalize_comments: bool, false, "Convert /* */ comments to // comments where possible";
wrap_match_arms: bool, true, "Wrap the body of arms in blocks when it does not fit on \
the same line with the pattern of arms";
Expand Down
15 changes: 11 additions & 4 deletions src/missed_spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,17 @@ impl<'a> FmtVisitor<'a> {
self.buffer.push_str(" ");
}

let comment_width = ::std::cmp::min(
self.config.comment_width(),
self.config.max_width() - self.block_indent.width(),
);
let comment_width = if self.config.absolute_comment_width() {
self.config
.comment_width()
.checked_sub(self.block_indent.width())
.unwrap_or(0)
} else {
::std::cmp::min(
self.config.comment_width(),
self.config.max_width() - self.block_indent.width(),
)
};
let comment_indent = Indent::from_width(self.config, self.buffer.cur_offset());

self.buffer.push_str(&rewrite_comment(
Expand Down
10 changes: 10 additions & 0 deletions tests/source/configs-absolute_comment_width.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 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
}
}
}
11 changes: 11 additions & 0 deletions tests/target/configs-absolute_comment_width.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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
}
}
}

0 comments on commit f3188c4

Please sign in to comment.