Class: MarshalParser::Formatters::AST::Renderers::RendererWithAnnotations

Inherits:
Object
  • Object
show all
Defined in:
lib/marshal-parser/formatters/ast/renderers/renderer_with_annotations.rb

Instance Method Summary collapse

Constructor Details

#initialize(indent_size:, width:) ⇒ RendererWithAnnotations

Returns a new instance of RendererWithAnnotations.



8
9
10
11
# File 'lib/marshal-parser/formatters/ast/renderers/renderer_with_annotations.rb', line 8

def initialize(indent_size:, width:)
  @indent_size = indent_size
  @width = width
end

Instance Method Details

#render(block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/marshal-parser/formatters/ast/renderers/renderer_with_annotations.rb', line 13

def render(block)
  # indent
  lines = apply_indentation(block, 0)

  # add annotations
  strings = lines.map do |line|
    case line
    when LineAnnotated
      "%-#{@width}s # %s" % [line.string, line.annotation]
    when Line
      line.string
    else
      raise "Unexpected line #{e} (#{e.class}), expected Line or LineAnnotated"
    end
  end

  strings.join("\n")
end