class FormatsLines

Attributes

lines[R]

Public Class Methods

format(lines) click to toggle source
# File lib/formats_lines.rb, line 4
def self.format(lines)
  new(lines).tap do |f|
    f.remove_comments!
    f.remove_leading_whitespace!
    f.join_broken_lines!
  end.lines
end
new(lines) click to toggle source
# File lib/formats_lines.rb, line 12
def initialize(lines)
  @lines = lines
  @keywords = %w(
    abstype and andalso as case do datatype div
    else end eqtype exception extract fn fun functor
    if in include infix infixr let local mod
    nonfix of op open orelse raise rec sharing sig
    signature struct structure then type val where
    with withtype
  )
end

Public Instance Methods

join_broken_lines!() click to toggle source
# File lib/formats_lines.rb, line 34
def join_broken_lines!
  lines = @lines.split("\n")

  each_in_reverse!(lines) do |i|
    join_lines!(lines, i, i-1) if i > 0 && broken_line(lines[i])
  end

  @lines = lines.join("\n")
  self
end
remove_comments!() click to toggle source
# File lib/formats_lines.rb, line 24
def remove_comments!
  @lines.gsub!(/\(\*.*?\*\)\n/m, "")
  self
end
remove_leading_whitespace!() click to toggle source
# File lib/formats_lines.rb, line 29
def remove_leading_whitespace!
  @lines.gsub!(/^\s+/, "")
  self
end

Private Instance Methods

broken_line(line) click to toggle source
# File lib/formats_lines.rb, line 47
def broken_line(line)
  not(line =~ Regexp.new("^(#{@keywords.join("|")})"))
end
each_in_reverse!(array, &block) click to toggle source
# File lib/formats_lines.rb, line 56
def each_in_reverse!(array, &block)
  (0..array.length-1).to_a.reverse.each do |i|
    block.call(i)
  end
end
join_lines!(lines, from, to) click to toggle source
# File lib/formats_lines.rb, line 52
def join_lines!(lines, from, to)
  lines[to] = lines[to] + " " + lines.delete_at(from)
end