Skip to content

Commit f70439b

Browse files
gschlagerjas14
authored andcommitted
Support elision for flat line trees
1 parent 060d503 commit f70439b

4 files changed

Lines changed: 104 additions & 10 deletions

File tree

.rubocop_todo.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Metrics/AbcSize:
1818
# AllowedMethods: refine
1919
Metrics/BlockLength:
2020
Max: 5536
21+
Exclude:
22+
- 'spec/unit/core/tiered_lines_elider_spec.rb'
2123

2224
# Offense count: 2
2325
# Configuration parameters: CountBlocks, CountModifierForms, Max.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add max major version constraints to dependencies. [#296](https://github.com/splitwise/super_diff/pull/296)
88
- Remove unused `syntax_tree` gems from development. [#297](https://github.com/splitwise/super_diff/pull/297)
99
- Simplify tiered lines elider. [#302](https://github.com/splitwise/super_diff/pull/302)
10+
- Support elision for flat line trees. [#300](https://github.com/splitwise/super_diff/pull/300)
1011

1112
## 0.18.0 - 2025-12-05
1213

lib/super_diff/core/tiered_lines_elider.rb

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

101101
def all_indentation_levels
102-
lines
103-
.map(&:indentation_level)
104-
.select(&:positive?)
105-
.uniq
102+
levels = lines.map(&:indentation_level).uniq
103+
normalized_indentation_levels(levels)
106104
end
107105

108106
def find_boxes_to_elide_within(pane)
@@ -137,13 +135,10 @@ def normalized_box_groups_at_decreasing_indentation_levels_within(pane)
137135
def box_groups_at_decreasing_indentation_levels_within(pane)
138136
boxes_within_pane = boxes.select { |box| box.fits_fully_within?(pane) }
139137

138+
levels = boxes_within_pane.map(&:indentation_level).uniq
139+
140140
possible_indentation_levels =
141-
boxes_within_pane
142-
.map(&:indentation_level)
143-
.select(&:positive?)
144-
.uniq
145-
.sort
146-
.reverse
141+
normalized_indentation_levels(levels).sort.reverse
147142

148143
possible_indentation_levels.map do |indentation_level|
149144
boxes_within_pane.select do |box|
@@ -172,6 +167,14 @@ def filter_out_boxes_fully_contained_in_others(boxes)
172167
end
173168
end
174169

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+
175178
def combine_congruent_boxes(boxes)
176179
combine(boxes, on: :indentation_level)
177180
end

spec/unit/core/tiered_lines_elider_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,94 @@
311311
end
312312

313313
context 'and the line tree contains non-noops in addition to noops' do
314+
context 'and the line tree is flat (indentation level 0)' do
315+
it 'elides the beginning of the noop so as to put it at the maximum' do
316+
# Diff:
317+
#
318+
# "one"
319+
# "two"
320+
# "three"
321+
# - "four"
322+
# + "FOUR"
323+
324+
lines = [
325+
an_actual_line(
326+
type: :noop,
327+
indentation_level: 0,
328+
value: %("one")
329+
),
330+
an_actual_line(
331+
type: :noop,
332+
indentation_level: 0,
333+
value: %("two")
334+
),
335+
an_actual_line(
336+
type: :noop,
337+
indentation_level: 0,
338+
value: %("three")
339+
),
340+
an_actual_line(
341+
type: :delete,
342+
indentation_level: 0,
343+
value: %("four")
344+
),
345+
an_actual_line(
346+
type: :insert,
347+
indentation_level: 0,
348+
value: %("FOUR")
349+
)
350+
]
351+
352+
line_tree_with_elisions =
353+
with_configuration(
354+
diff_elision_enabled: true,
355+
diff_elision_maximum: 2
356+
) { described_class.call(lines) }
357+
358+
# Result:
359+
#
360+
# # ...
361+
# "three"
362+
# - "four"
363+
# + "FOUR"
364+
365+
expect(line_tree_with_elisions).to match(
366+
[
367+
an_expected_elision(
368+
indentation_level: 0,
369+
children: [
370+
an_expected_line(
371+
type: :noop,
372+
indentation_level: 0,
373+
value: %("one")
374+
),
375+
an_expected_line(
376+
type: :noop,
377+
indentation_level: 0,
378+
value: %("two")
379+
)
380+
]
381+
),
382+
an_expected_line(
383+
type: :noop,
384+
indentation_level: 0,
385+
value: %("three")
386+
),
387+
an_expected_line(
388+
type: :delete,
389+
indentation_level: 0,
390+
value: %("four")
391+
),
392+
an_expected_line(
393+
type: :insert,
394+
indentation_level: 0,
395+
value: %("FOUR")
396+
)
397+
]
398+
)
399+
end
400+
end
401+
314402
context 'and the only noops that exist are above the only non-noops that exist' do
315403
it 'elides the beginning of the noop so as to put it at the maximum' do
316404
# Diff:

0 commit comments

Comments
 (0)