Module: Solargraph::Pin::Documenting

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

Overview

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

Defined Under Namespace

Classes: DocSection

Instance Method Summary collapse

Instance Method Details

#documentationString

Returns:

  • (String)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/solargraph/pin/documenting.rb', line 62

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)]
    normalize_indentation(docstring.to_s).gsub(/\t/, '  ').lines.each do |l|
      if l.strip.empty?
        sections.last.concat l
      else
        if (l =~ /^  [^\s]/ && sections.last.plaintext =~ /(\r?\n[ \t]*?){2,}$/) || (l.start_with?('  ') && sections.last.code?)
          # Code block
          sections.push DocSection.new(true) unless sections.last.code?
          sections.last.concat l[2..-1]
        else
          # Regular documentation
          sections.push DocSection.new(false) if sections.last.code?
          sections.last.concat l
        end
      end
    end
    sections.map(&:to_s).join
  end
end