-
Notifications
You must be signed in to change notification settings - Fork 933
Backport #4524 and fix #5138 - proper wrap strings with escape backslash #5483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,9 +71,10 @@ pub(crate) fn rewrite_string<'a>( | |
let indent_with_newline = fmt.shape.indent.to_string_with_newline(fmt.config); | ||
let indent_without_newline = fmt.shape.indent.to_string(fmt.config); | ||
|
||
// Strip line breaks. | ||
// Strip line breaks for lines that ends with continuation `\` forlowwed by a `new line`. | ||
// In this case, the indentation spaces at the beginning of the nex line are also stripped. | ||
// With this regex applied, all remaining whitespaces are significant | ||
let strip_line_breaks_re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][[:space:]]*").unwrap(); | ||
let strip_line_breaks_re = Regex::new(r"(([^\\]|(\\\\))(\\\\)*)\\[\n\r][[:space:]]*").unwrap(); | ||
let stripped_str = strip_line_breaks_re.replace_all(orig, "$1"); | ||
|
||
let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>(); | ||
|
@@ -223,6 +224,24 @@ fn not_whitespace_except_line_feed(g: &str) -> bool { | |
/// FIXME(issue#3281): We must follow UAX#14 algorithm instead of this. | ||
fn break_string(max_width: usize, trim_end: bool, line_end: &str, input: &[&str]) -> SnippetState { | ||
let break_at = |index /* grapheme at index is included */| { | ||
// Ensure break is not after an escape '\' as it will "escape" the `\` that is added | ||
// for concatenating the two parts of the broken line. | ||
let index = if input[index] != "\\" { | ||
index | ||
} else { | ||
let index_offset = match input[0..index] | ||
.iter() | ||
.rposition(|grapheme| grapheme.ne(&"\\")) | ||
{ | ||
// There is a non-`\` to the left | ||
Some(non_backslash_index) => (index - non_backslash_index) % 2, | ||
// Only `\` to the left | ||
None => (index + 1) % 2, | ||
}; | ||
// Make sure break is after even number (including zero) of `\` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if you could elaborate more on these comments or rephrase them somehow. When I read them I feel like there are some details that are missing. For example, why do we need to make sure the break is after an even number? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhanced the comments and moved some to a more appropriate place. I hope they are sufficiently clear now. |
||
index - index_offset | ||
}; | ||
|
||
// Take in any whitespaces to the left/right of `input[index]` while | ||
// preserving line feeds | ||
let index_minus_ws = input[0..=index] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this rephrasing could help explain that we're trying not to escape any line continuation characters that we add when we break the string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment enhanced in line with the requested change.