Class: Ast::Merge::StructuralEdit::PlanSet
- Inherits:
-
Object
- Object
- Ast::Merge::StructuralEdit::PlanSet
- 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
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#plans ⇒ Object
readonly
Returns the value of attribute plans.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #changed? ⇒ Boolean
-
#initialize(source:, plans:, metadata: {}, **options) ⇒ PlanSet
constructor
A new instance of PlanSet.
-
#inspect ⇒ String
Return a concise debug representation of the plan set.
-
#merged_content ⇒ String
Apply all splice plans and return merged source content.
-
#promoted_comment_regions ⇒ Array<Comment::Region>
Return all promoted comment regions across member plans.
-
#promoted_layout_gaps ⇒ Array<Layout::Gap>
Return all promoted layout gaps across member plans.
-
#rehome_plans ⇒ Array<RehomePlan>
Return all promoted rehome plans emitted by member plans.
-
#splice_plans ⇒ Array<SplicePlan>
Return all plans normalized to SplicePlan instances.
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: {}, **) @source = source.to_s @plans = Array(plans).compact.freeze @metadata = .merge().freeze validate_plans! end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
18 19 20 |
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 18 def @metadata end |
#plans ⇒ Object (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 |
#source ⇒ Object (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
44 45 46 |
# File 'lib/ast/merge/structural_edit/plan_set.rb', line 44 def changed? merged_content != source end |
#inspect ⇒ String
Return a concise debug representation of the plan set.
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_content ⇒ String
Apply all splice plans and return merged source content.
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 |
#promoted_comment_regions ⇒ Array<Comment::Region>
Return all promoted comment regions across member plans.
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 |
#promoted_layout_gaps ⇒ Array<Layout::Gap>
Return all promoted layout gaps across member plans.
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_plans ⇒ Array<RehomePlan>
Return all promoted rehome plans emitted by member plans.
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_plans ⇒ Array<SplicePlan>
Return all plans normalized to SplicePlan instances.
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 |