Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -132,7 +133,7 @@ fn rewrite_pairs_multiline<T: Rewrite>(
} 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))
Expand Down
22 changes: 22 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/target/issue_6859_hard_tab_breaks.rs
Original file line number Diff line number Diff line change
@@ -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
};
}