@@ -120,18 +120,18 @@ def try_apply_fix_to_file(owner, repo, file_path, before_code, after_code)
120120 # Try direct replacement if before_code is provided
121121 if before_code . present?
122122 direct_result = try_direct_replacement ( current_content , before_code , after_code , 1 )
123- if direct_result && direct_result [ " replacements" ] . present?
123+ if direct_result && direct_result [ : replacements] . present?
124124 Rails . logger . info "[SimpleFixApplier] Direct replacement succeeded for #{ normalized_path } "
125125
126126 # Apply the replacements
127127 lines = current_content . lines
128- direct_result [ " replacements" ] . sort_by { |r | -r [ " line" ] } . each do |replacement |
129- line_idx = replacement [ " line" ] - 1
128+ direct_result [ : replacements] . sort_by { |r | -r [ : line] } . each do |replacement |
129+ line_idx = replacement [ : line] - 1
130130 next if line_idx < 0 || line_idx >= lines . size
131131
132132 original_indent = lines [ line_idx ] . match ( /^(\s *)/ ) [ 1 ]
133133 line_ending = lines [ line_idx ] . end_with? ( "\n " ) ? "\n " : ""
134- new_stripped = replacement [ " new" ] . strip
134+ new_stripped = replacement [ : new] . strip
135135 lines [ line_idx ] = "#{ original_indent } #{ new_stripped } #{ line_ending } "
136136 end
137137 new_content = lines . join
@@ -387,11 +387,15 @@ def try_direct_replacement(file_content, before_code, after_code, error_line)
387387 # Preserve original indentation from file
388388 original_indent = lines [ match_start + idx ] &.match ( /^(\s *)/ ) &.[]( 1 ) || ""
389389 new_content = original_indent + new_line . lstrip
390+ old_content = lines [ match_start + idx ] &.chomp || old_line
391+
392+ # Only include replacement if old != new (skip unchanged lines)
393+ next if old_content . strip == new_content . strip
390394
391395 replacements << {
392- " line" => line_num ,
393- " old" => lines [ match_start + idx ] &. chomp || old_line ,
394- " new" => new_content . chomp
396+ line : line_num ,
397+ old : old_content ,
398+ new : new_content . chomp
395399 }
396400 end
397401
@@ -402,14 +406,16 @@ def try_direct_replacement(file_content, before_code, after_code, error_line)
402406 insert_after = match_start + before_lines . size + idx
403407 original_indent = lines [ match_start ] &.match ( /^(\s *)/ ) &.[]( 1 ) || ""
404408 insertions << {
405- " after_line" => insert_after ,
406- " content" => original_indent + new_line . lstrip . chomp
409+ after_line : insert_after ,
410+ content : original_indent + new_line . lstrip . chomp
407411 }
408412 end
409- return { " replacements" => replacements , " insertions" => insertions } if insertions . any?
413+ return { replacements : replacements , insertions : insertions } if insertions . any?
410414 end
411415
412- { "replacements" => replacements }
416+ return nil if replacements . empty?
417+
418+ { replacements : replacements }
413419 rescue => e
414420 Rails . logger . warn "[SimpleFixApplier] Direct replacement failed: #{ e . message } "
415421 nil
@@ -609,9 +615,9 @@ def apply_line_replacements(lines, fix_instructions)
609615
610616 # First, apply insertions (in reverse order to maintain line numbers)
611617 if fix_instructions [ :insertions ] . present?
612- fix_instructions [ :insertions ] . sort_by { |i | -i [ "after_line" ] } . each do |insertion |
613- after_idx = insertion [ "after_line" ] # 1-indexed, insert after this line
614- content = insertion [ "content" ]
618+ fix_instructions [ :insertions ] . sort_by { |i | -( i [ :after_line ] || i [ "after_line" ] ) } . each do |insertion |
619+ after_idx = insertion [ :after_line ] || insertion [ "after_line" ] # 1-indexed, insert after this line
620+ content = insertion [ :content ] || insertion [ "content" ]
615621
616622 next if after_idx < 0 || after_idx > new_lines . size
617623
@@ -643,10 +649,11 @@ def apply_line_replacements(lines, fix_instructions)
643649 end
644650
645651 # Then, apply replacements
646- ( fix_instructions [ :replacements ] || [ ] ) . sort_by { |r | -r [ "line" ] } . each do |replacement |
647- line_idx = replacement [ "line" ] - 1
648- old_content = replacement [ "old" ]
649- new_content = replacement [ "new" ]
652+ ( fix_instructions [ :replacements ] || [ ] ) . sort_by { |r | -( r [ :line ] || r [ "line" ] ) } . each do |replacement |
653+ line_num = replacement [ :line ] || replacement [ "line" ]
654+ line_idx = line_num - 1
655+ old_content = replacement [ :old ] || replacement [ "old" ]
656+ new_content = replacement [ :new ] || replacement [ "new" ]
650657
651658 next if line_idx < 0 || line_idx >= new_lines . size
652659
@@ -668,9 +675,9 @@ def apply_line_replacements(lines, fix_instructions)
668675 end
669676
670677 changes_made += 1
671- Rails . logger . info "[SimpleFixApplier] Replaced line #{ replacement [ 'line' ] } : #{ old_content . strip [ 0 ..50 ] } -> #{ new_stripped [ 0 ..50 ] } "
678+ Rails . logger . info "[SimpleFixApplier] Replaced line #{ line_num } : #{ old_content . strip [ 0 ..50 ] } -> #{ new_stripped [ 0 ..50 ] } "
672679 else
673- Rails . logger . warn "[SimpleFixApplier] Line #{ replacement [ 'line' ] } mismatch:"
680+ Rails . logger . warn "[SimpleFixApplier] Line #{ line_num } mismatch:"
674681 Rails . logger . warn " Expected: #{ old_content . inspect } "
675682 Rails . logger . warn " Actual: #{ current_line . inspect } "
676683
@@ -681,7 +688,7 @@ def apply_line_replacements(lines, fix_instructions)
681688 line_ending = current_line . end_with? ( "\n " ) ? "\n " : ""
682689 new_lines [ line_idx ] = "#{ original_indent } #{ new_stripped } #{ line_ending } "
683690 changes_made += 1
684- Rails . logger . info "[SimpleFixApplier] Fuzzy match succeeded for line #{ replacement [ 'line' ] } "
691+ Rails . logger . info "[SimpleFixApplier] Fuzzy match succeeded for line #{ line_num } "
685692 end
686693 end
687694 end
0 commit comments