1 // Format string literals.
4 use unicode_categories::UnicodeCategories;
5 use unicode_segmentation::UnicodeSegmentation;
7 use crate::config::Config;
8 use crate::shape::Shape;
9 use crate::utils::{unicode_str_width, wrap_str};
11 const MIN_STRING: usize = 10;
13 /// Describes the layout of a piece of text.
14 pub(crate) struct StringFormat<'a> {
15 /// The opening sequence of characters for the piece of text
16 pub(crate) opener: &'a str,
17 /// The closing sequence of characters for the piece of text
18 pub(crate) closer: &'a str,
19 /// The opening sequence of characters for a line
20 pub(crate) line_start: &'a str,
21 /// The closing sequence of characters for a line
22 pub(crate) line_end: &'a str,
23 /// The allocated box to fit the text into
24 pub(crate) shape: Shape,
25 /// Trim trailing whitespaces
26 pub(crate) trim_end: bool,
27 pub(crate) config: &'a Config,
30 impl<'a> StringFormat<'a> {
31 pub(crate) fn new(shape: Shape, config: &'a Config) -> StringFormat<'a> {
43 /// Returns the maximum number of graphemes that is possible on a line while taking the
44 /// indentation into account.
46 /// If we cannot put at least a single character per line, the rewrite won't succeed.
47 fn max_width_with_indent(&self) -> Option<usize> {
51 .checked_sub(self.opener.len() + self.line_end.len() + 1)?
56 /// Like max_width_with_indent but the indentation is not subtracted.
57 /// This allows to fit more graphemes from the string on a line when
58 /// SnippetState::EndWithLineFeed.
59 fn max_width_without_indent(&self) -> Option<usize> {
60 self.config.max_width().checked_sub(self.line_end.len())
64 pub(crate) fn rewrite_string<'a>(
66 fmt: &StringFormat<'a>,
67 newline_max_chars: usize,
69 let max_width_with_indent = fmt.max_width_with_indent()?;
70 let max_width_without_indent = fmt.max_width_without_indent()?;
71 let indent_with_newline = fmt.shape.indent.to_string_with_newline(fmt.config);
72 let indent_without_newline = fmt.shape.indent.to_string(fmt.config);
75 // With this regex applied, all remaining whitespaces are significant
76 let strip_line_breaks_re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap();
77 let stripped_str = strip_line_breaks_re.replace_all(orig, "$1");
79 let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
81 // `cur_start` is the position in `orig` of the start of the current line.
82 let mut cur_start = 0;
83 let mut result = String::with_capacity(
86 .checked_next_power_of_two()
87 .unwrap_or(usize::max_value()),
89 result.push_str(fmt.opener);
91 // Snip a line at a time from `stripped_str` until it is used up. Push the snippet
93 let mut cur_max_width = max_width_with_indent;
94 let is_bareline_ok = fmt.line_start.is_empty() || is_whitespace(fmt.line_start);
96 // All the input starting at cur_start fits on the current line
97 if graphemes_width(&graphemes[cur_start..]) <= cur_max_width {
98 for (i, grapheme) in graphemes[cur_start..].iter().enumerate() {
99 if is_new_line(grapheme) {
100 // take care of blank lines
101 result = trim_end_but_line_feed(fmt.trim_end, result);
103 if !is_bareline_ok && cur_start + i + 1 < graphemes.len() {
104 result.push_str(&indent_without_newline);
105 result.push_str(fmt.line_start);
108 result.push_str(grapheme);
111 result = trim_end_but_line_feed(fmt.trim_end, result);
115 // The input starting at cur_start needs to be broken
120 &graphemes[cur_start..],
122 SnippetState::LineEnd(line, len) => {
123 result.push_str(&line);
124 result.push_str(fmt.line_end);
125 result.push_str(&indent_with_newline);
126 result.push_str(fmt.line_start);
127 cur_max_width = newline_max_chars;
130 SnippetState::EndWithLineFeed(line, len) => {
131 if line == "\n" && fmt.trim_end {
132 result = result.trim_end().to_string();
134 result.push_str(&line);
136 // the next line can benefit from the full width
137 cur_max_width = max_width_without_indent;
139 result.push_str(&indent_without_newline);
140 result.push_str(fmt.line_start);
141 cur_max_width = max_width_with_indent;
145 SnippetState::EndOfInput(line) => {
146 result.push_str(&line);
152 result.push_str(fmt.closer);
153 wrap_str(result, fmt.config.max_width(), fmt.shape)
156 /// Returns the index to the end of the URL if the split at index of the given string includes an
157 /// URL or alike. Otherwise, returns `None`.
158 fn detect_url(s: &[&str], index: usize) -> Option<usize> {
159 let start = match s[..=index].iter().rposition(|g| is_whitespace(g)) {
160 Some(pos) => pos + 1,
163 // 8 = minimum length for a string to contain a URL
164 if s.len() < start + 8 {
167 let split = s[start..].concat();
168 if split.contains("https://")
169 || split.contains("http://")
170 || split.contains("ftp://")
171 || split.contains("file://")
173 match s[index..].iter().position(|g| is_whitespace(g)) {
174 Some(pos) => Some(index + pos - 1),
175 None => Some(s.len() - 1),
182 /// Trims whitespaces to the right except for the line feed character.
183 fn trim_end_but_line_feed(trim_end: bool, result: String) -> String {
184 let whitespace_except_line_feed = |c: char| c.is_whitespace() && c != '\n';
185 if trim_end && result.ends_with(whitespace_except_line_feed) {
187 .trim_end_matches(whitespace_except_line_feed)
194 /// Result of breaking a string so it fits in a line and the state it ended in.
195 /// The state informs about what to do with the snippet and how to continue the breaking process.
196 #[derive(Debug, PartialEq)]
198 /// The input could not be broken and so rewriting the string is finished.
200 /// The input could be broken and the returned snippet should be ended with a
201 /// `[StringFormat::line_end]`. The next snippet needs to be indented.
203 /// The returned string is the line to print out and the number is the length that got read in
204 /// the text being rewritten. That length may be greater than the returned string if trailing
205 /// whitespaces got trimmed.
206 LineEnd(String, usize),
207 /// The input could be broken but a newline is present that cannot be trimmed. The next snippet
208 /// to be rewritten *could* use more width than what is specified by the given shape. For
209 /// example with a multiline string, the next snippet does not need to be indented, allowing
210 /// more characters to be fit within a line.
212 /// The returned string is the line to print out and the number is the length that got read in
213 /// the text being rewritten.
214 EndWithLineFeed(String, usize),
217 fn not_whitespace_except_line_feed(g: &str) -> bool {
218 is_new_line(g) || !is_whitespace(g)
221 /// Break the input string at a boundary character around the offset `max_width`. A boundary
222 /// character is either a punctuation or a whitespace.
223 /// FIXME(issue#3281): We must follow UAX#14 algorithm instead of this.
224 fn break_string(max_width: usize, trim_end: bool, line_end: &str, input: &[&str]) -> SnippetState {
225 let break_at = |index /* grapheme at index is included */| {
226 // Take in any whitespaces to the left/right of `input[index]` while
227 // preserving line feeds
228 let index_minus_ws = input[0..=index]
230 .rposition(|grapheme| not_whitespace_except_line_feed(grapheme))
232 // Take into account newlines occurring in input[0..=index], i.e., the possible next new
233 // line. If there is one, then text after it could be rewritten in a way that the available
234 // space is fully used.
235 for (i, grapheme) in input[0..=index].iter().enumerate() {
236 if is_new_line(grapheme) {
237 if i <= index_minus_ws {
238 let mut line = &input[0..i].concat()[..];
240 line = line.trim_end();
242 return SnippetState::EndWithLineFeed(format!("{}\n", line), i + 1);
248 let mut index_plus_ws = index;
249 for (i, grapheme) in input[index + 1..].iter().enumerate() {
250 if !trim_end && is_new_line(grapheme) {
251 return SnippetState::EndWithLineFeed(
252 input[0..=index + 1 + i].concat(),
255 } else if not_whitespace_except_line_feed(grapheme) {
256 index_plus_ws = index + i;
262 SnippetState::LineEnd(input[0..=index_minus_ws].concat(), index_plus_ws + 1)
264 SnippetState::LineEnd(input[0..=index_plus_ws].concat(), index_plus_ws + 1)
268 // find a first index where the unicode width of input[0..x] become > max_width
269 let max_width_index_in_input = {
270 let mut cur_width = 0;
271 let mut cur_index = 0;
272 for (i, grapheme) in input.iter().enumerate() {
273 cur_width += unicode_str_width(grapheme);
275 if cur_width > max_width {
282 // Find the position in input for breaking the string
283 if line_end.is_empty()
285 && !is_whitespace(input[max_width_index_in_input - 1])
286 && is_whitespace(input[max_width_index_in_input])
288 // At a breaking point already
289 // The line won't invalidate the rewriting because:
290 // - no extra space needed for the line_end character
291 // - extra whitespaces to the right can be trimmed
292 return break_at(max_width_index_in_input - 1);
294 if let Some(url_index_end) = detect_url(input, max_width_index_in_input) {
295 let index_plus_ws = url_index_end
296 + input[url_index_end..]
299 .position(|grapheme| not_whitespace_except_line_feed(grapheme))
302 SnippetState::LineEnd(input[..=url_index_end].concat(), index_plus_ws + 1)
304 return SnippetState::LineEnd(input[..=index_plus_ws].concat(), index_plus_ws + 1);
308 match input[0..max_width_index_in_input]
310 .rposition(|grapheme| is_whitespace(grapheme))
312 // Found a whitespace and what is on its left side is big enough.
313 Some(index) if index >= MIN_STRING => break_at(index),
314 // No whitespace found, try looking for a punctuation instead
315 _ => match input[0..max_width_index_in_input]
317 .rposition(|grapheme| is_punctuation(grapheme))
319 // Found a punctuation and what is on its left side is big enough.
320 Some(index) if index >= MIN_STRING => break_at(index),
321 // Either no boundary character was found to the left of `input[max_chars]`, or the line
322 // got too small. We try searching for a boundary character to the right.
323 _ => match input[max_width_index_in_input..]
325 .position(|grapheme| is_whitespace(grapheme) || is_punctuation(grapheme))
327 // A boundary was found after the line limit
328 Some(index) => break_at(max_width_index_in_input + index),
329 // No boundary to the right, the input cannot be broken
330 None => SnippetState::EndOfInput(input.concat()),
336 fn is_new_line(grapheme: &str) -> bool {
337 let bytes = grapheme.as_bytes();
338 bytes.starts_with(b"\n") || bytes.starts_with(b"\r\n")
341 fn is_whitespace(grapheme: &str) -> bool {
342 grapheme.chars().all(char::is_whitespace)
345 fn is_punctuation(grapheme: &str) -> bool {
348 .all(UnicodeCategories::is_punctuation_other)
351 fn graphemes_width(graphemes: &[&str]) -> usize {
352 graphemes.iter().map(|s| unicode_str_width(s)).sum()
357 use super::{break_string, detect_url, rewrite_string, SnippetState, StringFormat};
358 use crate::config::Config;
359 use crate::shape::{Indent, Shape};
360 use unicode_segmentation::UnicodeSegmentation;
364 let config = Default::default();
365 let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
366 rewrite_string("eq_", &fmt, 2);
370 fn should_break_on_whitespace() {
371 let string = "Placerat felis. Mauris porta ante sagittis purus.";
372 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
374 break_string(20, false, "", &graphemes[..]),
375 SnippetState::LineEnd("Placerat felis. ".to_string(), 16)
378 break_string(20, true, "", &graphemes[..]),
379 SnippetState::LineEnd("Placerat felis.".to_string(), 16)
384 fn should_break_on_punctuation() {
385 let string = "Placerat_felis._Mauris_porta_ante_sagittis_purus.";
386 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
388 break_string(20, false, "", &graphemes[..]),
389 SnippetState::LineEnd("Placerat_felis.".to_string(), 15)
394 fn should_break_forward() {
395 let string = "Venenatis_tellus_vel_tellus. Aliquam aliquam dolor at justo.";
396 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
398 break_string(20, false, "", &graphemes[..]),
399 SnippetState::LineEnd("Venenatis_tellus_vel_tellus. ".to_string(), 29)
402 break_string(20, true, "", &graphemes[..]),
403 SnippetState::LineEnd("Venenatis_tellus_vel_tellus.".to_string(), 29)
408 fn nothing_to_break() {
409 let string = "Venenatis_tellus_vel_tellus";
410 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
412 break_string(20, false, "", &graphemes[..]),
413 SnippetState::EndOfInput("Venenatis_tellus_vel_tellus".to_string())
418 fn significant_whitespaces() {
419 let string = "Neque in sem. \n Pellentesque tellus augue.";
420 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
422 break_string(15, false, "", &graphemes[..]),
423 SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
426 break_string(25, false, "", &graphemes[..]),
427 SnippetState::EndWithLineFeed("Neque in sem. \n".to_string(), 20)
431 break_string(15, true, "", &graphemes[..]),
432 SnippetState::LineEnd("Neque in sem.".to_string(), 19)
435 break_string(25, true, "", &graphemes[..]),
436 SnippetState::EndWithLineFeed("Neque in sem.\n".to_string(), 20)
441 fn big_whitespace() {
442 let string = "Neque in sem. Pellentesque tellus augue.";
443 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
445 break_string(20, false, "", &graphemes[..]),
446 SnippetState::LineEnd("Neque in sem. ".to_string(), 25)
449 break_string(20, true, "", &graphemes[..]),
450 SnippetState::LineEnd("Neque in sem.".to_string(), 25)
455 fn newline_in_candidate_line() {
456 let string = "Nulla\nconsequat erat at massa. Vivamus id mi.";
458 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
460 break_string(25, false, "", &graphemes[..]),
461 SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
464 break_string(25, true, "", &graphemes[..]),
465 SnippetState::EndWithLineFeed("Nulla\n".to_string(), 6)
468 let mut config: Config = Default::default();
469 config.set().max_width(27);
470 let fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
471 let rewritten_string = rewrite_string(string, &fmt, 27);
474 Some("\"Nulla\nconsequat erat at massa. \\\n Vivamus id mi.\"".to_string())
479 fn last_line_fit_with_trailing_whitespaces() {
480 let string = "Vivamus id mi. ";
481 let config: Config = Default::default();
482 let mut fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
485 let rewritten_string = rewrite_string(string, &fmt, 25);
486 assert_eq!(rewritten_string, Some("\"Vivamus id mi.\"".to_string()));
488 fmt.trim_end = false; // default value of trim_end
489 let rewritten_string = rewrite_string(string, &fmt, 25);
490 assert_eq!(rewritten_string, Some("\"Vivamus id mi. \"".to_string()));
494 fn last_line_fit_with_newline() {
495 let string = "Vivamus id mi.\nVivamus id mi.";
496 let config: Config = Default::default();
497 let fmt = StringFormat {
502 shape: Shape::legacy(100, Indent::from_width(&config, 4)),
507 let rewritten_string = rewrite_string(string, &fmt, 100);
510 Some("Vivamus id mi.\n // Vivamus id mi.".to_string())
515 fn overflow_in_non_string_content() {
516 let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
517 let config: Config = Default::default();
518 let fmt = StringFormat {
523 shape: Shape::legacy(30, Indent::from_width(&config, 8)),
529 rewrite_string(comment, &fmt, 30),
531 "Aenean metus.\n // Vestibulum ac lacus. Vivamus\n // porttitor"
538 fn overflow_in_non_string_content_with_line_end() {
539 let comment = "Aenean metus.\nVestibulum ac lacus. Vivamus porttitor";
540 let config: Config = Default::default();
541 let fmt = StringFormat {
546 shape: Shape::legacy(30, Indent::from_width(&config, 8)),
552 rewrite_string(comment, &fmt, 30),
554 "Aenean metus.\n // Vestibulum ac lacus. Vivamus@\n // porttitor"
561 fn blank_line_with_non_empty_line_start() {
562 let config: Config = Default::default();
563 let mut fmt = StringFormat {
568 shape: Shape::legacy(30, Indent::from_width(&config, 4)),
573 let comment = "Aenean metus. Vestibulum\n\nac lacus. Vivamus porttitor";
575 rewrite_string(comment, &fmt, 30),
577 "Aenean metus. Vestibulum\n //\n // ac lacus. Vivamus porttitor".to_string()
581 fmt.shape = Shape::legacy(15, Indent::from_width(&config, 4));
582 let comment = "Aenean\n\nmetus. Vestibulum ac lacus. Vivamus porttitor";
584 rewrite_string(comment, &fmt, 15),
597 fn retain_blank_lines() {
598 let config: Config = Default::default();
599 let fmt = StringFormat {
604 shape: Shape::legacy(20, Indent::from_width(&config, 4)),
609 let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n\n";
611 rewrite_string(comment, &fmt, 20),
613 "Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n //\n".to_string()
617 let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n";
619 rewrite_string(comment, &fmt, 20),
620 Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n".to_string())
623 let comment = "Aenean\n \nmetus. Vestibulum ac lacus.";
625 rewrite_string(comment, &fmt, 20),
626 Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.".to_string())
631 fn boundary_on_edge() {
632 let config: Config = Default::default();
633 let mut fmt = StringFormat {
638 shape: Shape::legacy(13, Indent::from_width(&config, 4)),
643 let comment = "Aenean metus. Vestibulum ac lacus.";
645 rewrite_string(comment, &fmt, 13),
646 Some("Aenean metus.\n // Vestibulum ac\n // lacus.".to_string())
649 fmt.trim_end = false;
650 let comment = "Vestibulum ac lacus.";
652 rewrite_string(comment, &fmt, 13),
653 Some("Vestibulum \n // ac lacus.".to_string())
658 let comment = "Vestibulum ac lacus.";
660 rewrite_string(comment, &fmt, 13),
661 Some("Vestibulum\\\n // ac lacus.".to_string())
667 let string = "aaa http://example.org something";
668 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
669 assert_eq!(detect_url(&graphemes, 8), Some(21));
671 let string = "https://example.org something";
672 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
673 assert_eq!(detect_url(&graphemes, 0), Some(18));
675 let string = "aaa ftp://example.org something";
676 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
677 assert_eq!(detect_url(&graphemes, 8), Some(20));
679 let string = "aaa file://example.org something";
680 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
681 assert_eq!(detect_url(&graphemes, 8), Some(21));
683 let string = "aaa http not an url";
684 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
685 assert_eq!(detect_url(&graphemes, 6), None);
687 let string = "aaa file://example.org";
688 let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
689 assert_eq!(detect_url(&graphemes, 8), Some(21));