Skip to content

Commit f217ccc

Browse files
committed
Support elision for flat line trees
1 parent 5518fcd commit f217ccc

2 files changed

Lines changed: 102 additions & 10 deletions

File tree

lib/super_diff/core/tiered_lines_elider.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,8 @@ def one_dimensional_line_tree?
108108
end
109109

110110
def all_indentation_levels
111-
lines
112-
.map(&:indentation_level)
113-
.select(&:positive?)
114-
.uniq
111+
levels = lines.map(&:indentation_level).uniq
112+
normalized_indentation_levels(levels)
115113
end
116114

117115
def find_boxes_to_elide_within(pane)
@@ -146,13 +144,10 @@ def normalized_box_groups_at_decreasing_indentation_levels_within(pane)
146144
def box_groups_at_decreasing_indentation_levels_within(pane)
147145
boxes_within_pane = boxes.select { |box| box.fits_fully_within?(pane) }
148146

147+
levels = boxes_within_pane.map(&:indentation_level).uniq
148+
149149
possible_indentation_levels =
150-
boxes_within_pane
151-
.map(&:indentation_level)
152-
.select(&:positive?)
153-
.uniq
154-
.sort
155-
.reverse
150+
normalized_indentation_levels(levels).sort.reverse
156151

157152
possible_indentation_levels.map do |indentation_level|
158153
boxes_within_pane.select do |box|
@@ -174,6 +169,14 @@ def filter_out_boxes_fully_contained_in_others(boxes)
174169
end
175170
end
176171

172+
def normalized_indentation_levels(levels)
173+
# For flat structures (strings), include level 0
174+
return levels if levels.all?(&:zero?)
175+
176+
# For nested structures (arrays, hashes), exclude level 0 (brackets)
177+
levels.select(&:positive?)
178+
end
179+
177180
def combine_congruent_boxes(boxes)
178181
combine(boxes, on: :indentation_level)
179182
end

spec/unit/core/tiered_lines_elider_spec.rb

Lines changed: 89 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:
@@ -449,6 +537,7 @@
449537
]
450538
)
451539
end
540+
452541
end
453542

454543
context 'and the only noops that exist are below the only non-noops that exist' do

0 commit comments

Comments
 (0)