module Solargraph::Pin::Documenting

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

Public Instance Methods

documentation() click to toggle source

@return [String]

# 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.strip
  end
end

Private Instance Methods

normalize_indentation(text) click to toggle source

@param text [String] @return [String]

# File lib/solargraph/pin/documenting.rb, line 91
def normalize_indentation text
  text.lines.map { |l| remove_odd_spaces(l) }.join
end
remove_odd_spaces(line) click to toggle source

@param line [String] @return [String]

# File lib/solargraph/pin/documenting.rb, line 97
def remove_odd_spaces line
  return line unless line.start_with?(' ')
  spaces = line.match(/^ +/)[0].length
  return line unless spaces.odd?
  line[1..-1]
end