Class: Ast::Merge::StructuralEdit::RehomePlan
- Inherits:
-
Object
- Object
- Ast::Merge::StructuralEdit::RehomePlan
- Defined in:
- lib/ast/merge/structural_edit/rehome_plan.rb
Overview
Passive metadata for promoting preserved comment/layout fragments from a removed owner to a surviving adjacent boundary owner.
A rehome plan does not mutate attachments in place. It captures which fragments should survive a removal and how they should be re-exposed on the surviving side so emitters / downstream mergers can adopt the shared contract incrementally.
Instance Method Summary collapse
-
#comment_attachment ⇒ Ast::Merge::Comment::Attachment
Materialize the promoted comment regions as a shared attachment.
-
#comment_regions ⇒ Array<Comment::Region>
Return promoted comment regions to attach on the target side.
-
#edge ⇒ Symbol
Return the target edge receiving fragments.
- #empty? ⇒ Boolean
-
#initialize(target_boundary:, source_owner: nil, comment_regions: [], layout_gaps: [], metadata: {}, **options) ⇒ RehomePlan
constructor
:reek:LongParameterList Build a rehome plan for preserved fragments from a removed owner.
-
#inspect ⇒ String
Return a concise debug representation of the rehome plan.
-
#layout_attachment ⇒ Ast::Merge::Layout::Attachment
Materialize the promoted layout gaps as a shared attachment.
-
#layout_gaps ⇒ Array<Layout::Gap>
Return promoted layout gaps to attach on the target side.
- #leading? ⇒ Boolean
-
#metadata ⇒ Hash
Return metadata describing how the rehome plan was constructed.
-
#source_owner ⇒ Object?
Return the owner losing fragments during the rehome operation.
-
#target_boundary ⇒ Boundary
Return the surviving boundary receiving preserved fragments.
-
#target_owner ⇒ Object?
Return the surviving owner receiving preserved fragments.
- #trailing? ⇒ Boolean
Constructor Details
#initialize(target_boundary:, source_owner: nil, comment_regions: [], layout_gaps: [], metadata: {}, **options) ⇒ RehomePlan
:reek:LongParameterList Build a rehome plan for preserved fragments from a removed owner.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 23 def initialize(target_boundary:, source_owner: nil, comment_regions: [], layout_gaps: [], metadata: {}, **) raise ArgumentError, 'target_boundary is required' unless target_boundary @state = { source_owner: source_owner, target_boundary: target_boundary, comment_regions: Array(comment_regions).compact.freeze, layout_gaps: Array(layout_gaps).compact.freeze, metadata: .merge().freeze }.freeze end |
Instance Method Details
#comment_attachment ⇒ Ast::Merge::Comment::Attachment
Materialize the promoted comment regions as a shared attachment.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 100 def primary_region, *orphan_regions = comment_regions = { owner: target_owner, orphan_regions: orphan_regions, metadata: { source: :structural_edit_rehome_plan }.merge() } if leading? Ast::Merge::Comment::Attachment.new(**, trailing_region: primary_region) else Ast::Merge::Comment::Attachment.new(**, leading_region: primary_region) end end |
#comment_regions ⇒ Array<Comment::Region>
Return promoted comment regions to attach on the target side.
53 54 55 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 53 def comment_regions @state[:comment_regions] end |
#edge ⇒ Symbol
Return the target edge receiving fragments.
81 82 83 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 81 def edge target_boundary.edge end |
#empty? ⇒ Boolean
93 94 95 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 93 def empty? comment_regions.empty? && layout_gaps.empty? end |
#inspect ⇒ String
Return a concise debug representation of the rehome plan.
135 136 137 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 135 def inspect "#<#{self.class.name} edge=#{edge.inspect} target_owner=#{target_owner&.class&.name} comment_regions=#{comment_regions.size} layout_gaps=#{layout_gaps.size}>" end |
#layout_attachment ⇒ Ast::Merge::Layout::Attachment
Materialize the promoted layout gaps as a shared attachment.
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 118 def primary_gap = layout_gaps.first = { owner: target_owner, metadata: { source: :structural_edit_rehome_plan }.merge() } if leading? Ast::Merge::Layout::Attachment.new(**, trailing_gap: primary_gap) else Ast::Merge::Layout::Attachment.new(**, leading_gap: primary_gap) end end |
#layout_gaps ⇒ Array<Layout::Gap>
Return promoted layout gaps to attach on the target side.
60 61 62 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 60 def layout_gaps @state[:layout_gaps] end |
#leading? ⇒ Boolean
85 86 87 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 85 def leading? target_boundary.leading? end |
#metadata ⇒ Hash
Return metadata describing how the rehome plan was constructed.
67 68 69 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 67 def @state[:metadata] end |
#source_owner ⇒ Object?
Return the owner losing fragments during the rehome operation.
39 40 41 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 39 def source_owner @state[:source_owner] end |
#target_boundary ⇒ Boundary
Return the surviving boundary receiving preserved fragments.
46 47 48 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 46 def target_boundary @state[:target_boundary] end |
#target_owner ⇒ Object?
Return the surviving owner receiving preserved fragments.
74 75 76 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 74 def target_owner target_boundary.owner end |
#trailing? ⇒ Boolean
89 90 91 |
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 89 def trailing? target_boundary.trailing? end |