class Rufo::Formatter

def visit_when(node)

def visit_when(node)
  # [:when, conds, body, next_exp]
  _, conds, body, next_exp = node
  consume_keyword "when"
  consume_space
  space_after_when = nil
  indent(@column) do
    visit_comma_separated_list conds
    skip_space
  end
  then_keyword = keyword?("then")
  inline = then_keyword || semicolon?
  if then_keyword
    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
      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
  elsif semicolon?
    skip_semicolons
    if newline? || comment?
      inline = false
    else
      write ";"
      track_case_when
      write " "
    end
  end
  if inline
    indent do
      visit_exps body
    end
  else
    indent_body body
  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