Class: TypeProf::Core::FileContext

Inherits:
Object
  • Object
show all
Defined in:
lib/typeprof/core/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, position_encoding = nil, prism_source = nil, comments = nil) ⇒ FileContext

Returns a new instance of FileContext.



336
337
338
339
340
341
342
# File 'lib/typeprof/core/env.rb', line 336

def initialize(path, position_encoding = nil, prism_source = nil, comments = nil)
  @path = path
  @position_encoding = position_encoding || Encoding::UTF_16LE
  @prism_source = prism_source
  @code_units_cache = nil
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



335
336
337
# File 'lib/typeprof/core/env.rb', line 335

def comments
  @comments
end

#pathObject (readonly)

Returns the value of attribute path.



335
336
337
# File 'lib/typeprof/core/env.rb', line 335

def path
  @path
end

#position_encodingObject (readonly)

Returns the value of attribute position_encoding.



335
336
337
# File 'lib/typeprof/core/env.rb', line 335

def position_encoding
  @position_encoding
end

Instance Method Details

#column_offsets_for(prism_location) ⇒ Object

Returns [start_column, end_column] for a Prism::Location, in the session-configured position encoding. UTF-8 uses Prism's native byte columns directly (Prism's UTF-8 code_units_cache reports code points, not bytes — see LSP 3.17 spec).



347
348
349
350
351
352
353
354
355
356
357
# File 'lib/typeprof/core/env.rb', line 347

def column_offsets_for(prism_location)
  if @position_encoding == Encoding::UTF_8
    [prism_location.start_column, prism_location.end_column]
  else
    cache = code_units_cache
    [
      prism_location.cached_start_code_units_column(cache),
      prism_location.cached_end_code_units_column(cache),
    ]
  end
end