Module: Paru::PandocFilter::ASTManipulation
- Included in:
- Node
- Defined in:
- lib/paru/filter/ast_manipulation.rb
Overview
ASTManipulation is a mixin for Node with some standard tree manipulation methods such as inserting or removing nodes, replacing nodes, and so on.
Instance Method Summary collapse
-
#append(child) ⇒ Object
(also: #<<)
Append a child to the list with this node’s children.
-
#delete(child) ⇒ Object
Delete child from this node’s children.
-
#each_depth_first(&block) {|Node| ... } ⇒ Object
Walk the node tree starting at this node, depth first, and apply block to each node in the tree.
-
#find_index(child) ⇒ Number
Find index of child.
-
#get(index) ⇒ Node
Get the child node at index.
-
#insert(index, child) ⇒ Object
Insert child node among this node’s children at position index.
-
#prepend(child) ⇒ Object
Prepend a child to the list with this node’s children.
-
#remove_at(index) ⇒ Object
Remove the child at position index from this node’s children.
-
#replace(old_child, new_child) ⇒ Object
Replace a child from this node’s children with a new child.
-
#replace_at(index, new_child) ⇒ Object
Replace the child at position index from this node’s children with a new child.
Instance Method Details
#append(child) ⇒ Object Also known as: <<
Append a child to the list with this node’s children.
70 71 72 |
# File 'lib/paru/filter/ast_manipulation.rb', line 70 def append(child) @children.push child end |
#delete(child) ⇒ Object
Delete child from this node’s children.
56 57 58 |
# File 'lib/paru/filter/ast_manipulation.rb', line 56 def delete(child) @children.delete child end |
#each_depth_first(&block) {|Node| ... } ⇒ Object
Walk the node tree starting at this node, depth first, and apply block to each node in the tree
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/paru/filter/ast_manipulation.rb', line 109 def each_depth_first(&block) yield self node = if has_been_replaced? get_replacement else self end node.each { |child| child.each_depth_first(&block) } if node.has_children? end |
#find_index(child) ⇒ Number
Find index of child
32 33 34 |
# File 'lib/paru/filter/ast_manipulation.rb', line 32 def find_index(child) @children.find_index child end |
#get(index) ⇒ Node
Get the child node at index
41 42 43 |
# File 'lib/paru/filter/ast_manipulation.rb', line 41 def get(index) @children[index] end |
#insert(index, child) ⇒ Object
Insert child node among this node’s children at position index.
49 50 51 |
# File 'lib/paru/filter/ast_manipulation.rb', line 49 def insert(index, child) @children.insert index, child end |
#prepend(child) ⇒ Object
Prepend a child to the list with this node’s children.
78 79 80 |
# File 'lib/paru/filter/ast_manipulation.rb', line 78 def prepend(child) insert 0, child end |
#remove_at(index) ⇒ Object
Remove the child at position index from this node’s children
63 64 65 |
# File 'lib/paru/filter/ast_manipulation.rb', line 63 def remove_at(index) @children.delete_at index end |
#replace(old_child, new_child) ⇒ Object
Replace a child from this node’s children with a new child.
86 87 88 89 90 91 |
# File 'lib/paru/filter/ast_manipulation.rb', line 86 def replace(old_child, new_child) old_child_index = find_index old_child return unless old_child_index replace_at old_child_index, new_child end |
#replace_at(index, new_child) ⇒ Object
Replace the child at position index from this node’s children with a new child.
98 99 100 |
# File 'lib/paru/filter/ast_manipulation.rb', line 98 def replace_at(index, new_child) @children[index] = new_child end |