diff --git a/src/pairs.rs b/src/pairs.rs index 48948b88b3b..736e28c5b51 100644 --- a/src/pairs.rs +++ b/src/pairs.rs @@ -7,7 +7,8 @@ use crate::rewrite::{Rewrite, RewriteContext, RewriteErrorExt, RewriteResult}; use crate::shape::Shape; use crate::spanned::Spanned; use crate::utils::{ - first_line_width, is_single_line, last_line_width, trimmed_last_line_width, wrap_str, + first_line_width, is_single_line, last_line_width, last_line_width_cfg, + trimmed_last_line_width, wrap_str, }; /// Sigils that decorate a binop pair. @@ -132,7 +133,7 @@ fn rewrite_pairs_multiline( } else { shape.used_width() }; - if last_line_width(&result) + offset <= nested_shape.used_width() { + if last_line_width_cfg(context.config, &result) + offset <= nested_shape.used_width() { // We must snuggle the next line onto the previous line to avoid an orphan. if let Some(line_shape) = shape.offset_left_opt(s.len() + 2 + trimmed_last_line_width(&result)) diff --git a/src/utils.rs b/src/utils.rs index b676803379f..4c838751dc6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -222,6 +222,14 @@ pub(crate) fn last_line_width(s: &str) -> usize { unicode_str_width(s.rsplitn(2, '\n').next().unwrap_or("")) } +/// The width of the last line in s. +#[inline] +pub(crate) fn last_line_width_cfg(config: &Config, s: &str) -> usize { + let last_line = s.rsplitn(2, '\n').next().unwrap_or(""); + let (prefix_width, prefix_end) = get_prefix_space_width2(&config, last_line); + prefix_width + unicode_str_width(&last_line[prefix_end..]) +} + /// The total used width of the last line. #[inline] pub(crate) fn last_line_used_width(s: &str, offset: usize) -> usize { @@ -697,6 +705,20 @@ fn get_prefix_space_width(config: &Config, s: &str) -> usize { width } +fn get_prefix_space_width2(config: &Config, s: &str) -> (usize, usize) { + let mut width = 0; + let mut prefix_end = 0; + for c in s.chars() { + match c { + ' ' => width += 1, + '\t' => width += config.tab_spaces(), + _ => return (width, prefix_end), + } + prefix_end += 1; + } + (width, prefix_end) +} + pub(crate) trait NodeIdExt { fn root() -> Self; } diff --git a/tests/target/issue_6859_hard_tab_breaks.rs b/tests/target/issue_6859_hard_tab_breaks.rs new file mode 100644 index 00000000000..c83dc3637b8 --- /dev/null +++ b/tests/target/issue_6859_hard_tab_breaks.rs @@ -0,0 +1,11 @@ +// rustfmt-hard_tabs: true + +fn testing() { + let _ = some_long_name + | if some_other_long_name { + foo + } + | if some_other_name { + bar + }; +}