Class: Prism::Translation::Ripper::LineAndColumnCache

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/translation/ripper.rb,
sig/generated/prism/lex_compat.rbs

Overview

Provides optimized access to line and column information. Ripper bounds are mostly accessed in a linear fashion, so we can try a linear scan first and fall back to binary search.

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ LineAndColumnCache

: (Source source) -> void

Parameters:



458
459
460
461
462
# File 'lib/prism/translation/ripper.rb', line 458

def initialize(source)
  @source = source
  @offsets = source.offsets
  @hint = 0
end

Instance Method Details

#line_and_column(byte_offset) ⇒ [ Integer, Integer ]

: (Integer byte_offset) -> [Integer, Integer]

Parameters:

  • byte_offset (Integer)

Returns:

  • ([ Integer, Integer ])


465
466
467
468
# File 'lib/prism/translation/ripper.rb', line 465

def line_and_column(byte_offset)
  @hint = new_hint(byte_offset) || @source.find_line(byte_offset)
  return [@hint + @source.start_line, byte_offset - @offsets[@hint]]
end