123456789_123456789_123456789_123456789_123456789_

Class: TypeProf::CodeRange

Relationships & Source Files
Inherits: Object
Defined in: lib/typeprof/code_range.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(first, last) ⇒ CodeRange

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 52

def initialize(first, last)
  @first = first
  @last = last
  raise unless first
end

Class Method Details

.[](a, b, c, d)

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 73

def self.[](a, b, c, d)
  pos1 = CodePosition.new(a, b)
  pos2 = CodePosition.new(c, d)
  new(pos1, pos2)
end

.from_node(node, encoding = Encoding::UTF_16LE)

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 58

def self.from_node(node, encoding = Encoding::UTF_16LE)
  node = node.location if node.respond_to?(:location)
  if node.is_a?(Prism::Location)
    pos1 = CodePosition.new(node.start_line, node.start_code_units_column(encoding))
    pos2 = CodePosition.new(node.end_line, node.end_code_units_column(encoding))
  elsif node.is_a?(RBS::Location)
    pos1 = CodePosition.new(*node.start_loc)
    pos2 = CodePosition.new(*node.end_loc)
  else
    p node.class.ancestors
    raise "unknown type: #{ node.class }"
  end
  new(pos1, pos2)
end

Instance Attribute Details

#first (readonly)

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 83

attr_reader :first, :last

#last (readonly)

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 83

attr_reader :first, :last

Instance Method Details

#<=>(other)

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 109

def <=>(other)
  cmp = @first <=> other.first
  cmp == 0 ? @last <=> other.last : cmp
end

#==(other) Also known as: #eql?

See additional method definition at line 89.

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 105

def ==(other)
  @first == other.first && @last == other.last
end

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 93

alias eql? ==

#hash

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 95

def hash
  [@first, @last].hash
end

#include?(pos) ⇒ Boolean

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 85

def include?(pos)
  @first <= pos && pos < @last
end

#inspect

Alias for #to_s.

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 103

alias inspect to_s

#to_lsp

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 79

def to_lsp
  { start: @first.to_lsp, end: @last.to_lsp }
end

#to_s Also known as: #inspect

[ GitHub ]

  
# File 'lib/typeprof/code_range.rb', line 99

def to_s
  "%p-%p" % [@first, @last]
end