Class: Canon::Diff::DiffNodeEnricher

Inherits:
Object
  • Object
show all
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<DiffCharRange> 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

Instance Method Summary collapse

Constructor Details

#initialize(diff_nodes, text1, text2) ⇒ DiffNodeEnricher

Returns a new instance of DiffNodeEnricher.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/canon/diff/diff_node_enricher.rb', line 36

def initialize(diff_nodes, text1, text2)
  @diff_nodes = diff_nodes
  @text1 = text1
  @text2 = text2
  @line_map1 = SourceLocator.build_line_map(text1)
  @line_map2 = SourceLocator.build_line_map(text2)
  @lines1 = text1.split("\n")
  @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)
end

Class Method Details

.build(diff_nodes, text1, text2) ⇒ Array<DiffNode>

Enrich DiffNodes with character position data.

Parameters:

  • diff_nodes (Array<DiffNode>)

    The semantic differences

  • text1 (String)

    The first document (preprocessed)

  • text2 (String)

    The second document (preprocessed)

Returns:

  • (Array<DiffNode>)

    The same DiffNodes, enriched in place



29
30
31
32
33
34
# File 'lib/canon/diff/diff_node_enricher.rb', line 29

def self.build(diff_nodes, text1, text2)
  return diff_nodes if diff_nodes.nil? || diff_nodes.empty?
  return diff_nodes if text1.nil? || text2.nil?

  new(diff_nodes, text1, text2).enrich
end

Instance Method Details

#enrichObject



49
50
51
52
53
54
# File 'lib/canon/diff/diff_node_enricher.rb', line 49

def enrich
  @diff_nodes.each do |diff_node|
    enrich_node(diff_node)
  end
  @diff_nodes
end