super_diff 0.11.0 → 0.12.1

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