Class: Ast::Merge::StructuralEdit::RehomePlan

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • source_owner (Object, nil) (defaults to: nil)

    owner losing the fragments

  • target_boundary (Boundary)

    surviving boundary receiving fragments

  • comment_regions (Array<Comment::Region>) (defaults to: [])

    promoted comment regions

  • layout_gaps (Array<Layout::Gap>) (defaults to: [])

    promoted layout gaps

  • metadata (Hash) (defaults to: {})

    base metadata

  • options (Hash)

    extra metadata merged into metadata

Raises:

  • (ArgumentError)


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: {},
               **options)
  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(options).freeze
  }.freeze
end

Instance Method Details

#comment_attachmentAst::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 comment_attachment
  primary_region, *orphan_regions = comment_regions
  options = {
    owner: target_owner,
    orphan_regions: orphan_regions,
    metadata: { source: :structural_edit_rehome_plan }.merge()
  }

  if leading?
    Ast::Merge::Comment::Attachment.new(**options, trailing_region: primary_region)
  else
    Ast::Merge::Comment::Attachment.new(**options, leading_region: primary_region)
  end
end

#comment_regionsArray<Comment::Region>

Return promoted comment regions to attach on the target side.

Returns:



53
54
55
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 53

def comment_regions
  @state[:comment_regions]
end

#edgeSymbol

Return the target edge receiving fragments.

Returns:

  • (Symbol)


81
82
83
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 81

def edge
  target_boundary.edge
end

#empty?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 93

def empty?
  comment_regions.empty? && layout_gaps.empty?
end

#inspectString

Return a concise debug representation of the rehome plan.

Returns:

  • (String)


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_attachmentAst::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 layout_attachment
  primary_gap = layout_gaps.first
  options = {
    owner: target_owner,
    metadata: { source: :structural_edit_rehome_plan }.merge()
  }

  if leading?
    Ast::Merge::Layout::Attachment.new(**options, trailing_gap: primary_gap)
  else
    Ast::Merge::Layout::Attachment.new(**options, leading_gap: primary_gap)
  end
end

#layout_gapsArray<Layout::Gap>

Return promoted layout gaps to attach on the target side.

Returns:



60
61
62
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 60

def layout_gaps
  @state[:layout_gaps]
end

#leading?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 85

def leading?
  target_boundary.leading?
end

#metadataHash

Return metadata describing how the rehome plan was constructed.

Returns:

  • (Hash)


67
68
69
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 67

def 
  @state[:metadata]
end

#source_ownerObject?

Return the owner losing fragments during the rehome operation.

Returns:

  • (Object, nil)


39
40
41
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 39

def source_owner
  @state[:source_owner]
end

#target_boundaryBoundary

Return the surviving boundary receiving preserved fragments.

Returns:



46
47
48
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 46

def target_boundary
  @state[:target_boundary]
end

#target_ownerObject?

Return the surviving owner receiving preserved fragments.

Returns:

  • (Object, nil)


74
75
76
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 74

def target_owner
  target_boundary.owner
end

#trailing?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/ast/merge/structural_edit/rehome_plan.rb', line 89

def trailing?
  target_boundary.trailing?
end