Refactor indent and width into Shape struct

This commit is contained in:
Nick Cameron 2017-01-31 08:28:48 +13:00
parent 6054f28bd2
commit 428339fdc3
15 changed files with 865 additions and 885 deletions

View file

@ -13,7 +13,7 @@
use unicode_segmentation::UnicodeSegmentation;
use regex::Regex;
use Indent;
use Shape;
use config::Config;
use utils::wrap_str;
@ -24,8 +24,7 @@ pub struct StringFormat<'a> {
pub closer: &'a str,
pub line_start: &'a str,
pub line_end: &'a str,
pub width: usize,
pub offset: Indent,
pub shape: Shape,
pub trim_end: bool,
pub config: &'a Config,
}
@ -37,7 +36,7 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
let stripped_str = re.replace_all(orig, "$1");
let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
let indent = fmt.offset.to_string(fmt.config);
let indent = fmt.shape.indent.to_string(fmt.config);
let punctuation = ":,;.";
// `cur_start` is the position in `orig` of the start of the current line.
@ -50,7 +49,7 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
let ender_length = fmt.line_end.len();
// If we cannot put at least a single character per line, the rewrite won't
// succeed.
let max_chars = try_opt!(fmt.width.checked_sub(fmt.opener.len() + ender_length + 1)) + 1;
let max_chars = try_opt!(fmt.shape.width.checked_sub(fmt.opener.len() + ender_length + 1)) + 1;
// Snip a line at a time from `orig` until it is used up. Push the snippet
// onto result.
@ -118,7 +117,7 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
}
result.push_str(fmt.closer);
wrap_str(result, fmt.config.max_width, fmt.width, fmt.offset)
wrap_str(result, fmt.config.max_width, fmt.shape)
}
#[cfg(test)]
@ -133,8 +132,7 @@ mod test {
closer: "\"",
line_start: " ",
line_end: "\\",
width: 2,
offset: ::Indent::empty(),
shape: ::Shape::legacy(2, ::Indent::empty()),
trim_end: false,
config: &config,
};