123456789_123456789_123456789_123456789_123456789_

Class: RBS::Annotate::Formatter

Relationships & Source Files
Inherits: Object
Defined in: lib/rbs/annotate/formatter.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newFormatter

[ GitHub ]

  
# File 'lib/rbs/annotate/formatter.rb', line 8

def initialize()
  @buffer = +""
  @pending_separator = nil
end

Class Method Details

.each_part(doc, &block)

[ GitHub ]

  
# File 'lib/rbs/annotate/formatter.rb', line 60

def self.each_part(doc, &block)
  if block
    if doc.file
      yield doc
    else
      doc.each do |d|
        each_part(d, &block)
      end
    end
  else
    enum_for :each_part, doc
  end
end

.translate(doc)

[ GitHub ]

  
# File 'lib/rbs/annotate/formatter.rb', line 74

def self.translate(doc)
  if doc.file
    formatter = RDoc::Markup::ToMarkdown.new
    doc.accept(formatter).strip.lines.map(&:rstrip).join("\n")
  end
end

Instance Attribute Details

#buffer (readonly)

[ GitHub ]

  
# File 'lib/rbs/annotate/formatter.rb', line 6

attr_reader :buffer

#empty?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/annotate/formatter.rb', line 44

def empty?
  buffer.empty?
end

Instance Method Details

#<<(s)

[ GitHub ]

  
# File 'lib/rbs/annotate/formatter.rb', line 13

def <<(s)
  if s
    if s.is_a?(RDoc::Markup::Document)
      s = self.class.translate(s) or raise
    end

    s = s.rstrip

    unless s.empty?
      if ss = @pending_separator
        buffer << ss
        buffer << "\n"
        @pending_separator = nil
      end

      buffer << s
      buffer << "\n"
    end
  end

  self
end

#format(newline_at_end:)

[ GitHub ]

  
# File 'lib/rbs/annotate/formatter.rb', line 48

def format(newline_at_end:)
  unless buffer.empty?
    if newline_at_end
      buffer.strip + "\n\n"
    else
      buffer.strip + "\n"
    end
  else
    buffer
  end
end

#margin(separator: "")

[ GitHub ]

  
# File 'lib/rbs/annotate/formatter.rb', line 36

def margin(separator: "")
  unless buffer.empty?
    @pending_separator = separator
  end

  self
end