class Rufo::Formatter

def visit_when(node)

def visit_when(node)
  # [:when, conds, body, next_exp]
  # [:in, pattern, body, next_exp]
  kw, conds_or_pattern, body, next_exp = node
  case kw
  when :when
    consume_keyword "when"
  when :in
    if current_token_kind == :on_op
      consume_op "=>"
    else
      consume_keyword "in"
    end
  end
  consume_space
  indent(@column) do
    case kw
    when :when
      visit_comma_separated_list conds_or_pattern
    when :in
      parens = current_token_kind == :on_lparen
      if parens
        consume_token :on_lparen
        skip_space_or_newline
      end
      visit conds_or_pattern
      if parens
        skip_space_or_newline
        consume_token :on_rparen
      end
    end
    skip_space
  end
  written_space = false
  if semicolon?
    inline = true
    skip_semicolons
    if newline? || comment?
      inline = false
    else
      write ";"
      track_case_when
      write " "
      written_space = true
    end
  end
  if keyword?("then")
    inline = true
    next_token
    skip_space
    info = track_case_when
    skip_semicolons
    if newline?
      inline = false
      # Cancel tracking of `case when ... then` on a nelwine.
      @case_when_positions.pop
    else
      write_space unless written_space
      write "then"
      # We adjust the column and offset from:
      #
      #     when 1 then 2
      #           ^ (with offset 0)
      #
      # to:
      #
      #     when 1 then 2
      #                ^ (with offset 5)
      #
      # In that way we can align this with an `else` clause.
      if info
        offset = @column - info[1]
        info[1] = @column
        info[-1] = offset
      end
      write_space
    end
  end
  # If node is inline pattern matching, body will be nil
  if body
    if inline
      indent do
        visit_exps body
      end
    else
      indent_body body
    end
  end
  if next_exp
    write_indent
    if next_exp[0] == :else
      # [:else, body]
      consume_keyword "else"
      track_case_when
      first_space = skip_space
      if newline? || semicolon? || comment?
        # Cancel tracking of `else` on a nelwine.
        @case_when_positions.pop
        indent_body next_exp[1]
      else
        if align_case_when
          write_space
        else
          write_space_using_setting(first_space, :one)
        end
        visit_exps next_exp[1]
      end
    else
      visit next_exp
    end
  end
end