Class: FormatsLines

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ FormatsLines

Returns a new instance of FormatsLines.



12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



2
3
4
# File 'lib/formats_lines.rb', line 2

def lines
  @lines
end

Class Method Details

.format(lines) ⇒ Object



4
5
6
7
8
9
10
# 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

Instance Method Details

#join_broken_lines!Object



34
35
36
37
38
39
40
41
42
43
# 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!Object



24
25
26
27
# File 'lib/formats_lines.rb', line 24

def remove_comments!
  @lines.gsub!(/\(\*.*?\*\)\n/m, "")
  self
end

#remove_leading_whitespace!Object



29
30
31
32
# File 'lib/formats_lines.rb', line 29

def remove_leading_whitespace!
  @lines.gsub!(/^\s+/, "")
  self
end