Class: TypeProf::CodeRange

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(first, last) ⇒ CodeRange

Returns a new instance of CodeRange.



52
53
54
55
56
# File 'lib/typeprof/code_range.rb', line 52

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

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



86
87
88
# File 'lib/typeprof/code_range.rb', line 86

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



86
87
88
# File 'lib/typeprof/code_range.rb', line 86

def last
  @last
end

Class Method Details

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



76
77
78
79
80
# File 'lib/typeprof/code_range.rb', line 76

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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/typeprof/code_range.rb', line 58

def self.from_node(node, encoding = Encoding::UTF_16LE)
  node = node.location if node.is_a?(Prism::Node)
  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.respond_to?(:location)
    loc = node.location
    row, col = loc.start_loc
    pos1 = CodePosition.new(row, col) # TODO: use SPLAT
    row, col = loc.end_loc
    pos2 = CodePosition.new(row, col)
  else
    p node.class.ancestors
    raise "unknown type: #{ node.class }"
  end
  new(pos1, pos2)
end

Instance Method Details

#==(other) ⇒ Object



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

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

#eql?Object



96
97
98
# File 'lib/typeprof/code_range.rb', line 96

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

#hashObject



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

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

#include?(pos) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/typeprof/code_range.rb', line 88

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

#to_lspObject



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

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

#to_sObject Also known as: inspect



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

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