super_diff 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +0 -1
  3. data/lib/super_diff.rb +27 -6
  4. data/lib/super_diff/active_record.rb +7 -7
  5. data/lib/super_diff/active_record/differs/active_record_relation.rb +3 -13
  6. data/lib/super_diff/active_record/object_inspection.rb +2 -6
  7. data/lib/super_diff/active_record/object_inspection/inspection_tree_builders.rb +16 -0
  8. data/lib/super_diff/active_record/object_inspection/{inspectors → inspection_tree_builders}/active_record_model.rb +19 -20
  9. data/lib/super_diff/active_record/object_inspection/{inspectors → inspection_tree_builders}/active_record_relation.rb +9 -8
  10. data/lib/super_diff/active_record/operation_tree_flatteners.rb +10 -0
  11. data/lib/super_diff/active_record/operation_tree_flatteners/active_record_relation.rb +17 -0
  12. data/lib/super_diff/active_record/operation_trees/active_record_relation.rb +8 -7
  13. data/lib/super_diff/active_support.rb +8 -8
  14. data/lib/super_diff/active_support/differs/hash_with_indifferent_access.rb +3 -13
  15. data/lib/super_diff/active_support/object_inspection.rb +2 -6
  16. data/lib/super_diff/active_support/object_inspection/{inspectors.rb → inspection_tree_builders.rb} +2 -2
  17. data/lib/super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access.rb +37 -0
  18. data/lib/super_diff/active_support/operation_tree_builders/hash_with_indifferent_access.rb +17 -0
  19. data/lib/super_diff/active_support/operation_tree_flatteners.rb +10 -0
  20. data/lib/super_diff/active_support/operation_tree_flatteners/hash_with_indifferent_access.rb +17 -0
  21. data/lib/super_diff/active_support/operation_trees/hash_with_indifferent_access.rb +4 -7
  22. data/lib/super_diff/colorized_document_extensions.rb +9 -6
  23. data/lib/super_diff/configuration.rb +81 -8
  24. data/lib/super_diff/csi.rb +1 -2
  25. data/lib/super_diff/csi/four_bit_color.rb +0 -2
  26. data/lib/super_diff/differs/array.rb +1 -1
  27. data/lib/super_diff/differs/base.rb +3 -21
  28. data/lib/super_diff/differs/custom_object.rb +1 -1
  29. data/lib/super_diff/differs/default_object.rb +1 -1
  30. data/lib/super_diff/differs/hash.rb +1 -1
  31. data/lib/super_diff/differs/main.rb +1 -7
  32. data/lib/super_diff/differs/multiline_string.rb +1 -1
  33. data/lib/super_diff/differs/time_like.rb +1 -1
  34. data/lib/super_diff/equality_matchers/array.rb +2 -2
  35. data/lib/super_diff/equality_matchers/default.rb +2 -2
  36. data/lib/super_diff/equality_matchers/hash.rb +2 -2
  37. data/lib/super_diff/equality_matchers/multiline_string.rb +2 -2
  38. data/lib/super_diff/equality_matchers/primitive.rb +4 -7
  39. data/lib/super_diff/equality_matchers/singleline_string.rb +2 -2
  40. data/lib/super_diff/helpers.rb +52 -2
  41. data/lib/super_diff/line.rb +83 -0
  42. data/lib/super_diff/object_inspection.rb +12 -1
  43. data/lib/super_diff/object_inspection/inspection_tree.rb +183 -81
  44. data/lib/super_diff/object_inspection/inspection_tree_builders.rb +44 -0
  45. data/lib/super_diff/object_inspection/inspection_tree_builders/array.rb +38 -0
  46. data/lib/super_diff/object_inspection/inspection_tree_builders/base.rb +27 -0
  47. data/lib/super_diff/object_inspection/inspection_tree_builders/custom_object.rb +37 -0
  48. data/lib/super_diff/object_inspection/inspection_tree_builders/default_object.rb +63 -0
  49. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/defaults.rb +1 -2
  50. data/lib/super_diff/object_inspection/inspection_tree_builders/hash.rb +46 -0
  51. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/main.rb +5 -10
  52. data/lib/super_diff/object_inspection/inspection_tree_builders/primitive.rb +21 -0
  53. data/lib/super_diff/object_inspection/{inspectors → inspection_tree_builders}/time_like.rb +19 -18
  54. data/lib/super_diff/object_inspection/nodes.rb +33 -32
  55. data/lib/super_diff/object_inspection/nodes/as_lines_when_rendering_to_lines.rb +97 -0
  56. data/lib/super_diff/object_inspection/nodes/as_prefix_when_rendering_to_lines.rb +27 -0
  57. data/lib/super_diff/object_inspection/nodes/as_prelude_when_rendering_to_lines.rb +27 -0
  58. data/lib/super_diff/object_inspection/nodes/as_single_line.rb +33 -0
  59. data/lib/super_diff/object_inspection/nodes/base.rb +55 -20
  60. data/lib/super_diff/object_inspection/nodes/inspection.rb +47 -7
  61. data/lib/super_diff/object_inspection/nodes/nesting.rb +16 -5
  62. data/lib/super_diff/object_inspection/nodes/only_when.rb +54 -0
  63. data/lib/super_diff/object_inspection/nodes/text.rb +16 -2
  64. data/lib/super_diff/object_inspection/nodes/when_empty.rb +21 -6
  65. data/lib/super_diff/object_inspection/nodes/when_non_empty.rb +20 -5
  66. data/lib/super_diff/object_inspection/nodes/when_rendering_to_lines.rb +27 -0
  67. data/lib/super_diff/object_inspection/nodes/when_rendering_to_string.rb +27 -0
  68. data/lib/super_diff/object_inspection/prefix_for_next_node.rb +6 -0
  69. data/lib/super_diff/object_inspection/prelude_for_next_node.rb +6 -0
  70. data/lib/super_diff/operation_tree_builders/array.rb +7 -10
  71. data/lib/super_diff/operation_tree_builders/base.rb +6 -6
  72. data/lib/super_diff/operation_tree_builders/custom_object.rb +5 -2
  73. data/lib/super_diff/operation_tree_builders/default_object.rb +1 -1
  74. data/lib/super_diff/operation_tree_builders/hash.rb +0 -7
  75. data/lib/super_diff/operation_tree_builders/multiline_string.rb +2 -6
  76. data/lib/super_diff/operation_tree_flatteners.rb +20 -0
  77. data/lib/super_diff/operation_tree_flatteners/array.rb +15 -0
  78. data/lib/super_diff/operation_tree_flatteners/base.rb +54 -0
  79. data/lib/super_diff/operation_tree_flatteners/collection.rb +139 -0
  80. data/lib/super_diff/operation_tree_flatteners/custom_object.rb +28 -0
  81. data/lib/super_diff/operation_tree_flatteners/default_object.rb +32 -0
  82. data/lib/super_diff/operation_tree_flatteners/hash.rb +41 -0
  83. data/lib/super_diff/operation_tree_flatteners/multiline_string.rb +17 -0
  84. data/lib/super_diff/operation_trees/array.rb +4 -7
  85. data/lib/super_diff/operation_trees/base.rb +39 -16
  86. data/lib/super_diff/operation_trees/custom_object.rb +4 -8
  87. data/lib/super_diff/operation_trees/default_object.rb +28 -13
  88. data/lib/super_diff/operation_trees/hash.rb +4 -7
  89. data/lib/super_diff/operation_trees/main.rb +1 -1
  90. data/lib/super_diff/operation_trees/multiline_string.rb +4 -7
  91. data/lib/super_diff/operations/binary_operation.rb +1 -6
  92. data/lib/super_diff/operations/unary_operation.rb +2 -30
  93. data/lib/super_diff/recursion_guard.rb +3 -3
  94. data/lib/super_diff/rspec.rb +12 -13
  95. data/lib/super_diff/rspec/monkey_patches.rb +2 -2
  96. data/lib/super_diff/rspec/object_inspection.rb +4 -1
  97. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders.rb +44 -0
  98. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/collection_containing_exactly.rb +9 -8
  99. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/collection_including.rb +9 -8
  100. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/double.rb +103 -0
  101. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/hash_including.rb +36 -0
  102. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/instance_of.rb +3 -5
  103. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/kind_of.rb +3 -5
  104. data/lib/super_diff/rspec/object_inspection/{inspectors → inspection_tree_builders}/object_having_attributes.rb +10 -12
  105. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/primitive.rb +10 -0
  106. data/lib/super_diff/rspec/object_inspection/inspection_tree_builders/value_within.rb +33 -0
  107. data/lib/super_diff/rspec/operation_tree_builders/collection_containing_exactly.rb +0 -3
  108. data/lib/super_diff/tiered_lines.rb +4 -0
  109. data/lib/super_diff/tiered_lines_elider.rb +490 -0
  110. data/lib/super_diff/tiered_lines_formatter.rb +79 -0
  111. data/lib/super_diff/version.rb +1 -1
  112. data/spec/examples.txt +407 -410
  113. data/spec/integration/rails/active_support_spec.rb +19 -0
  114. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +12 -6
  115. data/spec/integration/rspec/eq_matcher_spec.rb +22 -88
  116. data/spec/integration/rspec/have_attributes_matcher_spec.rb +6 -7
  117. data/spec/integration/rspec/match_array_matcher_spec.rb +14 -7
  118. data/spec/integration/rspec/match_matcher_spec.rb +10 -5
  119. data/spec/spec_helper.rb +1 -0
  120. data/spec/support/command_runner.rb +15 -25
  121. data/spec/support/helpers.rb +21 -0
  122. data/spec/support/integration/helpers.rb +2 -0
  123. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +3 -3
  124. data/spec/support/integration/test_programs/base.rb +36 -10
  125. data/spec/support/shared_examples/active_record.rb +3 -2
  126. data/spec/support/shared_examples/active_support.rb +65 -0
  127. data/spec/support/shared_examples/elided_diffs.rb +914 -0
  128. data/spec/support/shared_examples/hash_with_indifferent_access.rb +16 -16
  129. data/spec/support/unit/helpers.rb +15 -0
  130. data/spec/support/unit/matchers/match_output.rb +41 -0
  131. data/spec/unit/active_record/object_inspection_spec.rb +273 -0
  132. data/spec/unit/equality_matchers/main_spec.rb +51 -71
  133. data/spec/unit/helpers_spec.rb +61 -0
  134. data/spec/unit/operation_tree_flatteners/array_spec.rb +604 -0
  135. data/spec/unit/operation_tree_flatteners/custom_object_spec.rb +667 -0
  136. data/spec/unit/operation_tree_flatteners/default_object_spec.rb +687 -0
  137. data/spec/unit/operation_tree_flatteners/hash_spec.rb +632 -0
  138. data/spec/unit/operation_tree_flatteners/multiline_string_spec.rb +121 -0
  139. data/spec/unit/rspec/object_inspection_spec.rb +446 -0
  140. data/spec/unit/super_diff_spec.rb +1488 -846
  141. data/spec/unit/tiered_lines_elider_spec.rb +6356 -0
  142. data/spec/unit/tiered_lines_formatter_spec.rb +193 -0
  143. metadata +88 -50
  144. data/lib/super_diff/active_record/diff_formatters.rb +0 -10
  145. data/lib/super_diff/active_record/diff_formatters/active_record_relation.rb +0 -23
  146. data/lib/super_diff/active_record/object_inspection/inspectors.rb +0 -16
  147. data/lib/super_diff/active_support/diff_formatters.rb +0 -10
  148. data/lib/super_diff/active_support/diff_formatters/hash_with_indifferent_access.rb +0 -36
  149. data/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb +0 -28
  150. data/lib/super_diff/diff_formatters.rb +0 -14
  151. data/lib/super_diff/diff_formatters/array.rb +0 -21
  152. data/lib/super_diff/diff_formatters/base.rb +0 -33
  153. data/lib/super_diff/diff_formatters/custom_object.rb +0 -30
  154. data/lib/super_diff/diff_formatters/default_object.rb +0 -46
  155. data/lib/super_diff/diff_formatters/defaults.rb +0 -10
  156. data/lib/super_diff/diff_formatters/hash.rb +0 -34
  157. data/lib/super_diff/diff_formatters/main.rb +0 -41
  158. data/lib/super_diff/object_inspection/inspectors.rb +0 -23
  159. data/lib/super_diff/object_inspection/inspectors/array.rb +0 -32
  160. data/lib/super_diff/object_inspection/inspectors/base.rb +0 -36
  161. data/lib/super_diff/object_inspection/inspectors/custom_object.rb +0 -37
  162. data/lib/super_diff/object_inspection/inspectors/default_object.rb +0 -61
  163. data/lib/super_diff/object_inspection/inspectors/hash.rb +0 -32
  164. data/lib/super_diff/object_inspection/inspectors/primitive.rb +0 -28
  165. data/lib/super_diff/object_inspection/inspectors/string.rb +0 -23
  166. data/lib/super_diff/object_inspection/nodes/break.rb +0 -15
  167. data/lib/super_diff/object_inspection/nodes/when_multiline.rb +0 -22
  168. data/lib/super_diff/object_inspection/nodes/when_singleline.rb +0 -24
  169. data/lib/super_diff/rspec/object_inspection/inspectors.rb +0 -40
  170. data/lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb +0 -36
  171. data/lib/super_diff/rspec/object_inspection/inspectors/primitive.rb +0 -13
  172. data/lib/super_diff/rspec/object_inspection/inspectors/value_within.rb +0 -29
  173. data/spec/support/object_id.rb +0 -27
  174. data/spec/support/ruby_versions.rb +0 -11
@@ -1,15 +1,13 @@
1
1
  module SuperDiff
2
2
  module RSpec
3
3
  module ObjectInspection
4
- module Inspectors
5
- class KindOf < SuperDiff::ObjectInspection::Inspectors::Base
4
+ module InspectionTreeBuilders
5
+ class KindOf < SuperDiff::ObjectInspection::InspectionTreeBuilders::Base
6
6
  def self.applies_to?(value)
7
7
  SuperDiff::RSpec.a_kind_of_something?(value) || SuperDiff::RSpec.kind_of_something?(value)
8
8
  end
9
9
 
10
- protected
11
-
12
- def inspection_tree
10
+ def call
13
11
  SuperDiff::ObjectInspection::InspectionTree.new do
14
12
  add_text do |value|
15
13
  klass = if SuperDiff::RSpec.a_kind_of_something?(value)
@@ -1,27 +1,25 @@
1
1
  module SuperDiff
2
2
  module RSpec
3
3
  module ObjectInspection
4
- module Inspectors
5
- class ObjectHavingAttributes < SuperDiff::ObjectInspection::Inspectors::Base
4
+ module InspectionTreeBuilders
5
+ class ObjectHavingAttributes < SuperDiff::ObjectInspection::InspectionTreeBuilders::Base
6
6
  def self.applies_to?(value)
7
7
  SuperDiff::RSpec.an_object_having_some_attributes?(value)
8
8
  end
9
9
 
10
- protected
11
-
12
- def inspection_tree
10
+ def call
13
11
  SuperDiff::ObjectInspection::InspectionTree.new do
14
- add_text "#<an object having attributes ("
12
+ as_lines_when_rendering_to_lines(collection_bookend: :open) do
13
+ add_text "#<an object having attributes ("
14
+ end
15
15
 
16
16
  nested do |aliased_matcher|
17
- insert_hash_inspection_of(
18
- aliased_matcher.expected,
19
- initial_break: nil,
20
- )
17
+ insert_hash_inspection_of(aliased_matcher.expected)
21
18
  end
22
19
 
23
- add_break
24
- add_text ")>"
20
+ as_lines_when_rendering_to_lines(collection_bookend: :close) do
21
+ add_text ")>"
22
+ end
25
23
  end
26
24
  end
27
25
  end
@@ -0,0 +1,10 @@
1
+ module SuperDiff
2
+ module RSpec
3
+ module ObjectInspection
4
+ module InspectionTreeBuilders
5
+ class Primitive < SuperDiff::ObjectInspection::InspectionTreeBuilders::Primitive
6
+ end
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,33 @@
1
+ module SuperDiff
2
+ module RSpec
3
+ module ObjectInspection
4
+ module InspectionTreeBuilders
5
+ class ValueWithin < SuperDiff::ObjectInspection::InspectionTreeBuilders::Base
6
+ def self.applies_to?(value)
7
+ SuperDiff::RSpec.a_value_within_something?(value)
8
+ end
9
+
10
+ def call
11
+ SuperDiff::ObjectInspection::InspectionTree.new do
12
+ as_prelude_when_rendering_to_lines do
13
+ add_text "#<a value within "
14
+
15
+ add_inspection_of as_lines: false do |aliased_matcher|
16
+ aliased_matcher.base_matcher.instance_variable_get("@delta")
17
+ end
18
+
19
+ add_text " of "
20
+ end
21
+
22
+ # rubocop:disable Style/SymbolProc
23
+ add_inspection_of { |aliased_matcher| aliased_matcher.expected }
24
+ # rubocop:enable Style/SymbolProc
25
+
26
+ add_text ">"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -52,7 +52,6 @@ module SuperDiff
52
52
  key: index,
53
53
  value: value,
54
54
  index: index,
55
- index_in_collection: collection.index(value),
56
55
  )
57
56
  end
58
57
 
@@ -64,7 +63,6 @@ module SuperDiff
64
63
  key: index,
65
64
  value: value,
66
65
  index: index,
67
- index_in_collection: collection.index(value),
68
66
  )
69
67
  end
70
68
 
@@ -76,7 +74,6 @@ module SuperDiff
76
74
  key: index,
77
75
  value: value,
78
76
  index: index,
79
- index_in_collection: collection.index(value),
80
77
  )
81
78
  end
82
79
 
@@ -0,0 +1,4 @@
1
+ module SuperDiff
2
+ class TieredLines < Array
3
+ end
4
+ end
@@ -0,0 +1,490 @@
1
+ module SuperDiff
2
+ class TieredLinesElider
3
+ SIZE_OF_ELISION = 1
4
+
5
+ extend AttrExtras.mixin
6
+ include Helpers
7
+
8
+ method_object :lines
9
+
10
+ def call
11
+ if all_lines_are_changed_or_unchanged?
12
+ lines
13
+ else
14
+ elided_lines
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def all_lines_are_changed_or_unchanged?
21
+ panes.size == 1 && panes.first.range == Range.new(0, lines.length - 1)
22
+ end
23
+
24
+ def elided_lines
25
+ boxes_to_elide.reverse.reduce(lines) do |lines_with_elisions, box|
26
+ with_box_elided(box, lines_with_elisions)
27
+ end
28
+ end
29
+
30
+ def boxes_to_elide
31
+ @_boxes_to_elide ||=
32
+ panes_to_consider_for_eliding.reduce([]) do |array, pane|
33
+ array + (find_boxes_to_elide_within(pane) || [])
34
+ end
35
+ end
36
+
37
+ def panes_to_consider_for_eliding
38
+ panes.select do |pane|
39
+ pane.type == :clean && pane.range.size > maximum
40
+ end
41
+ end
42
+
43
+ def panes
44
+ @_panes ||= BuildPanes.call(dirty_panes: padded_dirty_panes, lines: lines)
45
+ end
46
+
47
+ def padded_dirty_panes
48
+ @_padded_dirty_panes ||= combine_congruent_panes(
49
+ dirty_panes.
50
+ map(&:padded).
51
+ map { |pane| pane.capped_to(0, lines.size - 1) }
52
+ )
53
+ end
54
+
55
+ def dirty_panes
56
+ @_dirty_panes ||= lines.
57
+ each_with_index.
58
+ select { |line, index| line.type != :noop }.
59
+ reduce([]) do |panes, (_, index)|
60
+ if !panes.empty? && panes.last.range.end == index - 1
61
+ panes[0..-2] + [panes[-1].extended_to(index)]
62
+ else
63
+ panes + [Pane.new(type: :dirty, range: index..index)]
64
+ end
65
+ end
66
+ end
67
+
68
+ def with_box_elided(box, lines)
69
+ box_at_start_of_lines =
70
+ if lines.first.complete_bookend?
71
+ box.range.begin == 1
72
+ else
73
+ box.range.begin == 0
74
+ end
75
+
76
+ box_at_end_of_lines =
77
+ if lines.last.complete_bookend?
78
+ box.range.end == lines.size - 2
79
+ else
80
+ box.range.end == lines.size - 1
81
+ end
82
+
83
+ if one_dimensional_line_tree? && outermost_box?(box)
84
+ if box_at_start_of_lines
85
+ with_start_of_box_elided(box, lines)
86
+ elsif box_at_end_of_lines
87
+ with_end_of_box_elided(box, lines)
88
+ else
89
+ with_middle_of_box_elided(box, lines)
90
+ end
91
+ else
92
+ with_subset_of_lines_elided(
93
+ lines,
94
+ range: box.range,
95
+ indentation_level: box.indentation_level,
96
+ )
97
+ end
98
+ end
99
+
100
+ def outermost_box?(box)
101
+ box.indentation_level == all_indentation_levels.min
102
+ end
103
+
104
+ def one_dimensional_line_tree?
105
+ all_indentation_levels.size == 1
106
+ end
107
+
108
+ def all_indentation_levels
109
+ lines.
110
+ map(&:indentation_level).
111
+ select { |indentation_level| indentation_level > 0 }.
112
+ uniq
113
+ end
114
+
115
+ def find_boxes_to_elide_within(pane)
116
+ set_of_boxes =
117
+ normalized_box_groups_at_decreasing_indentation_levels_within(pane)
118
+
119
+ total_size_before_eliding = lines[pane.range].
120
+ reject(&:complete_bookend?).
121
+ size
122
+
123
+ if total_size_before_eliding > maximum
124
+ if maximum > 0
125
+ set_of_boxes.find do |boxes|
126
+ total_size_after_eliding =
127
+ total_size_before_eliding -
128
+ boxes.sum { |box| box.range.size - SIZE_OF_ELISION }
129
+ total_size_after_eliding <= maximum
130
+ end
131
+ else
132
+ set_of_boxes[-1]
133
+ end
134
+ else
135
+ []
136
+ end
137
+ end
138
+
139
+ def normalized_box_groups_at_decreasing_indentation_levels_within(pane)
140
+ box_groups_at_decreasing_indentation_levels_within(pane).
141
+ map(&method(:filter_out_boxes_fully_contained_in_others)).
142
+ map(&method(:combine_congruent_boxes))
143
+ end
144
+
145
+ def box_groups_at_decreasing_indentation_levels_within(pane)
146
+ boxes_within_pane = boxes.select do |box|
147
+ box.fits_fully_within?(pane)
148
+ end
149
+
150
+ possible_indentation_levels = boxes_within_pane.
151
+ map(&:indentation_level).
152
+ select { |indentation_level| indentation_level > 0 }.
153
+ uniq.
154
+ sort.
155
+ reverse
156
+
157
+ possible_indentation_levels.map do |indentation_level|
158
+ boxes_within_pane.select do |box|
159
+ box.indentation_level >= indentation_level
160
+ end
161
+ end
162
+ end
163
+
164
+ def filter_out_boxes_fully_contained_in_others(boxes)
165
+ sorted_boxes = boxes.sort_by do |box|
166
+ [box.indentation_level, box.range.begin, box.range.end]
167
+ end
168
+
169
+ boxes.reject do |box2|
170
+ sorted_boxes.any? do |box1|
171
+ !box1.equal?(box2) && box1.fully_contains?(box2)
172
+ end
173
+ end
174
+ end
175
+
176
+ def combine_congruent_boxes(boxes)
177
+ combine(boxes, on: :indentation_level)
178
+ end
179
+
180
+ def combine_congruent_panes(panes)
181
+ combine(panes, on: :type)
182
+ end
183
+
184
+ def combine(spannables, on:)
185
+ criterion = on
186
+ spannables.reduce([]) do |combined_spannables, spannable|
187
+ if (
188
+ !combined_spannables.empty? &&
189
+ spannable.range.begin <= combined_spannables.last.range.end + 1 &&
190
+ spannable.public_send(criterion) ==
191
+ combined_spannables.last.public_send(criterion)
192
+ )
193
+ combined_spannables[0..-2] + [
194
+ combined_spannables[-1].extended_to(spannable.range.end),
195
+ ]
196
+ else
197
+ combined_spannables + [spannable]
198
+ end
199
+ end
200
+ end
201
+
202
+ def boxes
203
+ @_boxes ||= BuildBoxes.call(lines)
204
+ end
205
+
206
+ def with_start_of_box_elided(box, lines)
207
+ amount_to_elide =
208
+ if maximum > 0
209
+ box.range.size - maximum + SIZE_OF_ELISION
210
+ else
211
+ box.range.size
212
+ end
213
+
214
+ with_subset_of_lines_elided(
215
+ lines,
216
+ range: Range.new(
217
+ box.range.begin,
218
+ box.range.begin + amount_to_elide - 1,
219
+ ),
220
+ indentation_level: box.indentation_level
221
+ )
222
+ end
223
+
224
+ def with_end_of_box_elided(box, lines)
225
+ amount_to_elide =
226
+ if maximum > 0
227
+ box.range.size - maximum + SIZE_OF_ELISION
228
+ else
229
+ box.range.size
230
+ end
231
+
232
+ range =
233
+ if amount_to_elide > 0
234
+ Range.new(box.range.end - amount_to_elide + 1, box.range.end)
235
+ else
236
+ box.range
237
+ end
238
+
239
+ with_subset_of_lines_elided(
240
+ lines,
241
+ range: range,
242
+ indentation_level: box.indentation_level
243
+ )
244
+ end
245
+
246
+ def with_middle_of_box_elided(box, lines)
247
+ half_of_maximum, remainder =
248
+ if maximum > 0
249
+ (maximum - SIZE_OF_ELISION).divmod(2)
250
+ else
251
+ [0, 0]
252
+ end
253
+
254
+ opening_length, closing_length =
255
+ half_of_maximum, half_of_maximum + remainder
256
+
257
+ with_subset_of_lines_elided(
258
+ lines,
259
+ range: Range.new(
260
+ box.range.begin + opening_length,
261
+ box.range.end - closing_length,
262
+ ),
263
+ indentation_level: box.indentation_level
264
+ )
265
+ end
266
+
267
+ def with_subset_of_lines_elided(lines, range:, indentation_level:)
268
+ with_slice_of_array_replaced(
269
+ lines,
270
+ range,
271
+ Elision.new(
272
+ indentation_level: indentation_level,
273
+ children: lines[range].map(&:as_elided),
274
+ ),
275
+ )
276
+ end
277
+
278
+ def maximum
279
+ SuperDiff.configuration.diff_elision_maximum || 0
280
+ end
281
+
282
+ class BuildPanes
283
+ extend AttrExtras.mixin
284
+
285
+ method_object [:dirty_panes!, :lines!]
286
+
287
+ def call
288
+ beginning + middle + ending
289
+ end
290
+
291
+ private
292
+
293
+ def beginning
294
+ if (
295
+ dirty_panes.empty? ||
296
+ dirty_panes.first.range.begin == 0
297
+ )
298
+ []
299
+ else
300
+ [
301
+ Pane.new(
302
+ type: :clean,
303
+ range: Range.new(
304
+ 0,
305
+ dirty_panes.first.range.begin - 1
306
+ )
307
+ )
308
+ ]
309
+ end
310
+ end
311
+
312
+ def middle
313
+ if dirty_panes.size == 1
314
+ dirty_panes
315
+ else
316
+ dirty_panes.
317
+ each_with_index.
318
+ each_cons(2).
319
+ reduce([]) do |panes, ((pane1, _), (pane2, index2))|
320
+ panes +
321
+ [
322
+ pane1,
323
+ Pane.new(
324
+ type: :clean,
325
+ range: Range.new(
326
+ pane1.range.end + 1,
327
+ pane2.range.begin - 1,
328
+ )
329
+ )
330
+ ] + (
331
+ index2 == dirty_panes.size - 1 ?
332
+ [pane2] :
333
+ []
334
+ )
335
+ end
336
+ end
337
+ end
338
+
339
+ def ending
340
+ if (
341
+ dirty_panes.empty? ||
342
+ dirty_panes.last.range.end >= lines.size - 1
343
+ )
344
+ []
345
+ else
346
+ [
347
+ Pane.new(
348
+ type: :clean,
349
+ range: Range.new(
350
+ dirty_panes.last.range.end + 1,
351
+ lines.size - 1
352
+ )
353
+ )
354
+ ]
355
+ end
356
+ end
357
+ end
358
+
359
+ class Pane
360
+ extend AttrExtras.mixin
361
+
362
+ rattr_initialize [:type!, :range!]
363
+
364
+ def extended_to(new_end)
365
+ self.class.new(type: type, range: range.begin..new_end)
366
+ end
367
+
368
+ def padded
369
+ self.class.new(
370
+ type: type,
371
+ range: Range.new(range.begin, range.end)
372
+ )
373
+ end
374
+
375
+ def capped_to(beginning, ending)
376
+ new_beginning = range.begin < beginning ? beginning : range.begin
377
+ new_ending = range.end > ending ? ending : range.end
378
+ self.class.new(
379
+ type: type,
380
+ range: Range.new(new_beginning, new_ending),
381
+ )
382
+ end
383
+ end
384
+
385
+ class BuildBoxes
386
+ def self.call(lines)
387
+ builder = new(lines)
388
+ builder.build
389
+ builder.final_boxes
390
+ end
391
+
392
+ attr_reader :final_boxes
393
+
394
+ def initialize(lines)
395
+ @lines = lines
396
+
397
+ @open_collection_boxes = []
398
+ @final_boxes = []
399
+ end
400
+
401
+ def build
402
+ lines.each_with_index do |line, index|
403
+ if line.opens_collection?
404
+ open_new_collection_box(line, index)
405
+ elsif line.closes_collection?
406
+ extend_working_collection_box(index)
407
+ close_working_collection_box
408
+ else
409
+ extend_working_collection_box(index) if open_collection_boxes.any?
410
+ record_item_box(line, index)
411
+ end
412
+ end
413
+ end
414
+
415
+ private
416
+
417
+ attr_reader :lines, :open_collection_boxes
418
+
419
+ def extend_working_collection_box(index)
420
+ open_collection_boxes.last.extend_to(index)
421
+ end
422
+
423
+ def close_working_collection_box
424
+ final_boxes << open_collection_boxes.pop
425
+ end
426
+
427
+ def open_new_collection_box(line, index)
428
+ open_collection_boxes << Box.new(
429
+ indentation_level: line.indentation_level,
430
+ range: index..index,
431
+ )
432
+ end
433
+
434
+ def record_item_box(line, index)
435
+ final_boxes << Box.new(
436
+ indentation_level: line.indentation_level,
437
+ range: index..index,
438
+ )
439
+ end
440
+ end
441
+
442
+ class Box
443
+ extend AttrExtras.mixin
444
+
445
+ rattr_initialize [:indentation_level!, :range!]
446
+
447
+ def fully_contains?(other)
448
+ range.begin <= other.range.begin && range.end >= other.range.end
449
+ end
450
+
451
+ def fits_fully_within?(other)
452
+ other.range.begin <= range.begin && other.range.end >= range.end
453
+ end
454
+
455
+ def extended_to(new_end)
456
+ dup.tap { |clone| clone.extend_to(new_end) }
457
+ end
458
+
459
+ def extend_to(new_end)
460
+ @range = range.begin..new_end
461
+ end
462
+ end
463
+
464
+ class Elision
465
+ extend AttrExtras.mixin
466
+
467
+ rattr_initialize [:indentation_level!, :children!]
468
+
469
+ def type
470
+ :elision
471
+ end
472
+
473
+ def prefix
474
+ ""
475
+ end
476
+
477
+ def value
478
+ "# ..."
479
+ end
480
+
481
+ def elided?
482
+ true
483
+ end
484
+
485
+ def add_comma?
486
+ false
487
+ end
488
+ end
489
+ end
490
+ end