Skip to content
Open
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
2 changes: 1 addition & 1 deletion crates/cli-python/tests/dbt/output.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
== [models/customers.sql] FAIL
L: 22 | P: 16 | LT02 | Expected indent of 4 spaces. [layout.indent]
L: 42 | P: 27 | LT02 | Expected line break and indent of 4 spaces before "on".
L: 42 | P: 27 | LT02 | Expected line break and indent of 8 spaces before "on".
| [layout.indent]
L: 42 | P: 31 | LT02 | Expected indent of 12 spaces. [layout.indent]
L: 65 | P: 41 | LT01 | Expected only single space before "customers". Found "
Expand Down
23 changes: 19 additions & 4 deletions crates/lib/src/utils/reflow/reindent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ impl IndentPoint {
fn closing_indent_balance(&self) -> isize {
self.initial_indent_balance + self.indent_impulse
}

/// Returns the count of relevant untaken indents, filtering when there's a trough.
/// This mirrors the logic in `IndentLine::desired_indent_units`.
fn relevant_untaken_count(&self) -> usize {
if self.indent_trough != 0 {
self.untaken_indents
.iter()
.filter(|&&i| {
i <= self.initial_indent_balance - (self.indent_impulse - self.indent_trough)
})
.count()
} else {
self.untaken_indents.len()
}
}
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -675,7 +690,7 @@ fn lint_line_untaken_positive_indents(
if imbalanced_indent_locs.contains(&ip.idx) {
// Force it at the relevant position.
let desired_indent = single_indent
.repeat((ip.closing_indent_balance() - ip.untaken_indents.len() as isize) as usize);
.repeat((ip.closing_indent_balance() - ip.relevant_untaken_count() as isize).max(0) as usize);
let target_point = elements[ip.idx].as_point().unwrap();

let (results, new_point) = target_point.indent_to(
Expand All @@ -689,7 +704,7 @@ fn lint_line_untaken_positive_indents(

elements[ip.idx] = ReflowElement::Point(new_point);
// Keep track of the indent we forced, by returning it.
return (results, vec![ip.closing_indent_balance() as usize]);
return (results, vec![ip.closing_indent_balance().max(0) as usize]);
}
}

Expand Down Expand Up @@ -757,7 +772,7 @@ fn lint_line_untaken_positive_indents(
if ip.closing_indent_balance() == closing_trough {
target_point_idx = ip.idx;
desired_indent = single_indent
.repeat((ip.closing_indent_balance() - ip.untaken_indents.len() as isize) as usize);
.repeat((ip.closing_indent_balance() - ip.relevant_untaken_count() as isize).max(0) as usize);
break;
}
}
Expand Down Expand Up @@ -829,7 +844,7 @@ fn lint_line_untaken_negative_indents(
}

let desired_indent = single_indent.repeat(
(ip.closing_indent_balance() - ip.untaken_indents.len() as isize
(ip.closing_indent_balance() - ip.relevant_untaken_count() as isize
+ forced_indents.len() as isize)
.max(0) as usize,
);
Expand Down
Loading