Module: Solargraph::Pin::Documenting

Included in:
Base, Conversions
Defined in:
lib/solargraph/pin/documenting.rb

Overview

A module to add the Pin::Base#documentation method.

Defined Under Namespace

Classes: DocSection

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalize_indentation(text) ⇒ String

Parameters:

  • text (String)

Returns:

  • (String)


103
104
105
106
107
108
109
110
111
# File 'lib/solargraph/pin/documenting.rb', line 103

def self.normalize_indentation text
  left = text.lines.map do |line|
    match = line.match(/^ +/)
    next 0 unless match
    match[0].length
  end.min
  return text if left.nil? || left.zero?
  text.lines.map { |line| line[left..] }.join
end

.strip_html_comments(text) ⇒ String

Parameters:

  • text (String)

Returns:

  • (String)


97
98
99
# File 'lib/solargraph/pin/documenting.rb', line 97

def self.strip_html_comments text
  text.gsub(/<!--([\s\S]*?)-->/, '').strip
end

Instance Method Details

#documentationString

Returns:

  • (String)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/solargraph/pin/documenting.rb', line 75

def documentation
  @documentation ||= begin
    # Using DocSections allows for code blocks that start with an empty
    # line and at least two spaces of indentation. This is a common
    # convention in Ruby core documentation, e.g., String#split.
    sections = [DocSection.new(false)]
    Documenting.normalize_indentation(Documenting.strip_html_comments(docstring.to_s.gsub("\t", '  '))).lines.each do |l|
      if l.start_with?('  ')
        # Code block
        sections.push DocSection.new(true) unless sections.last.code?
      elsif sections.last.code?
        # Regular documentation
        sections.push DocSection.new(false)
      end
      sections.last.concat l
    end
    sections.map(&:to_s).join.strip
  end
end