Class: Canon::Diff::DiffNodeEnricher
- Inherits:
-
Object
- Object
- Canon::Diff::DiffNodeEnricher
- Defined in:
- lib/canon/diff/diff_node_enricher.rb
Overview
Enriches DiffNodes with character position data (DiffCharRanges).
This is Phase 1 of the two-phase diff pipeline. It runs after comparison and before rendering. It CAN use string operations (including LCS) on serialized content to determine character-level change positions.
The output is DiffNodes enriched with:
- char_ranges: Array
mapping changes to specific line/columns - line_range_before: [start_line, end_line] in text1
- line_range_after: [start_line, end_line] in text2
Phase 2 (DiffLineBuilder) then assembles DiffLines from these enriched DiffNodes without any further computation.
Class Method Summary collapse
-
.build(diff_nodes, text1, text2, lines1: nil, lines2: nil) ⇒ Array<DiffNode>
Enrich DiffNodes with character position data.
Instance Method Summary collapse
- #enrich ⇒ Object
-
#initialize(diff_nodes, text1, text2, lines1 = nil, lines2 = nil) ⇒ DiffNodeEnricher
constructor
A new instance of DiffNodeEnricher.
Constructor Details
#initialize(diff_nodes, text1, text2, lines1 = nil, lines2 = nil) ⇒ DiffNodeEnricher
Returns a new instance of DiffNodeEnricher.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/canon/diff/diff_node_enricher.rb', line 32 def initialize(diff_nodes, text1, text2, lines1 = nil, lines2 = nil) @diff_nodes = diff_nodes @text1 = text1 @text2 = text2 @line_map1 = SourceLocator.build_line_map(text1) @line_map2 = SourceLocator.build_line_map(text2) # Shared split — see DiffLineBuilder's note. @lines1 = lines1 || text1.split("\n") @lines2 = lines2 || text2.split("\n") # Track occurrences for text_content dimension to find correct element instance @text_occurrence1 = Hash.new(0) @text_occurrence2 = Hash.new(0) # Opener-offset caches: one scan per unique element name per # document replaces the per-call regex walk (which was also # quadratic on repeated element names). @opener_offsets1 = {} @opener_offsets2 = {} end |
Class Method Details
.build(diff_nodes, text1, text2, lines1: nil, lines2: nil) ⇒ Array<DiffNode>
Enrich DiffNodes with character position data.
25 26 27 28 29 30 |
# File 'lib/canon/diff/diff_node_enricher.rb', line 25 def self.build(diff_nodes, text1, text2, lines1: nil, lines2: nil) return diff_nodes if diff_nodes.nil? || diff_nodes.empty? return diff_nodes if text1.nil? || text2.nil? new(diff_nodes, text1, text2, lines1, lines2).enrich end |
Instance Method Details
#enrich ⇒ Object
51 52 53 54 55 56 |
# File 'lib/canon/diff/diff_node_enricher.rb', line 51 def enrich @diff_nodes.each do |diff_node| enrich_node(diff_node) end @diff_nodes end |