Class: Ast::Merge::StructuralEdit::SplicePlan
- Inherits:
-
Object
- Object
- Ast::Merge::StructuralEdit::SplicePlan
- Defined in:
- lib/ast/merge/structural_edit/splice_plan.rb
Overview
Passive plan for a contiguous structural splice.
The first shared primitive models exact line-range replacement: keep the original source untouched outside the replaced window and substitute only the requested structural range. This avoids separator surgery in callers such as PartialTemplateMergerBase and gives future remove/rehome work a stable place to grow richer ownership-transfer rules.
Instance Attribute Summary collapse
- #leading_boundary ⇒ String, ... readonly
- #metadata ⇒ String, ... readonly
- #preserve_removed_trailing_blank_lines ⇒ String, ... readonly
- #replace_end_line ⇒ String, ... readonly
- #replace_start_line ⇒ String, ... readonly
- #replacement ⇒ String, ... readonly
- #source ⇒ String, ... readonly
- #trailing_boundary ⇒ String, ... readonly
Instance Method Summary collapse
-
#after_content ⇒ String
Return content after the replaced range.
-
#apply_to(alternate_source = source) ⇒ String
Apply the splice to the original source or a compatible alternate source.
-
#before_content ⇒ String
Return content before the replaced range.
- #changed? ⇒ Boolean
-
#initialize(source:, replacement:, replace_start_line:, replace_end_line:, leading_boundary: nil, trailing_boundary: nil, preserve_removed_trailing_blank_lines: true, metadata: {}, **options) ⇒ SplicePlan
constructor
Build a plan for exact line-range replacement.
-
#inspect ⇒ String
Return a concise debug representation of the splice plan.
-
#line_chunks ⇒ Array<String>
Return the source split into line chunks.
-
#line_range ⇒ Range
Return the replaced line range.
-
#merged_content ⇒ String
Return merged content after applying the splice.
-
#removed_content ⇒ String
Return the content removed by the splice.
-
#to_splice_plan ⇒ SplicePlan
Return self so splice-compatible plans share a common adapter surface.
Constructor Details
#initialize(source:, replacement:, replace_start_line:, replace_end_line:, leading_boundary: nil, trailing_boundary: nil, preserve_removed_trailing_blank_lines: true, metadata: {}, **options) ⇒ SplicePlan
Build a plan for exact line-range replacement.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 42 def initialize(source:, replacement:, replace_start_line:, replace_end_line:, leading_boundary: nil, trailing_boundary: nil, preserve_removed_trailing_blank_lines: true, metadata: {}, **) @source = source.to_s @replacement = replacement.to_s @replace_start_line = Integer(replace_start_line) @replace_end_line = Integer(replace_end_line) @leading_boundary = leading_boundary @trailing_boundary = trailing_boundary @preserve_removed_trailing_blank_lines = preserve_removed_trailing_blank_lines @metadata = .merge().freeze validate_range! end |
Instance Attribute Details
#leading_boundary ⇒ String, ... (readonly)
22 23 24 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 22 def leading_boundary @leading_boundary end |
#metadata ⇒ String, ... (readonly)
22 23 24 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 22 def @metadata end |
#preserve_removed_trailing_blank_lines ⇒ String, ... (readonly)
22 23 24 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 22 def preserve_removed_trailing_blank_lines @preserve_removed_trailing_blank_lines end |
#replace_end_line ⇒ String, ... (readonly)
22 23 24 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 22 def replace_end_line @replace_end_line end |
#replace_start_line ⇒ String, ... (readonly)
22 23 24 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 22 def replace_start_line @replace_start_line end |
#replacement ⇒ String, ... (readonly)
22 23 24 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 22 def replacement @replacement end |
#source ⇒ String, ... (readonly)
22 23 24 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 22 def source @source end |
#trailing_boundary ⇒ String, ... (readonly)
22 23 24 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 22 def trailing_boundary @trailing_boundary end |
Instance Method Details
#after_content ⇒ String
Return content after the replaced range.
87 88 89 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 87 def after_content line_chunks[replace_end_line..].to_a.join end |
#apply_to(alternate_source = source) ⇒ String
Apply the splice to the original source or a compatible alternate source.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 113 def apply_to(alternate_source = source) alternate_text = alternate_source.to_s return merged_content if alternate_text == source self.class.new( source: alternate_text, replacement: replacement, replace_start_line: replace_start_line, replace_end_line: replace_end_line, leading_boundary: leading_boundary, trailing_boundary: trailing_boundary, metadata: ).merged_content end |
#before_content ⇒ String
Return content before the replaced range.
66 67 68 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 66 def before_content line_chunks[0...(replace_start_line - 1)].to_a.join end |
#changed? ⇒ Boolean
98 99 100 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 98 def changed? merged_content != source end |
#inspect ⇒ String
Return a concise debug representation of the splice plan.
131 132 133 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 131 def inspect "#<#{self.class.name} lines=#{replace_start_line}..#{replace_end_line} changed=#{changed?}>" end |
#line_chunks ⇒ Array<String>
Return the source split into line chunks.
59 60 61 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 59 def line_chunks @line_chunks ||= source.lines end |
#line_range ⇒ Range
Return the replaced line range.
73 74 75 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 73 def line_range replace_start_line..replace_end_line end |
#merged_content ⇒ String
Return merged content after applying the splice.
94 95 96 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 94 def merged_content +before_content + replacement_with_preserved_boundary_layout + after_content end |
#removed_content ⇒ String
Return the content removed by the splice.
80 81 82 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 80 def removed_content line_chunks[(replace_start_line - 1)..(replace_end_line - 1)].to_a.join end |
#to_splice_plan ⇒ SplicePlan
Return self so splice-compatible plans share a common adapter surface.
105 106 107 |
# File 'lib/ast/merge/structural_edit/splice_plan.rb', line 105 def to_splice_plan self end |