Class: Parser::Source::TreeRewriter::Action Private

Inherits:
Object
  • Object
show all
Defined in:
lib/parser/source/tree_rewriter/action.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Actions are arranged in a tree and get combined so that:

  • Children are strictly contained by their parent

  • Siblings are all disjointed from one another and ordered

  • Only actions with ‘replacement == nil` may have children

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range, enforcer, insert_before: '', replacement: nil, insert_after: '', children: []) ⇒ Action

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Action.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/parser/source/tree_rewriter/action.rb', line 17

def initialize(range, enforcer,
     insert_before: '',
     replacement: nil,
     insert_after: '',
     children: []
  )
  @range, @enforcer, @children, @insert_before, @replacement, @insert_after =
    range, enforcer, children.freeze, insert_before.freeze, replacement, insert_after.freeze

  freeze
end

Instance Attribute Details

#insert_afterObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/parser/source/tree_rewriter/action.rb', line 15

def insert_after
  @insert_after
end

#insert_beforeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/parser/source/tree_rewriter/action.rb', line 15

def insert_before
  @insert_before
end

#rangeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/parser/source/tree_rewriter/action.rb', line 15

def range
  @range
end

#replacementObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



15
16
17
# File 'lib/parser/source/tree_rewriter/action.rb', line 15

def replacement
  @replacement
end

Instance Method Details

#combine(action) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
# File 'lib/parser/source/tree_rewriter/action.rb', line 29

def combine(action)
  return self if action.empty? # Ignore empty action
  do_combine(action)
end

#contractAction

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A root action has its range set to the whole source range, even though it typically does not act on that range. This method returns the action as if it was a child action with its range contracted.

Returns:



68
69
70
71
72
73
74
75
76
# File 'lib/parser/source/tree_rewriter/action.rb', line 68

def contract
  raise 'Empty actions can not be contracted' if empty?
  return self if insertion?
  range = @range.with(
    begin_pos: children.first.range.begin_pos,
    end_pos: children.last.range.end_pos,
  )
  with(range: range)
end

#empty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/parser/source/tree_rewriter/action.rb', line 34

def empty?
  @insert_before.empty? &&
    @insert_after.empty? &&
    @children.empty? &&
    (@replacement == nil || (@replacement.empty? && @range.empty?))
end

#insertion?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


58
59
60
# File 'lib/parser/source/tree_rewriter/action.rb', line 58

def insertion?
  !insert_before.empty? || !insert_after.empty? || (replacement && !replacement.empty?)
end

#moved(source_buffer, offset) ⇒ Action

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

No check is done on validity of resulting range.

Returns:

  • (Action)

    that has been moved to the given source_buffer and with the given offset



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/parser/source/tree_rewriter/action.rb', line 81

def moved(source_buffer, offset)
  moved_range = ::Parser::Source::Range.new(
    source_buffer,
    @range.begin_pos + offset,
    @range.end_pos + offset
  )
  with(
    range: moved_range,
    children: children.map { |child| child.moved(source_buffer, offset) }
  )
end

#nested_actionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
53
54
55
56
# File 'lib/parser/source/tree_rewriter/action.rb', line 50

def nested_actions
  actions = []
  actions << [:wrap, @range, @insert_before, @insert_after] if !@insert_before.empty? ||
                                                               !@insert_after.empty?
  actions << [:replace, @range, @replacement] if @replacement
  actions.concat(@children.flat_map(&:nested_actions))
end

#ordered_replacementsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
44
45
46
47
48
# File 'lib/parser/source/tree_rewriter/action.rb', line 41

def ordered_replacements
  reps = []
  reps << [@range.begin, @insert_before] unless @insert_before.empty?
  reps << [@range, @replacement] if @replacement
  reps.concat(@children.flat_map(&:ordered_replacements))
  reps << [@range.end, @insert_after] unless @insert_after.empty?
  reps
end