class GraphQLFormatter

Constants

VERSION

Attributes

in_parens[R]
result[R]

Public Class Methods

new(input) click to toggle source
# File lib/graphql-formatter.rb, line 16
def initialize(input)
  @input     = input
  @result    = LineCollection.new
  @in_parens = false
  parse!
end

Private Instance Methods

parse!() click to toggle source
# File lib/graphql-formatter.rb, line 25
def parse!
  @input.chars do |char|
    case char
    when '('
      parse_open_paren
    when ')'
      parse_open_close_paren
    when '{'
      parse_open_bracket
    when '}'
      parse_close_bracket
    when ':'
      parse_colon
    when ','
      parse_comma
    when ' '
      parse_space
    else
      parse_character char
    end
  end
end
parse_character(char) click to toggle source
# File lib/graphql-formatter.rb, line 106
def parse_character(char)
  current_string << ' ' if in_parens && %w{: ,}.include?(last_character)
  new_string color: :light_green if current_indent_level == 0 && current_line.empty?
  current_string.color = :magenta if current_string.end_with? '...'
  current_string << char
end
parse_close_bracket() click to toggle source
# File lib/graphql-formatter.rb, line 66
def parse_close_bracket
  current_string << ' ' unless last_character == ' ' || current_line.empty?
  new_line indent_level: current_indent_level - 1
  new_string << '}'
  new_line
end
parse_colon() click to toggle source
# File lib/graphql-formatter.rb, line 73
def parse_colon
  new_string << ':'
  new_string color: in_parens ? :yellow : :blue
end
parse_comma() click to toggle source
# File lib/graphql-formatter.rb, line 78
def parse_comma
  (last_character == '}' ? previous_string : new_string) << ','
  if in_parens
    new_string(color: :light_yellow)
  else
    new_line unless current_line.empty?
    new_string(color: :light_blue)
  end
end
parse_open_bracket() click to toggle source
# File lib/graphql-formatter.rb, line 59
def parse_open_bracket
  current_string << ' ' unless last_character == ' '
  new_string << '{'
  new_line indent_level: current_indent_level + 1
  new_string color: :light_blue
end
parse_open_close_paren() click to toggle source
# File lib/graphql-formatter.rb, line 54
def parse_open_close_paren
  @in_parens = false
  new_string << ')'
end
parse_open_paren() click to toggle source
# File lib/graphql-formatter.rb, line 48
def parse_open_paren
  @in_parens = true
  new_string << '('
  new_string color: :light_yellow
end
parse_space() click to toggle source
# File lib/graphql-formatter.rb, line 88
def parse_space
  if current_indent_level == 0
    new_color =
      case current_color
      when :light_green
        :light_magenta
      when :light_magenta
        nil
      when nil
        :light_cyan
      else
        current_color
      end
    new_string color: new_color
  end
  current_string << ' ' unless current_line.empty?
end