Class: Gluez::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/gluez/formatter.rb

Class Method Summary collapse

Class Method Details

.format(lines) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gluez/formatter.rb', line 3

def self.format(lines)
  indent = 0

  prev_line = nil
  lines.split("\n").collect do |line|
    line = line.strip
    
    indent -= 2 if ['}', 'fi', 'else'].include?(line)
    
    l = (' ' * indent) + line
    indent += 2 if [/^function /, /^if /, /^else/].any?{|e| line =~ e}

    indent -= 2 if line =~ /^data=/
    indent += 2 if line == ')' && prev_line == 'DATA'
    
    prev_line = line
    
    l
  end.join("\n")
end