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
|
# File 'lib/ruby_indentation.rb', line 7
def self.[](buffer)
opening_and_modifier_tokens = %w[if unless until while].to_set
opening_tokens = %w[begin case class def for module do {].to_set
closing_tokens = %w[end }].to_set
separator = [';', :operator]
indent = 0
buffer_tokens = [separator] + CodeRay.scan(buffer, :ruby).each_slice(2).select{|_, kind|
kind != :space
}
buffer_tokens.each_cons(2){ |(*old_pair), (token, kind)|
if kind == :keyword || kind == :operator
if opening_tokens.include?(token) ||
opening_and_modifier_tokens.include?(token) &&
( old_pair == separator || old_pair == ['=', :operator ] )
indent += 1
elsif closing_tokens.include?(token)
indent -= 1
end
end
}
indent < 0 ? 0 : indent
end
|