Class: Ast::Merge::StructuralEdit::RemovePlan
- Inherits:
-
Object
- Object
- Ast::Merge::StructuralEdit::RemovePlan
- Defined in:
- lib/ast/merge/structural_edit/remove_plan.rb
Overview
Passive plan for removing a contiguous structural range while preserving exact untouched source and recording which removed comment/layout attachments should be promoted to surviving adjacent owners. :reek:TooManyMethods :reek:MissingSafeMethod
Instance Method Summary collapse
-
#after_content ⇒ String
Return the untouched content after the removed range.
-
#apply_to(alternate_source = source) ⇒ String
Apply the removal to the original source or a compatible alternate source.
-
#before_content ⇒ String
Return the untouched content before the removed range.
- #changed? ⇒ Boolean
-
#initialize(source:, remove_start_line:, remove_end_line:, leading_boundary: nil, trailing_boundary: nil, removed_attachments: [], removed_owners: nil, retained_owners: nil, metadata: {}, preserve_removed_trailing_blank_lines: true, **options) ⇒ RemovePlan
constructor
:reek:LongParameterList :reek:ControlParameter Build a plan that removes a contiguous line range and preserves promotable attachments.
-
#inspect ⇒ String
Return a concise debug representation of the remove plan.
-
#leading_boundary ⇒ Boundary?
Return the surviving boundary before the removed range.
-
#line_range ⇒ Range
Return the removed line range.
-
#merged_content ⇒ String
Return merged content after applying the removal.
-
#metadata ⇒ Hash
Return metadata describing how the remove plan was constructed.
-
#promoted_comment_regions ⇒ Array<Comment::Region>
Return all promoted comment regions implied by the rehome plans.
-
#promoted_layout_gaps ⇒ Array<Layout::Gap>
Return all promoted layout gaps implied by the rehome plans.
-
#rehome_plans ⇒ Array<RehomePlan>
Return all fragment rehome plans produced from removed attachments.
-
#remove_end_line ⇒ Integer
Return the last removed line number.
-
#remove_start_line ⇒ Integer
Return the first removed line number.
-
#removed_attachments ⇒ Array<Object>
Return all attachments collected from removed owners.
-
#removed_content ⇒ String
Return the content removed by the plan.
-
#removed_owners ⇒ Array<Object>
Return removed owners associated with the removal.
-
#retained_owners ⇒ Array<Object>
Return surviving owners allowed to receive promoted fragments.
-
#source ⇒ String
Return the original source text.
-
#to_splice_plan ⇒ SplicePlan
Return the equivalent splice plan for exact application.
-
#trailing_boundary ⇒ Boundary?
Return the surviving boundary after the removed range.
Constructor Details
#initialize(source:, remove_start_line:, remove_end_line:, leading_boundary: nil, trailing_boundary: nil, removed_attachments: [], removed_owners: nil, retained_owners: nil, metadata: {}, preserve_removed_trailing_blank_lines: true, **options) ⇒ RemovePlan
:reek:LongParameterList :reek:ControlParameter Build a plan that removes a contiguous line range and preserves promotable attachments.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 27 def initialize(source:, remove_start_line:, remove_end_line:, leading_boundary: nil, trailing_boundary: nil, removed_attachments: [], removed_owners: nil, retained_owners: nil, metadata: {}, preserve_removed_trailing_blank_lines: true, **) = Array().compact.freeze @state = { boundaries: { leading: leading_boundary, trailing: trailing_boundary }.freeze, removed_attachments: , removed_owners: normalize_owners(removed_owners || ()), retained_owners: normalize_owners(retained_owners || [leading_boundary&.owner, trailing_boundary&.owner]), metadata: .merge().freeze }.freeze validate_boundaries! @splice_plan = SplicePlan.new( source: source, replacement: '', replace_start_line: remove_start_line, replace_end_line: remove_end_line, leading_boundary: leading_boundary, trailing_boundary: trailing_boundary, preserve_removed_trailing_blank_lines: preserve_removed_trailing_blank_lines, metadata: { source: :structural_edit_remove_plan }.merge(self.) ) end |
Instance Method Details
#after_content ⇒ String
Return the untouched content after the removed range.
141 142 143 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 141 def after_content splice_plan.after_content end |
#apply_to(alternate_source = source) ⇒ String
Apply the removal to the original source or a compatible alternate source.
167 168 169 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 167 def apply_to(alternate_source = source) splice_plan.apply_to(alternate_source) end |
#before_content ⇒ String
Return the untouched content before the removed range.
127 128 129 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 127 def before_content splice_plan.before_content end |
#changed? ⇒ Boolean
152 153 154 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 152 def changed? splice_plan.changed? end |
#inspect ⇒ String
Return a concise debug representation of the remove plan.
195 196 197 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 195 def inspect "#<#{self.class.name} lines=#{remove_start_line}..#{remove_end_line} removed_owners=#{removed_owners.size} rehome_plans=#{rehome_plans.size}>" end |
#leading_boundary ⇒ Boundary?
Return the surviving boundary before the removed range.
85 86 87 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 85 def leading_boundary boundaries[:leading] end |
#line_range ⇒ Range
Return the removed line range.
78 79 80 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 78 def line_range remove_start_line..remove_end_line end |
#merged_content ⇒ String
Return merged content after applying the removal.
148 149 150 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 148 def merged_content splice_plan.merged_content end |
#metadata ⇒ Hash
Return metadata describing how the remove plan was constructed.
120 121 122 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 120 def @state[:metadata] end |
#promoted_comment_regions ⇒ Array<Comment::Region>
Return all promoted comment regions implied by the rehome plans.
181 182 183 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 181 def promoted_comment_regions rehome_plans.flat_map(&:comment_regions) end |
#promoted_layout_gaps ⇒ Array<Layout::Gap>
Return all promoted layout gaps implied by the rehome plans.
188 189 190 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 188 def promoted_layout_gaps rehome_plans.flat_map(&:layout_gaps) end |
#rehome_plans ⇒ Array<RehomePlan>
Return all fragment rehome plans produced from removed attachments.
174 175 176 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 174 def rehome_plans .flat_map { || build_rehome_plans_for() } end |
#remove_end_line ⇒ Integer
Return the last removed line number.
71 72 73 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 71 def remove_end_line splice_plan.replace_end_line end |
#remove_start_line ⇒ Integer
Return the first removed line number.
64 65 66 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 64 def remove_start_line splice_plan.replace_start_line end |
#removed_attachments ⇒ Array<Object>
Return all attachments collected from removed owners.
99 100 101 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 99 def @state[:removed_attachments] end |
#removed_content ⇒ String
Return the content removed by the plan.
134 135 136 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 134 def removed_content splice_plan.removed_content end |
#removed_owners ⇒ Array<Object>
Return removed owners associated with the removal.
106 107 108 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 106 def removed_owners @state[:removed_owners] end |
#retained_owners ⇒ Array<Object>
Return surviving owners allowed to receive promoted fragments.
113 114 115 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 113 def retained_owners @state[:retained_owners] end |
#source ⇒ String
Return the original source text.
57 58 59 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 57 def source splice_plan.source end |
#to_splice_plan ⇒ SplicePlan
Return the equivalent splice plan for exact application.
159 160 161 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 159 def to_splice_plan splice_plan end |
#trailing_boundary ⇒ Boundary?
Return the surviving boundary after the removed range.
92 93 94 |
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 92 def trailing_boundary boundaries[:trailing] end |