Class: RBS::Annotate::Formatter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



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

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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

Class Method Details

.each_part(doc, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object



74
75
76
77
78
79
# 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 Method Details

#<<(s) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  buffer.empty?
end

#format(newline_at_end:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# 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: "") ⇒ Object



36
37
38
39
40
41
42
# File 'lib/rbs/annotate/formatter.rb', line 36

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

  self
end