Class: Psych::Merge::DiffMapper

Inherits:
Ast::Merge::DiffMapperBase
  • Object
show all
Defined in:
lib/psych/merge/diff_mapper.rb

Overview

Maps unified git diffs to YAML AST paths.

DiffMapper parses unified diffs and maps changed lines to their
corresponding YAML key paths (e.g., [“AllCops”, “Exclude”]).

Examples:

Basic usage

mapper = DiffMapper.new
mappings = mapper.map(diff_text, original_yaml)

mappings.each do |mapping|
  puts "Path: #{mapping.path.join('.')}"
  puts "Operation: #{mapping.operation}"
end

See Also:

  • Ast::Merge::DiffMapperBase

Instance Method Summary collapse

Instance Method Details

#create_analysis(content) ⇒ FileAnalysis

Create a FileAnalysis for the original YAML content.

Parameters:

  • content (String)

    The original YAML content

Returns:



25
26
27
# File 'lib/psych/merge/diff_mapper.rb', line 25

def create_analysis(content)
  FileAnalysis.new(content)
end

#map_hunk_to_paths(hunk, original_analysis) ⇒ Array<DiffMapping>

Map a diff hunk to YAML key paths.

Parameters:

  • hunk (DiffHunk)

    The hunk to map

  • original_analysis (FileAnalysis)

    Analysis of the original YAML

Returns:

  • (Array<DiffMapping>)

    Mappings for this hunk



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/psych/merge/diff_mapper.rb', line 34

def map_hunk_to_paths(hunk, original_analysis)
  mappings = []

  # Group consecutive changed lines by their containing node
  path_groups = group_lines_by_path(hunk, original_analysis)

  path_groups.each do |path, lines|
    mappings << DiffMapping.new(
      path: path,
      operation: determine_operation_for_lines(lines),
      lines: lines,
      hunk: hunk,
    )
  end

  mappings
end