Class: Ast::Merge::StructuralEdit::RemovePlan

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

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.

Parameters:

  • source (String)

    original source text

  • remove_start_line (Integer)

    first removed line, 1-based

  • remove_end_line (Integer)

    last removed line, 1-based

  • leading_boundary (Boundary, nil) (defaults to: nil)

    surviving boundary before the removed range

  • trailing_boundary (Boundary, nil) (defaults to: nil)

    surviving boundary after the removed range

  • removed_attachments (Array<Object>) (defaults to: [])

    attachments from removed owners

  • removed_owners (Array<Object>, nil) (defaults to: nil)

    explicit removed owners

  • retained_owners (Array<Object>, nil) (defaults to: nil)

    explicit surviving owners

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

    base metadata

  • preserve_removed_trailing_blank_lines (Boolean) (defaults to: true)

    whether trailing blank lines should survive in the splice

  • options (Hash)

    extra metadata merged into metadata



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, **options)
  normalized_removed_attachments = Array(removed_attachments).compact.freeze
  @state = {
    boundaries: {
      leading: leading_boundary,
      trailing: trailing_boundary
    }.freeze,
    removed_attachments: normalized_removed_attachments,
    removed_owners: normalize_owners(removed_owners || removed_attachment_owners(normalized_removed_attachments)),
    retained_owners: normalize_owners(retained_owners || [leading_boundary&.owner, trailing_boundary&.owner]),
    metadata: .merge(options).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_contentString

Return the untouched content after the removed range.

Returns:

  • (String)


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.

Parameters:

  • alternate_source (String) (defaults to: source)

    alternate source text

Returns:

  • (String)


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_contentString

Return the untouched content before the removed range.

Returns:

  • (String)


127
128
129
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 127

def before_content
  splice_plan.before_content
end

#changed?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 152

def changed?
  splice_plan.changed?
end

#inspectString

Return a concise debug representation of the remove plan.

Returns:

  • (String)


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_boundaryBoundary?

Return the surviving boundary before the removed range.

Returns:



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

def leading_boundary
  boundaries[:leading]
end

#line_rangeRange

Return the removed line range.

Returns:

  • (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_contentString

Return merged content after applying the removal.

Returns:

  • (String)


148
149
150
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 148

def merged_content
  splice_plan.merged_content
end

#metadataHash

Return metadata describing how the remove plan was constructed.

Returns:

  • (Hash)


120
121
122
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 120

def 
  @state[:metadata]
end

Return all promoted comment regions implied by the rehome plans.

Returns:



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

Return all promoted layout gaps implied by the rehome plans.

Returns:



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_plansArray<RehomePlan>

Return all fragment rehome plans produced from removed attachments.

Returns:



174
175
176
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 174

def rehome_plans
  removed_attachments.flat_map { |attachment| build_rehome_plans_for(attachment) }
end

#remove_end_lineInteger

Return the last removed line number.

Returns:

  • (Integer)


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_lineInteger

Return the first removed line number.

Returns:

  • (Integer)


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_attachmentsArray<Object>

Return all attachments collected from removed owners.

Returns:

  • (Array<Object>)


99
100
101
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 99

def removed_attachments
  @state[:removed_attachments]
end

#removed_contentString

Return the content removed by the plan.

Returns:

  • (String)


134
135
136
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 134

def removed_content
  splice_plan.removed_content
end

#removed_ownersArray<Object>

Return removed owners associated with the removal.

Returns:

  • (Array<Object>)


106
107
108
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 106

def removed_owners
  @state[:removed_owners]
end

#retained_ownersArray<Object>

Return surviving owners allowed to receive promoted fragments.

Returns:

  • (Array<Object>)


113
114
115
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 113

def retained_owners
  @state[:retained_owners]
end

#sourceString

Return the original source text.

Returns:

  • (String)


57
58
59
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 57

def source
  splice_plan.source
end

#to_splice_planSplicePlan

Return the equivalent splice plan for exact application.

Returns:



159
160
161
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 159

def to_splice_plan
  splice_plan
end

#trailing_boundaryBoundary?

Return the surviving boundary after the removed range.

Returns:



92
93
94
# File 'lib/ast/merge/structural_edit/remove_plan.rb', line 92

def trailing_boundary
  boundaries[:trailing]
end