From 67fa394e4ed4f4a933c09f0cbd50a1e5b1af56e5 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Fri, 9 Mar 2018 09:30:47 +0900 Subject: [PATCH] Restrict the width of doc comments with comment_width See the diff in tests/target/enum.rs for an example. --- src/attr.rs | 12 ++---------- src/shape.rs | 12 ++++++++++++ tests/target/enum.rs | 5 +++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/attr.rs b/src/attr.rs index 7dedc65b0e0..2e4a4632620 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -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()); diff --git a/src/shape.rs b/src/shape.rs index 22d6a096efa..047a50340eb 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -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)] diff --git a/tests/target/enum.rs b/tests/target/enum.rs index de88f610b6a..50d1037fe09 100644 --- a/tests/target/enum.rs +++ b/tests/target/enum.rs @@ -145,8 +145,9 @@ pub enum Bencoding<'i> { Str(&'i [u8]), Int(i64), List(Vec>), - /// 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>>), }