Module: Format::Stuff

Defined in:
lib/format/stuff.rb,
lib/format/stuff/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#count(l, stuff) ⇒ Object



5
6
7
8
9
# File 'lib/format/stuff.rb', line 5

def count(l, stuff)
  stuff
    .map { |s| l.scan(s).size }
    .reduce(:+)
end

#count_tmp_indent(l, tmp_indent) ⇒ Object



11
12
13
14
# File 'lib/format/stuff.rb', line 11

def count_tmp_indent(l, tmp_indent)
  (tmp_indent.map{ |i| l.match(i) ? 1 : 0} + [0])
    .reduce(:+)
end

#gradle_closeObject



28
29
30
# File 'lib/format/stuff.rb', line 28

def gradle_close
  ["}"]
end

#gradle_openObject

def ruby_open

["def", "begin", "(", "[", "{"]

end def ruby_close

["end", ")", "]", "}"]

end def ruby_tmp_indent

[/^\./, /^\|\|/, /^&&/]

end



25
26
27
# File 'lib/format/stuff.rb', line 25

def gradle_open
  ["{"]
end

#run_format(data, output, indent, open = ['(', '[', '{'], close = [')', ']', '}'], tmp_indent = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/format/stuff.rb', line 32

def run_format(data, output, indent, open=['(', '[', '{'], close=[')', ']', '}'], tmp_indent=[])
  indent_level = 0
  data.each_line do |l|
    l.strip!
    # STDERR.puts "working on '#{l}'"
    opening = count(l, open)
    # STDERR.puts "opening #{opening}"
    closing = count(l, close)
    # STDERR.puts "closing #{closing}"
    old_indent_level = indent_level
    indent_level = indent_level + opening - closing
    prefix =
      if indent_level > old_indent_level
        indent * old_indent_level
      else
        indent * indent_level
      end
    tmp_indent_level = count_tmp_indent(l, tmp_indent)
    # STDERR.puts "tmp_indent #{tmp_indent_level}"
    # STDERR.puts "prefix '#{prefix}'"
    prefix = prefix + (indent * tmp_indent_level)
    # STDERR.puts "prefix '#{prefix}'"
    output.puts((prefix + l).rstrip)
  end
  output.close
end