Skip to content

Commit beda1d3

Browse files
authored
Remove indentation level special-casing in TieredLinesElider (#307)
As [promised](#300 (review)). Two minor changes: First, in `#all_indentation_levels`, we were excluding bookends by inference: specifically, we assumed that if there were nonzero indentation levels, the lines with indentation level zero must be top-level bookends. We can just ask if `&:complete_bookend?` instead. Second – and this one is more complicated – it turns out we don't need to exclude level 0 from `box_groups_at_decreasing_indentation_levels_within(pane)`. Why? First, note that only clean panes are ever elided. Furthermore, we can assume that there is at least one dirty pane; otherwise, we [don't try to elide](https://github.com/splitwise/super_diff/blob/a14d17283fb9827db003f848be79c2003fac5e95/lib/super_diff/core/tiered_lines_elider.rb#L14). Now: for a _top-level_ structure (no nesting), such as a multi-line string, we want to include level-0 boxes for elision. For _nested_ structures, we don't, because we don't want to elide the bookend symbols/lines, such as `{`/`}` for a Hash `[`/`]` for an Array, etc. However, a level-0 box for a _nested_ structure necessarily spans the entire structure and therefore all lines; it starts with the open bookend and ends with the close bookend. And since there is at least one dirty pane, it's not possible for that level-0 box to fit within any given pane. Thus, level-0 boxes for nested structured will be [filtered out of `boxes_within_pane`](https://github.com/splitwise/super_diff/blob/a14d17283fb9827db003f848be79c2003fac5e95/lib/super_diff/core/tiered_lines_elider.rb#L138). So, for nested structures, `possible_indentation_levels` will still exclude 0. For flat top-level structures, a top-level box would have been created for each top-level line, so it should still _include_ zero.
1 parent e767257 commit beda1d3

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Fix integration snippet indentation. [#299](https://github.com/splitwise/super_diff/pull/299) by [@gschlager](https://github.com/gschlager)
1212
- Pin all actions to full commit SHA. [#305](https://github.com/splitwise/super_diff/pull/305)
1313
- Do not attempt to sub-diff multiline strings. [#304](https://github.com/splitwise/super_diff/pull/304)
14+
- Tweak TieredLinesElider. [#307](https://github.com/splitwise/super_diff/pull/307)
1415

1516
## 0.18.0 - 2025-12-05
1617

lib/super_diff/core/tiered_lines_elider.rb

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ def one_dimensional_line_tree?
9999
end
100100

101101
def all_indentation_levels
102-
levels = lines.map(&:indentation_level).uniq
103-
normalized_indentation_levels(levels)
102+
lines
103+
.reject(&:complete_bookend?)
104+
.map(&:indentation_level)
105+
.uniq
104106
end
105107

106108
def find_boxes_to_elide_within(pane)
@@ -129,16 +131,15 @@ def find_boxes_to_elide_within(pane)
129131
def normalized_box_groups_at_decreasing_indentation_levels_within(pane)
130132
box_groups_at_decreasing_indentation_levels_within(pane).map(
131133
&method(:filter_out_boxes_fully_contained_in_others)
132-
).map(&method(:combine_congruent_boxes))
134+
).map(&method(:combine_contiguous_boxes))
133135
end
134136

135137
def box_groups_at_decreasing_indentation_levels_within(pane)
136138
boxes_within_pane = boxes.select { |box| box.fits_fully_within?(pane) }
137139

138140
levels = boxes_within_pane.map(&:indentation_level).uniq
139141

140-
possible_indentation_levels =
141-
normalized_indentation_levels(levels).sort.reverse
142+
possible_indentation_levels = levels.sort.reverse
142143

143144
possible_indentation_levels.map do |indentation_level|
144145
boxes_within_pane.select do |box|
@@ -167,15 +168,7 @@ def filter_out_boxes_fully_contained_in_others(boxes)
167168
end
168169
end
169170

170-
def normalized_indentation_levels(levels)
171-
# For flat structures (strings), include level 0
172-
return levels if levels.all?(&:zero?)
173-
174-
# For nested structures (arrays, hashes), exclude level 0 (brackets)
175-
levels.select(&:positive?)
176-
end
177-
178-
def combine_congruent_boxes(boxes)
171+
def combine_contiguous_boxes(boxes)
179172
combine(boxes, on: :indentation_level)
180173
end
181174

0 commit comments

Comments
 (0)