Class: Prism::Merge::SmartMerger
- Inherits:
-
Object
- Object
- Prism::Merge::SmartMerger
- Defined in:
- lib/prism/merge/smart_merger.rb
Overview
Orchestrates the smart merge process using FileAnalysis, FileAligner, ConflictResolver, and MergeResult to merge two Ruby files intelligently.
SmartMerger provides flexible configuration for different merge scenarios. When matching class or module definitions are found in both files, the merger automatically performs recursive merging of their bodies, intelligently combining nested methods, constants, and other definitions.
Instance Attribute Summary collapse
-
#aligner ⇒ FileAligner
readonly
Aligner for finding matches and differences.
-
#dest_analysis ⇒ FileAnalysis
readonly
Analysis of the destination file.
-
#resolver ⇒ ConflictResolver
readonly
Resolver for handling conflicting content.
-
#result ⇒ MergeResult
readonly
Result object tracking merged content.
-
#template_analysis ⇒ FileAnalysis
readonly
Analysis of the template file.
Instance Method Summary collapse
-
#initialize(template_content, dest_content, signature_generator: nil, signature_match_preference: :destination, add_template_only_nodes: false, freeze_token: FileAnalysis::DEFAULT_FREEZE_TOKEN, max_recursion_depth: Float::INFINITY, current_depth: 0) ⇒ SmartMerger
constructor
Creates a new SmartMerger for intelligent Ruby file merging.
-
#merge ⇒ String
Performs the intelligent merge of template and destination files.
-
#merge_with_debug ⇒ Hash
Performs merge and returns detailed debug information.
Constructor Details
#initialize(template_content, dest_content, signature_generator: nil, signature_match_preference: :destination, add_template_only_nodes: false, freeze_token: FileAnalysis::DEFAULT_FREEZE_TOKEN, max_recursion_depth: Float::INFINITY, current_depth: 0) ⇒ SmartMerger
Creates a new SmartMerger for intelligent Ruby file merging.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/prism/merge/smart_merger.rb', line 151 def initialize(template_content, dest_content, signature_generator: nil, signature_match_preference: :destination, add_template_only_nodes: false, freeze_token: FileAnalysis::DEFAULT_FREEZE_TOKEN, max_recursion_depth: Float::INFINITY, current_depth: 0) @template_content = template_content @dest_content = dest_content @signature_match_preference = signature_match_preference @add_template_only_nodes = add_template_only_nodes @freeze_token = freeze_token @max_recursion_depth = max_recursion_depth @current_depth = current_depth @template_analysis = FileAnalysis.new(template_content, signature_generator: signature_generator, freeze_token: freeze_token) @dest_analysis = FileAnalysis.new(dest_content, signature_generator: signature_generator, freeze_token: freeze_token) @aligner = FileAligner.new(@template_analysis, @dest_analysis) @resolver = ConflictResolver.new( @template_analysis, @dest_analysis, signature_match_preference: signature_match_preference, add_template_only_nodes: add_template_only_nodes, ) @result = MergeResult.new end |
Instance Attribute Details
#aligner ⇒ FileAligner (readonly)
Returns Aligner for finding matches and differences.
57 58 59 |
# File 'lib/prism/merge/smart_merger.rb', line 57 def aligner @aligner end |
#dest_analysis ⇒ FileAnalysis (readonly)
Returns Analysis of the destination file.
54 55 56 |
# File 'lib/prism/merge/smart_merger.rb', line 54 def dest_analysis @dest_analysis end |
#resolver ⇒ ConflictResolver (readonly)
Returns Resolver for handling conflicting content.
60 61 62 |
# File 'lib/prism/merge/smart_merger.rb', line 60 def resolver @resolver end |
#result ⇒ MergeResult (readonly)
Returns Result object tracking merged content.
63 64 65 |
# File 'lib/prism/merge/smart_merger.rb', line 63 def result @result end |
#template_analysis ⇒ FileAnalysis (readonly)
Returns Analysis of the template file.
51 52 53 |
# File 'lib/prism/merge/smart_merger.rb', line 51 def template_analysis @template_analysis end |
Instance Method Details
#merge ⇒ String
Performs the intelligent merge of template and destination files.
The merge process:
-
Validates both files for syntax errors
-
Finds anchors (matching sections) and boundaries (differences)
-
Processes anchors and boundaries in order
-
Returns merged content as a string
Merge behavior is controlled by constructor parameters:
-
‘signature_match_preference`: Which version wins for matching nodes
-
‘add_template_only_nodes`: Whether to add template-only content
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/prism/merge/smart_merger.rb', line 202 def merge # Handle invalid files unless @template_analysis.valid? raise Prism::Merge::TemplateParseError.new( "Template file has parsing errors", content: @template_content, parse_result: @template_analysis.parse_result, ) end unless @dest_analysis.valid? raise Prism::Merge::DestinationParseError.new( "Destination file has parsing errors", content: @dest_content, parse_result: @dest_analysis.parse_result, ) end # Find anchors and boundaries boundaries = @aligner.align # Process the merge by walking through anchors and boundaries in order process_merge(boundaries) # Return final content @result.to_s end |
#merge_with_debug ⇒ Hash
Performs merge and returns detailed debug information.
This method provides comprehensive information about merge decisions, useful for debugging, testing, and understanding merge behavior.
259 260 261 262 263 264 265 266 |
# File 'lib/prism/merge/smart_merger.rb', line 259 def merge_with_debug content = merge { content: content, debug: @result.debug_output, statistics: @result.statistics, } end |