Class: Rbs::Merge::SmartMerger

Inherits:
Ast::Merge::SmartMergerBase
  • Object
show all
Includes:
Ast::Merge::CommentLayoutEmissionSupport
Defined in:
lib/rbs/merge/smart_merger.rb

Overview

Orchestrates the smart merge process for RBS type signature files. Uses FileAnalysis, FileAligner, ConflictResolver, and MergeResult to merge two RBS files intelligently.

SmartMerger provides flexible configuration for different merge scenarios. When matching class or module definitions are found in both files, the merger can perform recursive merging of their members.

Examples:

Basic merge (destination customizations preserved)

merger = SmartMerger.new(template_content, dest_content)
result = merger.merge

Template updates win

merger = SmartMerger.new(
  template_content,
  dest_content,
  preference: :template,
  add_template_only_nodes: true
)
result = merger.merge

Custom signature matching

sig_gen = ->(node) { [:decl, node.name.to_s] }
merger = SmartMerger.new(
  template_content,
  dest_content,
  signature_generator: sig_gen
)

With node_typing for per-node-type preferences

merger = SmartMerger.new(template, dest,
  node_typing: { "ClassDecl" => ->(n) { NodeTyping.with_merge_type(n, :model) } },
  preference: { default: :destination, model: :template })

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_content, dest_content, signature_generator: nil, preference: :destination, add_template_only_nodes: false, remove_template_missing_nodes: false, corruption_handling: :heal, freeze_token: nil, match_refiner: nil, regions: nil, region_placeholder: nil, node_typing: nil, max_recursion_depth: Float::INFINITY, **options) ⇒ SmartMerger

Creates a new SmartMerger for intelligent RBS file merging.

Parameters:

  • template_content (String)

    Template RBS source code

  • dest_content (String)

    Destination RBS source code

  • signature_generator (Proc, nil) (defaults to: nil)

    Optional proc to generate custom node signatures. The proc receives an RBS declaration and should return one of:

    • An array representing the node's signature
    • nil to indicate the node should have no signature
    • The original node to fall through to default signature computation
  • preference (Symbol, Hash) (defaults to: :destination)

    Controls which version to use when nodes have matching signatures but different content:

    • :destination (default) - Use destination version (preserves customizations)
    • :template - Use template version (applies updates)
    • Hash for per-type preferences
  • add_template_only_nodes (Boolean) (defaults to: false)

    Controls whether to add nodes that only exist in template:

    • false (default) - Skip template-only nodes
    • true - Add template-only nodes to result
  • remove_template_missing_nodes (Boolean) (defaults to: false)

    Controls whether to remove destination-only declarations while promoting their leading comments

  • corruption_handling (Symbol) (defaults to: :heal)

    How to handle detected historical duplicate-prefix corruption (:heal, :warn, :error, :skip)

  • freeze_token (String) (defaults to: nil)

    Token to use for freeze block markers. Default: "rbs-merge" (looks for # rbs-merge:freeze / # rbs-merge:unfreeze)

  • match_refiner (#call, nil) (defaults to: nil)

    Match refiner for fuzzy matching

  • regions (Array<Hash>, nil) (defaults to: nil)

    Region configurations for nested merging

  • region_placeholder (String, nil) (defaults to: nil)

    Custom placeholder for regions

  • node_typing (Hash{Symbol,String => #call}, nil) (defaults to: nil)

    Node typing configuration

  • max_recursion_depth (Integer, Float) (defaults to: Float::INFINITY)

    Maximum depth for recursive body merging. Default: Float::INFINITY (no limit)

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/rbs/merge/smart_merger.rb', line 87

def initialize(
  template_content,
  dest_content,
  signature_generator: nil,
  preference: :destination,
  add_template_only_nodes: false,
  remove_template_missing_nodes: false,
  corruption_handling: :heal,
  freeze_token: nil,
  match_refiner: nil,
  regions: nil,
  region_placeholder: nil,
  node_typing: nil,
  max_recursion_depth: Float::INFINITY,
  **options
)
  @max_recursion_depth = max_recursion_depth
  @remove_template_missing_nodes = remove_template_missing_nodes
  @corruption_handling = ::Ast::Merge::Healer.normalize_mode(corruption_handling)
  super(
    template_content,
    dest_content,
    signature_generator: signature_generator,
    preference: preference,
    add_template_only_nodes: add_template_only_nodes,
    remove_template_missing_nodes: remove_template_missing_nodes,
    freeze_token: freeze_token,
    match_refiner: match_refiner,
    regions: regions,
    region_placeholder: region_placeholder,
    node_typing: node_typing,
    **options
  )
end

Instance Attribute Details

#corruption_handlingObject (readonly)

Returns the value of attribute corruption_handling.



46
47
48
# File 'lib/rbs/merge/smart_merger.rb', line 46

def corruption_handling
  @corruption_handling
end