module Gapic::FormattingUtils

Various string formatting utils

Public Class Methods

format_doc_lines(api, lines, disable_xrefs: false) click to toggle source

Given an enumerable of lines, performs yardoc formatting, including:

  • Interpreting cross-references identified as described in AIP 192

  • Escaping literal braces that look like yardoc type links

Tries to be smart about exempting preformatted text blocks.

@param api [Gapic::Schema::Api] @param lines [Enumerable<String>] @param disable_xrefs [Boolean] (default is `false`) Disable linking to

cross-references, and render them simply as text. This can be used if
it is known that the targets are not present in the current library.

@return [Enumerable<String>]

# File lib/gapic/formatting_utils.rb, line 44
def format_doc_lines api, lines, disable_xrefs: false
  # To detect preformatted blocks, this tracks the "expected" base indent
  # according to Markdown. Specifically, this is the effective indent of
  # previous block, which is normally 0 except if we're in a list item.
  # Then, if a block is indented at least 4 spaces past that expected
  # indent (and as long as it remains so), those lines are considered
  # preformatted.
  in_block = nil
  base_indent = 0
  (lines - @omit_lines).map do |line|
    indent = line_indent line
    if indent.nil?
      in_block = nil
    else
      in_block, base_indent = update_indent_state in_block, base_indent, line, indent
      if in_block == false
        line = escape_line_braces line
        line = format_line_xrefs api, line, disable_xrefs
      end
    end
    line
  end
end
format_number(value) click to toggle source

Given a number, format it in such a way that Rubocop will be happy. Specifically, we add underscores if the magnitude is at least 10_000. This works for both integers and floats.

@param value [Numeric] @return [String]

# File lib/gapic/formatting_utils.rb, line 76
def format_number value
  return value.to_s if value.abs < 10_000
  str = value.is_a?(Integer) ? value.to_s : BigDecimal(value.to_f.to_s).to_s("F")
  re = /^(-?\d+)(\d\d\d)([_.][_.\d]+)?$/
  while (m = re.match str)
    str = "#{m[1]}_#{m[2]}#{m[3]}"
  end
  str
end

Private Class Methods

convert_address_to_ruby(entity) click to toggle source
# File lib/gapic/formatting_utils.rb, line 153
def convert_address_to_ruby entity
  file = entity.containing_file
  api = file.containing_api
  address = entity.address
  address = address.join "." if address.is_a? Array
  address = address.sub file.package, file.ruby_package if file.ruby_package&.present?
  address.split(/\.|::/).reject(&:empty?).map(&:camelize).map { |node| api.fix_namespace node }.join("::")
end
escape_line_braces(line) click to toggle source
# File lib/gapic/formatting_utils.rb, line 105
def escape_line_braces line
  while (m = @brace_detector.match line)
    line = "#{m[:pre]}\\\\{#{m[:inside]}}#{m[:post]}"
  end
  line
end
format_line_xrefs(api, line, disable_xrefs) click to toggle source
# File lib/gapic/formatting_utils.rb, line 112
def format_line_xrefs api, line, disable_xrefs
  while (m = @xref_detector.match line)
    entity = api.lookup m[:addr]
    return line if entity.nil?
    text = m[:text]
    yard_link = disable_xrefs ? text : yard_link_for_entity(entity, text)
    return line if yard_link.nil?
    line = "#{m[:pre]}#{yard_link}#{m[:post]}"
  end
  line
end
line_indent(line) click to toggle source
# File lib/gapic/formatting_utils.rb, line 100
def line_indent line
  m = /^( *)\S/.match line
  m.nil? ? nil : m[1].length
end
update_indent_state(in_block, base_indent, line, indent) click to toggle source
# File lib/gapic/formatting_utils.rb, line 88
def update_indent_state in_block, base_indent, line, indent
  if in_block != true && @list_element_detector =~ line
    in_block = false
    indent = base_indent if indent > base_indent
    base_indent = (indent + 7) / 4 * 4
  else
    in_block = indent >= base_indent + 4 unless in_block == false
    base_indent = indent / 4 * 4 if in_block == false && indent < base_indent
  end
  [in_block, base_indent]
end