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
sibblings all disjoint 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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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 do not act on that range. This method returns the action as if it was a child action with its range contracted.

Returns:



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

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)


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

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)


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

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



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

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.



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

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.



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

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