2015-06-23 15:58:58 +02:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// Format string literals.
|
|
|
|
|
2015-09-03 23:38:12 -04:00
|
|
|
use regex::Regex;
|
2017-07-13 18:42:14 +09:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2015-09-03 23:38:12 -04:00
|
|
|
|
2015-09-05 22:39:28 -07:00
|
|
|
use config::Config;
|
2017-09-17 15:23:25 +09:00
|
|
|
use shape::Shape;
|
2017-01-06 16:35:28 +13:00
|
|
|
use utils::wrap_str;
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2017-07-13 18:42:14 +09:00
|
|
|
const MIN_STRING: usize = 10;
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2018-10-02 00:41:56 +02:00
|
|
|
/// Describes the layout of a piece of text.
|
2015-06-23 15:58:58 +02:00
|
|
|
pub struct StringFormat<'a> {
|
2018-10-02 00:41:56 +02:00
|
|
|
/// The opening sequence of characters for the piece of text
|
2015-06-23 15:58:58 +02:00
|
|
|
pub opener: &'a str,
|
2018-10-02 00:41:56 +02:00
|
|
|
/// The closing sequence of characters for the piece of text
|
2015-06-23 15:58:58 +02:00
|
|
|
pub closer: &'a str,
|
2018-10-02 00:41:56 +02:00
|
|
|
/// The opening sequence of characters for a line
|
2015-06-23 15:58:58 +02:00
|
|
|
pub line_start: &'a str,
|
2018-10-02 00:41:56 +02:00
|
|
|
/// The closing sequence of characters for a line
|
2015-06-23 15:58:58 +02:00
|
|
|
pub line_end: &'a str,
|
2018-10-02 00:41:56 +02:00
|
|
|
/// The allocated box to fit the text into
|
2017-01-31 08:28:48 +13:00
|
|
|
pub shape: Shape,
|
2018-10-02 00:41:56 +02:00
|
|
|
/// Trim trailing whitespaces
|
2015-06-23 15:58:58 +02:00
|
|
|
pub trim_end: bool,
|
2015-09-05 22:39:28 -07:00
|
|
|
pub config: &'a Config,
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
|
|
|
|
2017-10-01 19:39:00 +09:00
|
|
|
impl<'a> StringFormat<'a> {
|
|
|
|
pub fn new(shape: Shape, config: &'a Config) -> StringFormat<'a> {
|
|
|
|
StringFormat {
|
|
|
|
opener: "\"",
|
|
|
|
closer: "\"",
|
|
|
|
line_start: " ",
|
|
|
|
line_end: "\\",
|
2018-01-22 13:05:18 +09:00
|
|
|
shape,
|
2017-10-01 19:39:00 +09:00
|
|
|
trim_end: false,
|
2018-01-22 13:05:18 +09:00
|
|
|
config,
|
2017-10-01 19:39:00 +09:00
|
|
|
}
|
|
|
|
}
|
2018-07-07 12:24:09 +02:00
|
|
|
|
|
|
|
/// Returns the maximum number of graphemes that is possible on a line while taking the
|
|
|
|
/// indentation into account.
|
|
|
|
///
|
|
|
|
/// If we cannot put at least a single character per line, the rewrite won't succeed.
|
|
|
|
fn max_chars_with_indent(&self) -> Option<usize> {
|
|
|
|
Some(
|
|
|
|
self.shape
|
|
|
|
.width
|
|
|
|
.checked_sub(self.opener.len() + self.line_end.len() + 1)?
|
2018-07-14 19:17:07 +02:00
|
|
|
+ 1,
|
2018-07-07 12:24:09 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like max_chars_with_indent but the indentation is not substracted.
|
|
|
|
/// This allows to fit more graphemes from the string on a line when
|
2018-10-04 11:40:47 +02:00
|
|
|
/// SnippetState::EndWithLineFeed.
|
2018-07-07 12:24:09 +02:00
|
|
|
fn max_chars_without_indent(&self) -> Option<usize> {
|
|
|
|
Some(self.config.max_width().checked_sub(self.line_end.len())?)
|
|
|
|
}
|
2017-10-01 19:39:00 +09:00
|
|
|
}
|
|
|
|
|
2018-07-14 19:17:07 +02:00
|
|
|
pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String> {
|
2018-07-07 12:24:09 +02:00
|
|
|
let max_chars_with_indent = fmt.max_chars_with_indent()?;
|
|
|
|
let max_chars_without_indent = fmt.max_chars_without_indent()?;
|
2018-10-04 11:40:47 +02:00
|
|
|
let indent_with_newline = fmt.shape.indent.to_string_with_newline(fmt.config);
|
|
|
|
let indent_without_newline = fmt.shape.indent.to_string(fmt.config);
|
2018-07-07 12:24:09 +02:00
|
|
|
|
2015-09-03 23:38:12 -04:00
|
|
|
// Strip line breaks.
|
2018-07-07 12:24:09 +02:00
|
|
|
// With this regex applied, all remaining whitespaces are significant
|
|
|
|
let strip_line_breaks_re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
|
|
|
|
let stripped_str = strip_line_breaks_re.replace_all(orig, "$1");
|
2015-09-03 23:38:12 -04:00
|
|
|
|
2017-05-25 16:08:08 +09:00
|
|
|
let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2016-04-12 06:45:47 +12:00
|
|
|
// `cur_start` is the position in `orig` of the start of the current line.
|
2015-06-23 15:58:58 +02:00
|
|
|
let mut cur_start = 0;
|
2017-06-16 08:49:49 +09:00
|
|
|
let mut result = String::with_capacity(
|
|
|
|
stripped_str
|
|
|
|
.len()
|
|
|
|
.checked_next_power_of_two()
|
|
|
|
.unwrap_or(usize::max_value()),
|
|
|
|
);
|
2015-06-23 15:58:58 +02:00
|
|
|
result.push_str(fmt.opener);
|
|
|
|
|
2018-07-07 12:24:09 +02:00
|
|
|
// Snip a line at a time from `stripped_str` until it is used up. Push the snippet
|
2016-04-12 06:45:47 +12:00
|
|
|
// onto result.
|
2018-07-07 12:24:09 +02:00
|
|
|
let mut cur_max_chars = max_chars_with_indent;
|
2018-10-04 11:40:47 +02:00
|
|
|
let is_overflow_allowed = is_whitespace(fmt.line_start);
|
2018-07-07 12:24:09 +02:00
|
|
|
loop {
|
|
|
|
// All the input starting at cur_start fits on the current line
|
|
|
|
if graphemes.len() - cur_start <= cur_max_chars {
|
2018-10-04 09:16:08 +02:00
|
|
|
let last_line = graphemes[cur_start..].join("");
|
|
|
|
if fmt.trim_end {
|
|
|
|
result.push_str(&last_line.trim_right());
|
|
|
|
} else {
|
|
|
|
result.push_str(&last_line);
|
|
|
|
}
|
2018-07-07 12:24:09 +02:00
|
|
|
break;
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
2015-09-25 12:53:25 +02:00
|
|
|
|
2018-07-07 12:24:09 +02:00
|
|
|
// The input starting at cur_start needs to be broken
|
|
|
|
match break_string(cur_max_chars, fmt.trim_end, &graphemes[cur_start..]) {
|
|
|
|
SnippetState::LineEnd(line, len) => {
|
|
|
|
result.push_str(&line);
|
|
|
|
result.push_str(fmt.line_end);
|
2018-10-04 11:40:47 +02:00
|
|
|
result.push_str(&indent_with_newline);
|
2018-07-07 12:24:09 +02:00
|
|
|
result.push_str(fmt.line_start);
|
|
|
|
cur_max_chars = max_chars_with_indent;
|
|
|
|
cur_start += len;
|
|
|
|
}
|
2018-10-04 11:40:47 +02:00
|
|
|
SnippetState::EndWithLineFeed(line, len) => {
|
2018-07-07 12:24:09 +02:00
|
|
|
result.push_str(&line);
|
2018-10-04 11:40:47 +02:00
|
|
|
if is_overflow_allowed {
|
|
|
|
// the next line can benefit from the full width
|
|
|
|
cur_max_chars = max_chars_without_indent;
|
|
|
|
} else {
|
|
|
|
result.push_str(&indent_without_newline);
|
|
|
|
result.push_str(fmt.line_start);
|
|
|
|
cur_max_chars = max_chars_with_indent;
|
|
|
|
}
|
2018-07-07 12:24:09 +02:00
|
|
|
cur_start += len;
|
|
|
|
}
|
|
|
|
SnippetState::EndOfInput(line) => {
|
|
|
|
result.push_str(&line);
|
2015-06-23 15:58:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 12:24:09 +02:00
|
|
|
}
|
2016-04-12 06:45:47 +12:00
|
|
|
|
2018-07-07 12:24:09 +02:00
|
|
|
result.push_str(fmt.closer);
|
|
|
|
wrap_str(result, fmt.config.max_width(), fmt.shape)
|
|
|
|
}
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2018-07-07 12:24:09 +02:00
|
|
|
/// Result of breaking a string so it fits in a line and the state it ended in.
|
|
|
|
/// The state informs about what to do with the snippet and how to continue the breaking process.
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum SnippetState {
|
|
|
|
/// The input could not be broken and so rewriting the string is finished.
|
|
|
|
EndOfInput(String),
|
|
|
|
/// The input could be broken and the returned snippet should be ended with a
|
|
|
|
/// `[StringFormat::line_end]`. The next snippet needs to be indented.
|
2018-10-04 11:40:47 +02:00
|
|
|
///
|
2018-10-02 00:41:56 +02:00
|
|
|
/// The returned string is the line to print out and the number is the length that got read in
|
|
|
|
/// the text being rewritten. That length may be greater than the returned string if trailing
|
|
|
|
/// whitespaces got trimmed.
|
2018-07-07 12:24:09 +02:00
|
|
|
LineEnd(String, usize),
|
2018-10-04 11:40:47 +02:00
|
|
|
/// The input could be broken but a newline is present that cannot be trimmed. The next snippet
|
|
|
|
/// to be rewritten *could* use more width than what is specified by the given shape. For
|
|
|
|
/// example with a multiline string, the next snippet does not need to be indented, allowing
|
|
|
|
/// more characters to be fit within a line.
|
|
|
|
///
|
|
|
|
/// The returned string is the line to print out and the number is the length that got read in
|
|
|
|
/// the text being rewritten.
|
|
|
|
EndWithLineFeed(String, usize),
|
2018-07-07 12:24:09 +02:00
|
|
|
}
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2018-07-07 12:24:09 +02:00
|
|
|
/// Break the input string at a boundary character around the offset `max_chars`. A boundary
|
|
|
|
/// character is either a punctuation or a whitespace.
|
|
|
|
fn break_string(max_chars: usize, trim_end: bool, input: &[&str]) -> SnippetState {
|
|
|
|
let break_at = |index /* grapheme at index is included */| {
|
|
|
|
// Take in any whitespaces to the left/right of `input[index]` and
|
|
|
|
// check if there is a line feed, in which case whitespaces needs to be kept.
|
|
|
|
let mut index_minus_ws = index;
|
|
|
|
for (i, grapheme) in input[0..=index].iter().enumerate().rev() {
|
2018-10-02 00:41:56 +02:00
|
|
|
if !is_whitespace(grapheme) {
|
2018-07-07 12:24:09 +02:00
|
|
|
index_minus_ws = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-02 00:41:56 +02:00
|
|
|
// Take into account newlines occuring in input[0..=index], i.e., the possible next new
|
|
|
|
// line. If there is one, then text after it could be rewritten in a way that the available
|
|
|
|
// space is fully used.
|
|
|
|
for (i, grapheme) in input[0..=index].iter().enumerate() {
|
|
|
|
if is_line_feed(grapheme) {
|
|
|
|
if i < index_minus_ws || !trim_end {
|
2018-10-04 11:40:47 +02:00
|
|
|
return SnippetState::EndWithLineFeed(input[0..=i].join("").to_string(), i + 1);
|
2018-10-02 00:41:56 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-07 12:24:09 +02:00
|
|
|
let mut index_plus_ws = index;
|
|
|
|
for (i, grapheme) in input[index + 1..].iter().enumerate() {
|
|
|
|
if !trim_end && is_line_feed(grapheme) {
|
2018-10-04 11:40:47 +02:00
|
|
|
return SnippetState::EndWithLineFeed(
|
2018-07-07 12:24:09 +02:00
|
|
|
input[0..=index + 1 + i].join("").to_string(),
|
|
|
|
index + 2 + i,
|
|
|
|
);
|
|
|
|
} else if !is_whitespace(grapheme) {
|
|
|
|
index_plus_ws = index + i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 15:04:50 +09:00
|
|
|
|
2018-07-07 12:24:09 +02:00
|
|
|
if trim_end {
|
|
|
|
SnippetState::LineEnd(
|
|
|
|
input[0..=index_minus_ws].join("").to_string(),
|
|
|
|
index_plus_ws + 1,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
SnippetState::LineEnd(
|
|
|
|
input[0..=index_plus_ws].join("").to_string(),
|
|
|
|
index_plus_ws + 1,
|
|
|
|
)
|
2017-10-31 15:04:50 +09:00
|
|
|
}
|
2018-07-07 12:24:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Find the position in input for breaking the string
|
|
|
|
match input[0..max_chars]
|
|
|
|
.iter()
|
|
|
|
.rposition(|grapheme| is_whitespace(grapheme))
|
|
|
|
{
|
|
|
|
// Found a whitespace and what is on its left side is big enough.
|
|
|
|
Some(index) if index >= MIN_STRING => break_at(index),
|
|
|
|
// No whitespace found, try looking for a punctuation instead
|
|
|
|
_ => match input[0..max_chars]
|
|
|
|
.iter()
|
|
|
|
.rposition(|grapheme| is_punctuation(grapheme))
|
|
|
|
{
|
|
|
|
// Found a punctuation and what is on its left side is big enough.
|
|
|
|
Some(index) if index >= MIN_STRING => break_at(index),
|
|
|
|
// Either no boundary character was found to the left of `input[max_chars]`, or the line
|
|
|
|
// got too small. We try searching for a boundary character to the right.
|
|
|
|
_ => match input[max_chars..]
|
|
|
|
.iter()
|
|
|
|
.position(|grapheme| is_whitespace(grapheme) || is_punctuation(grapheme))
|
|
|
|
{
|
|
|
|
// A boundary was found after the line limit
|
|
|
|
Some(index) => break_at(max_chars + index),
|
|
|
|
// No boundary to the right, the input cannot be broken
|
|
|
|
None => SnippetState::EndOfInput(input.join("").to_string()),
|
|
|
|
},
|
|
|
|
},
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|
2018-07-07 12:24:09 +02:00
|
|
|
}
|
2015-06-23 15:58:58 +02:00
|
|
|
|
2018-07-07 12:24:09 +02:00
|
|
|
fn is_line_feed(grapheme: &str) -> bool {
|
|
|
|
grapheme.as_bytes()[0] == b'\n'
|
2015-09-25 12:53:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 20:08:29 +02:00
|
|
|
fn is_whitespace(grapheme: &str) -> bool {
|
|
|
|
grapheme.chars().all(|c| c.is_whitespace())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_punctuation(grapheme: &str) -> bool {
|
|
|
|
match grapheme.as_bytes()[0] {
|
|
|
|
b':' | b',' | b';' | b'.' => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 12:53:25 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2018-07-07 12:24:09 +02:00
|
|
|
use super::{break_string, rewrite_string, SnippetState, StringFormat};
|
2018-10-02 00:41:56 +02:00
|
|
|
use config::Config;
|
2017-09-17 15:23:25 +09:00
|
|
|
use shape::{Indent, Shape};
|
2018-07-07 12:24:09 +02:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2015-09-25 12:53:25 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue343() {
|
|
|
|
let config = Default::default();
|
2017-10-01 19:39:00 +09:00
|
|
|
let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
|
2018-07-14 19:17:07 +02:00
|
|
|
rewrite_string("eq_", &fmt);
|
2015-09-25 12:53:25 +02:00
|
|
|
}
|
2018-07-07 12:24:09 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_break_on_whitespace() {
|
|
|
|
let string = "Placerat felis. Mauris porta ante sagittis purus.";
|
|
|
|
let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
|
|
|
|
assert_eq!(
|
|
|
|
break_string(20, false, &graphemes[..]),
|
|
|
|
SnippetState::LineEnd("Placerat felis. ".to_string(), 16)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
break_string(20, true, &graphemes[..]),
|
|
|
|
SnippetState::LineEnd("Placerat felis.".to_string(), 16)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_break_on_punctuation() {
|
|
|
|
let string = "Placerat_felis._Mauris_porta_ante_sagittis_purus.";
|
|
|
|
let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
|
|
|
|
assert_eq!(
|
|
|
|
break_string(20, false, &graphemes[..]),
|
|
|
|
SnippetState::LineEnd("Placerat_felis.".to_string(), 15)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_break_forward() {
|
|
|
|
let string = "Venenatis_tellus_vel_tellus. Aliquam aliquam dolor at justo.";
|
|
|
|
let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
|
|
|
|
assert_eq!(
|
|
|
|
break_string(20, false, &graphemes[..]),
|
|
|
|
SnippetState::LineEnd("Venenatis_tellus_vel_tellus. ".to_string(), 29)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
break_string(20, true, &graphemes[..]),
|
|
|
|
SnippetState::LineEnd("Venenatis_tellus_vel_tellus.".to_string(), 29)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nothing_to_break() {
|
|
|
|
let string = "Venenatis_tellus_vel_tellus";
|
|
|
|
let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
|
|
|
|
assert_eq!(
|
|
|
|
break_string(20, false, &graphemes[..]),
|
|
|
|
SnippetState::EndOfInput("Venenatis_tellus_vel_tellus".to_string())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn significant_whitespaces() {
|
|
|
|
let string = "Neque in sem. \n Pellentesque tellus augue.";
|
|
|
|
let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
|
|
|
|
assert_eq!(
|
|
|
|
break_string(15, false, &graphemes[..]),
|
2018-10-04 11:40:47 +02:00
|
|
|
SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
|
2018-07-07 12:24:09 +02:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
break_string(25, false, &graphemes[..]),
|
2018-10-04 11:40:47 +02:00
|
|
|
SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
|
2018-07-07 12:24:09 +02:00
|
|
|
);
|
|
|
|
// if `StringFormat::line_end` is true, then the line feed does not matter anymore
|
|
|
|
assert_eq!(
|
|
|
|
break_string(15, true, &graphemes[..]),
|
|
|
|
SnippetState::LineEnd("Neque in sem.".to_string(), 26)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
break_string(25, true, &graphemes[..]),
|
|
|
|
SnippetState::LineEnd("Neque in sem.".to_string(), 26)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn big_whitespace() {
|
|
|
|
let string = "Neque in sem. Pellentesque tellus augue.";
|
|
|
|
let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
|
|
|
|
assert_eq!(
|
|
|
|
break_string(20, false, &graphemes[..]),
|
|
|
|
SnippetState::LineEnd("Neque in sem. ".to_string(), 25)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
break_string(20, true, &graphemes[..]),
|
|
|
|
SnippetState::LineEnd("Neque in sem.".to_string(), 25)
|
|
|
|
);
|
|
|
|
}
|
2018-10-02 00:41:56 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn newline_in_candidate_line() {
|
|
|
|
let string = "Nulla\nconsequat erat at massa. Vivamus id mi.";
|
|
|
|
|
|
|
|
let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
|
|
|
|
assert_eq!(
|
|
|
|
break_string(25, false, &graphemes[..]),
|
2018-10-04 11:40:47 +02:00
|
|
|
SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
|
2018-10-02 00:41:56 +02:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
break_string(25, true, &graphemes[..]),
|
2018-10-04 11:40:47 +02:00
|
|
|
SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
|
2018-10-02 00:41:56 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut config: Config = Default::default();
|
|
|
|
config.set().max_width(27);
|
|
|
|
let fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
|
|
|
|
let rewritten_string = rewrite_string(string, &fmt);
|
|
|
|
assert_eq!(
|
|
|
|
rewritten_string,
|
|
|
|
Some("\"Nulla\nconsequat erat at massa. \\\n Vivamus id mi.\"".to_string())
|
|
|
|
);
|
|
|
|
}
|
2018-10-04 09:16:08 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn last_line_fit_with_trailing_whitespaces() {
|
|
|
|
let string = "Vivamus id mi. ";
|
|
|
|
let config: Config = Default::default();
|
|
|
|
let mut fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
|
|
|
|
|
|
|
|
fmt.trim_end = true;
|
|
|
|
let rewritten_string = rewrite_string(string, &fmt);
|
|
|
|
assert_eq!(rewritten_string, Some("\"Vivamus id mi.\"".to_string()));
|
|
|
|
|
|
|
|
fmt.trim_end = false; // default value of trim_end
|
|
|
|
let rewritten_string = rewrite_string(string, &fmt);
|
|
|
|
assert_eq!(rewritten_string, Some("\"Vivamus id mi. \"".to_string()));
|
|
|
|
}
|
2018-10-04 11:40:47 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow_in_non_string_content() {
|
|
|
|
let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
|
|
|
|
let config: Config = Default::default();
|
|
|
|
let fmt = StringFormat {
|
|
|
|
opener: "",
|
|
|
|
closer: "",
|
|
|
|
line_start: "// ",
|
|
|
|
line_end: "",
|
|
|
|
shape: Shape::legacy(30, Indent::from_width(&config, 8)),
|
|
|
|
trim_end: true,
|
|
|
|
config: &config,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
rewrite_string(comment, &fmt),
|
|
|
|
Some(
|
|
|
|
"Aenean metus.\n // Vestibulum ac lacus. Vivamus\n // porttitor"
|
|
|
|
.to_string()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn overflow_in_non_string_content_with_line_end() {
|
|
|
|
let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
|
|
|
|
let config: Config = Default::default();
|
|
|
|
let fmt = StringFormat {
|
|
|
|
opener: "",
|
|
|
|
closer: "",
|
|
|
|
line_start: "// ",
|
|
|
|
line_end: "@",
|
|
|
|
shape: Shape::legacy(30, Indent::from_width(&config, 8)),
|
|
|
|
trim_end: true,
|
|
|
|
config: &config,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
rewrite_string(comment, &fmt),
|
|
|
|
Some(
|
|
|
|
"Aenean metus.\n // Vestibulum ac lacus. Vivamus@\n // porttitor"
|
|
|
|
.to_string()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2015-06-23 15:58:58 +02:00
|
|
|
}
|