class RubyLsp::ERBDocument::ERBScanner

def initialize(source)

: (String source) -> void
def initialize(source)
  @source = source
  @host_language = +"" #: String
  @ruby = +"" #: String
  @current_pos = 0 #: Integer
  @inside_ruby = false #: bool
end

def next_char

: -> String
def next_char
  @source[@current_pos + 1] || ""
end

def push_char(char)

: (String char) -> void
def push_char(char)
  if @inside_ruby
    @ruby << char
    @host_language << " " * char.length
  else
    @ruby << " " * char.length
    @host_language << char
  end
end

def scan

: -> void
def scan
  while @current_pos < @source.length
    scan_char
    @current_pos += 1
  end
end

def scan_char

: -> void
def scan_char
  char = @source[@current_pos]
  case char
  when "<"
    if next_char == "%"
      @inside_ruby = true
      @current_pos += 1
      push_char("  ")
      if next_char == "=" && @source[@current_pos + 2] == "="
        @current_pos += 2
        push_char("  ")
      elsif next_char == "=" || next_char == "-"
        @current_pos += 1
        push_char(" ")
      end
    else
      push_char(
        char, #: as !nil
      )
    end
  when "-"
    if @inside_ruby && next_char == "%" &&
        @source[@current_pos + 2] == ">"
      @current_pos += 2
      push_char("   ")
      @inside_ruby = false
    else
      push_char(
        char, #: as !nil
      )
    end
  when "%"
    if @inside_ruby && next_char == ">"
      @inside_ruby = false
      @current_pos += 1
      push_char("  ")
    else
      push_char(
        char, #: as !nil
      )
    end
  when "\r"
    @ruby << char
    @host_language << char
    if next_char == "\n"
      @ruby << next_char
      @host_language << next_char
      @current_pos += 1
    end
  when "\n"
    @ruby << char
    @host_language << char
  else
    push_char(
      char, #: as !nil
    )
  end
end