Class: Ast::Merge::StructuralEdit::PlanSet

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/structural_edit/plan_set.rb

Overview

Passive batch of non-overlapping structural edit plans against one source.

PlanSet makes the existing line-based structural edit primitives usable as a general-purpose replacement API for downstream callers that need to apply more than one exact source-preserving edit without falling back to ad hoc line-array surgery.

Each plan must be splice-compatible, meaning it either is a SplicePlan itself or responds to #to_splice_plan (for example RemovePlan). All plans must reference the same original source and must not overlap.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, plans:, metadata: {}, **options) ⇒ PlanSet

Returns a new instance of PlanSet.



20
21
22
23
24
25
26
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 20

def initialize(source:, plans:, metadata: {}, **options)
  @source = source.to_s
  @plans = Array(plans).compact.freeze
  @metadata = .merge(options).freeze

  validate_plans!
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



18
19
20
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 18

def 
  @metadata
end

#plansObject (readonly)

Returns the value of attribute plans.



18
19
20
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 18

def plans
  @plans
end

#sourceObject (readonly)

Returns the value of attribute source.



18
19
20
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 18

def source
  @source
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 44

def changed?
  merged_content != source
end

#inspectString

Return a concise debug representation of the plan set.

Returns:

  • (String)


84
85
86
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 84

def inspect
  "#<#{self.class.name} plans=#{plans.size} changed=#{changed?}>"
end

#merged_contentString

Apply all splice plans and return merged source content.

Returns:

  • (String)


38
39
40
41
42
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 38

def merged_content
  ordered_splice_plans.reduce(source) do |current_source, splice_plan|
    splice_plan.apply_to(current_source)
  end
end

Return all promoted comment regions across member plans.

Returns:



62
63
64
65
66
67
68
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 62

def promoted_comment_regions
  plans.filter_map do |plan|
    next unless plan.respond_to?(:promoted_comment_regions)

    Array(plan.promoted_comment_regions)
  end.flatten.freeze
end

Return all promoted layout gaps across member plans.

Returns:



73
74
75
76
77
78
79
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 73

def promoted_layout_gaps
  plans.filter_map do |plan|
    next unless plan.respond_to?(:promoted_layout_gaps)

    Array(plan.promoted_layout_gaps)
  end.flatten.freeze
end

#rehome_plansArray<RehomePlan>

Return all promoted rehome plans emitted by member plans.

Returns:



51
52
53
54
55
56
57
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 51

def rehome_plans
  plans.filter_map do |plan|
    next unless plan.respond_to?(:rehome_plans)

    Array(plan.rehome_plans)
  end.flatten.freeze
end

#splice_plansArray<SplicePlan>

Return all plans normalized to SplicePlan instances.

Returns:



31
32
33
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 31

def splice_plans
  @splice_plans ||= plans.map { |plan| normalize_plan(plan) }.freeze
end