Class: Ast::Merge::FileAlignerBase

Inherits:
Object
  • Object
show all
Includes:
TrailingGroups::AlignmentSort
Defined in:
lib/ast/merge/file_aligner_base.rb

Overview

Shared signature-based alignment pipeline for format-specific file aligners.

Concrete aligners typically customize only:

  • entry payload keys (template_node / dest_node, template_decl / dest_decl, ...)
  • extra signature aliases for special wrapper nodes
  • template-only positioning metadata
  • optional fuzzy match refinement
  • sort-key overrides via TrailingGroups::AlignmentSort

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TrailingGroups::AlignmentSort

#dest_only_sort_key, #match_sort_key, #sort_alignment_with_template_position, #template_only_sort_key

Constructor Details

#initialize(template_analysis, dest_analysis, match_refiner: nil, **_options) ⇒ FileAlignerBase

Returns a new instance of FileAlignerBase.



18
19
20
21
22
# File 'lib/ast/merge/file_aligner_base.rb', line 18

def initialize(template_analysis, dest_analysis, match_refiner: nil, **_options)
  @template_analysis = template_analysis
  @dest_analysis = dest_analysis
  @match_refiner = match_refiner
end

Instance Attribute Details

#dest_analysisObject (readonly)

Returns the value of attribute dest_analysis.



16
17
18
# File 'lib/ast/merge/file_aligner_base.rb', line 16

def dest_analysis
  @dest_analysis
end

#match_refinerObject (readonly)

Returns the value of attribute match_refiner.



16
17
18
# File 'lib/ast/merge/file_aligner_base.rb', line 16

def match_refiner
  @match_refiner
end

#template_analysisObject (readonly)

Returns the value of attribute template_analysis.



16
17
18
# File 'lib/ast/merge/file_aligner_base.rb', line 16

def template_analysis
  @template_analysis
end

Instance Method Details

#alignArray<Hash>

Align template and destination statements into match/template-only/dest-only entries.

Returns:

  • (Array<Hash>)

    ordered alignment entries consumed by conflict resolvers



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ast/merge/file_aligner_base.rb', line 27

def align
  template_statements = statements_for(template_analysis)
  dest_statements = statements_for(dest_analysis)

  template_by_sig = build_signature_map(template_statements, template_analysis)
  dest_by_sig = build_signature_map(dest_statements, dest_analysis)

  matched_template = Set.new
  matched_dest = Set.new
  alignment = []

  template_by_sig.each do |sig, template_indices|
    next unless dest_by_sig.key?(sig)

    dest_indices = dest_by_sig[sig]

    template_indices.zip(dest_indices).each do |t_idx, d_idx|
      next unless t_idx && d_idx
      next if matched_template.include?(t_idx) || matched_dest.include?(d_idx)

      alignment << build_match_entry(
        signature: sig,
        template_index: t_idx,
        dest_index: d_idx,
        template_statement: template_statements[t_idx],
        dest_statement: dest_statements[d_idx]
      )

      matched_template << t_idx
      matched_dest << d_idx
    end
  end

  apply_match_refiner!(
    alignment,
    template_statements: template_statements,
    dest_statements: dest_statements,
    matched_template: matched_template,
    matched_dest: matched_dest
  )

  matched_entries_by_template_position = alignment
                                         .select { |entry| entry[:type] == :match }
                                         .sort_by do |entry|
    [
      entry[:template_index], entry[:dest_index]
    ]
  end

  template_statements.each_with_index do |statement, idx|
    next if matched_template.include?(idx)

    alignment << build_template_only_entry(
      template_index: idx,
      template_statement: statement,
      matched_entries_by_template_position: matched_entries_by_template_position
    )
  end

  dest_statements.each_with_index do |statement, idx|
    next if matched_dest.include?(idx)

    alignment << build_dest_only_entry(
      dest_index: idx,
      dest_statement: statement
    )
  end

  sort_alignment(alignment)
  log_alignment(alignment)
  alignment
end