Class: Prettyrb::Formatter

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

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Formatter

Returns a new instance of Formatter.



5
6
7
# File 'lib/prettyrb/formatter.rb', line 5

def initialize(code)
  @code = code
end

Instance Method Details

#formatObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/prettyrb/formatter.rb', line 9

def format
  parser = Parser::CurrentRuby.new(Prettyrb::Builder.new)

  parser.diagnostics.all_errors_are_fatal = true
  parser.diagnostics.ignore_warnings      = true

  parser.diagnostics.consumer = lambda do |diagnostic|
    $stderr.puts(diagnostic.render)
  end

  root_node, _comments = parser.parse_with_comments(
    Parser::CurrentRuby.send(:setup_source_buffer, "file='(string)'", 1, @code, parser.default_encoding)
  )

  visitor = Visitor.new(root_node)
  visitor.visit(root_node)

  visitor.output
end