-
Notifications
You must be signed in to change notification settings - Fork 7
/
insert_action.rb
47 lines (42 loc) · 1.13 KB
/
insert_action.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
module Synvert::Core
# InsertAction to insert code to the top of node body.
class Rewriter::InsertAction < Rewriter::Action
DO_LENGTH = ' do'.length
# Begin position to insert code.
#
# @return [Integer] begin position.
def begin_pos
case @node.type
when :block
if @node.children[1].children.empty?
@node.children[0].loc.expression.end_pos + DO_LENGTH
else
@node.children[1].loc.expression.end_pos
end
when :class
@node.children[1] ? @node.children[1].loc.expression.end_pos : @node.children[0].loc.expression.end_pos
else
@node.children.last.loc.expression.end_pos
end
end
# End position, always same to begin position.
#
# @return [Integer] end position.
def end_pos
begin_pos
end
private
# Indent of the node.
#
# @param node [Parser::AST::Node]
# @return [String] n times whitesphace
def indent(node)
if %i[block class].include? node.type
' ' * (node.indent + DEFAULT_INDENT)
else
' ' * node.indent
end
end
end
end