Restrict the width of doc comments with comment_width

See the diff in tests/target/enum.rs for an example.
This commit is contained in:
Seiichi Uchida 2018-03-09 09:30:47 +09:00
parent 9344d2ca83
commit 67fa394e4e
3 changed files with 17 additions and 12 deletions

View file

@ -21,8 +21,6 @@ use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
use utils::{count_newlines, mk_sp};
use std::cmp;
/// Returns attributes on the given statement.
pub fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
match stmt.node {
@ -140,7 +138,7 @@ fn rewrite_first_group_attrs(
.join("\n");
return Some((
sugared_docs.len(),
rewrite_doc_comment(&snippet, shape, context.config)?,
rewrite_doc_comment(&snippet, shape.comment(context.config), context.config)?,
));
}
// Rewrite `#[derive(..)]`s.
@ -249,13 +247,7 @@ impl Rewrite for ast::Attribute {
};
let snippet = context.snippet(self.span);
if self.is_sugared_doc {
let doc_shape = Shape {
width: cmp::min(shape.width, context.config.comment_width())
.checked_sub(shape.indent.width())
.unwrap_or(0),
..shape
};
rewrite_doc_comment(snippet, doc_shape, context.config)
rewrite_doc_comment(snippet, shape.comment(context.config), context.config)
} else {
if contains_comment(snippet) {
return Some(snippet.to_owned());

View file

@ -9,6 +9,7 @@
// except according to those terms.
use std::borrow::Cow;
use std::cmp::min;
use std::ops::{Add, Sub};
use Config;
@ -276,6 +277,17 @@ impl Shape {
.checked_sub(self.used_width() + self.width)
.unwrap_or(0)
}
pub fn comment(&self, config: &Config) -> Shape {
let width = min(
self.width,
config
.comment_width()
.checked_sub(self.indent.width())
.unwrap_or(0),
);
Shape { width, ..*self }
}
}
#[cfg(test)]

View file

@ -145,8 +145,9 @@ pub enum Bencoding<'i> {
Str(&'i [u8]),
Int(i64),
List(Vec<Bencoding<'i>>),
/// A bencoded dict value. The first element the slice of bytes in the source that the dict is
/// composed of. The second is the dict, decoded into an ordered map.
/// A bencoded dict value. The first element the slice of bytes in the
/// source that the dict is composed of. The second is the dict,
/// decoded into an ordered map.
// TODO make Dict "structlike" AKA name the two values.
Dict(&'i [u8], BTreeMap<&'i [u8], Bencoding<'i>>),
}