module GraphQL::Language::BlockString

Public Class Methods

print(str, indent: '') click to toggle source
trim_whitespace(str) click to toggle source

Remove leading and trailing whitespace from a block string. See “Block Strings” in github.com/facebook/graphql/blob/master/spec/Section%202%20–%20Language.md

# File lib/graphql/language/block_string.rb, line 7
def self.trim_whitespace(str)
  lines = str.split("\n")
  common_indent = nil

  # find the common whitespace
  lines.each_with_index do |line, idx|
    if idx == 0
      next
    end
    line_length = line.size
    line_indent = line[/\A */].size
    if line_indent < line_length && (common_indent.nil? || line_indent < common_indent)
      common_indent = line_indent
    end
  end

  # Remove the common whitespace
  if common_indent
    lines.each_with_index do |line, idx|
      if idx == 0
        next
      else
        line[0, common_indent] = ""
      end
    end
  end

  # Remove leading & trailing blank lines
  while lines.first.empty?
    lines.shift
  end
  while lines.last.empty?
    lines.pop
  end

  # Rebuild the string
  lines.join("\n")
end

Private Class Methods

break_line(line, length) click to toggle source
# File lib/graphql/language/block_string.rb, line 67
def self.break_line(line, length)
  return [line] if line.length < length + 5

  parts = line.split(Regexp.new("((?: |^).{15,#{length - 40}}(?= |$))"))
  return [line] if parts.length < 4

  sublines = [parts.slice!(0, 3).join]

  parts.each_with_index do |part, i|
    next if i % 2 == 1
    sublines << "#{part[1..-1]}#{parts[i + 1]}"
  end

  sublines
end