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.



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

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



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

def last
  @last
end

Class Method Details

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



73
74
75
76
77
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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 Method Details

#<=>(other) ⇒ Object



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

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

#==(other) ⇒ Object



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

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

#eql?Object



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

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

#hashObject



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

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

#include?(pos) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_lspObject



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

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

#to_sObject Also known as: inspect



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

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