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.



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

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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

Class Method Details

.each_part(doc, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rbs/annotate/formatter.rb', line 58

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



72
73
74
75
76
77
# File 'lib/rbs/annotate/formatter.rb', line 72

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rbs/annotate/formatter.rb', line 11

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

    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)


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

def empty?
  buffer.empty?
end

#format(newline_at_end:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbs/annotate/formatter.rb', line 46

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



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

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

  self
end