/
code-range.rb
177 lines (154 loc) · 4.56 KB
/
code-range.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
module TypeProf
class CodeLocation
# In Ruby, lineno is 1-origin, and column is 0-origin
def initialize(lineno, column)
@lineno = lineno
@column = column
end
def inspect
"(%d,%d)" % [@lineno, @column]
end
attr_reader :lineno, :column
def self.from_lsp(lsp_loc)
# In the Language Server Protocol, lineno and column are both 0-origin
CodeLocation.new(lsp_loc[:line] + 1, lsp_loc[:character])
end
def to_lsp
{ line: @lineno - 1, character: @column }
end
def advance_cursor(offset, source_text)
new_lineno = @lineno
new_column = @column
while offset > 0
line_text = source_text.lines[new_lineno - 1]
if new_column + offset >= line_text.length
advanced = line_text.length - new_column
offset -= advanced
new_lineno += 1
new_column = 0
else
new_column += offset
break
end
end
CodeLocation.new(new_lineno, new_column)
end
def <=>(other)
ret = @lineno <=> other.lineno
return ret if ret != 0
@column <=> other.column
end
include Comparable
end
class CodeRange
def initialize(first, last)
@first, @last = first, last
end
def inspect
"%p-%p" % [@first, @last]
end
attr_reader :first
attr_reader :last
def self.from_lsp(lsp_range)
CodeRange.new(CodeLocation.from_lsp(lsp[:start]), CodeLocation.from_lsp(lsp[:end]))
end
def self.from_rbs(rbs_loc)
CodeRange.new(
CodeLocation.new(rbs_loc.start_line, rbs_loc.start_column),
CodeLocation.new(rbs_loc.end_line, rbs_loc.end_column),
)
end
def to_lsp
{ start: @first.to_lsp, end: @last.to_lsp }
end
def contain_loc?(loc)
@first <= loc && loc < @last
end
def contain?(other)
@first <= other.first && other.last <= @last
end
def overlap?(other)
if @first <= other.first
return @last > other.first
else
return @first < other.last
end
end
end
class CodeRangeTable
Entry = Struct.new(:range, :value, :children)
class Entry
def inspect
"[%p, %p, %p]" % [range, value, children]
end
end
def initialize(list = [])
@list = list # Array[Entry]
end
def []=(range, value)
i_b = @list.bsearch_index {|e| e.range.last > range.first } || @list.size
i_e = @list.bsearch_index {|e| e.range.first >= range.last } || @list.size
if i_b < i_e
# for all i in i_b...i_e, @list[i] overlaps with the range
if i_e - i_b == 1
if range.contain?(@list[i_b].range)
@list[i_b] = Entry[range, value, CodeRangeTable.new(@list[i_b, 1])]
elsif @list[i_b].range.contain?(range)
@list[i_b].children[range] = value
else
raise
end
else
if range.contain?(@list[i_b].range) && range.contain?(@list[i_e - 1].range)
@list[i_b...i_e] = [Entry[range, value, CodeRangeTable.new(@list[i_b...i_e])]]
else
raise
end
end
else
@list[i_b, 0] = [Entry[range, value, CodeRangeTable.new]]
end
end
def [](loc)
e = @list.bsearch {|e| e.range.last > loc }
if e && e.range.contain_loc?(loc)
return e.children[loc] || e.value
end
return nil
end
end
end
if $0 == __FILE__
include TypeProf
cr1 = CodeRange.new(CodeLocation.new(1, 0), CodeLocation.new(1, 2))
cr2 = CodeRange.new(CodeLocation.new(1, 2), CodeLocation.new(1, 4))
cr3 = CodeRange.new(CodeLocation.new(2, 0), CodeLocation.new(2, 2))
cr4 = CodeRange.new(CodeLocation.new(2, 3), CodeLocation.new(2, 5))
cr1and2 = CodeRange.new(CodeLocation.new(1, 0), CodeLocation.new(1, 5))
cr3and4 = CodeRange.new(CodeLocation.new(2, 0), CodeLocation.new(2, 5))
[[cr1, "A"], [cr2, "B"], [cr3, "C"], [cr4, "D"], [cr1and2, "AB"], [cr3and4, "CD"]].permutation do |ary|
tbl = CodeRangeTable.new
ary.each do |cr, v|
tbl[cr] = v
end
values = []
[1, 2].each do |lineno|
(0..5).each do |column|
values << tbl[CodeLocation.new(lineno, column)]
end
end
raise if values != ["A", "A", "B", "B", "AB", nil, "C", "C", "CD", "D", "D", nil]
end
source = <<~EOS
AB
CDE
F
EOS
a_loc = CodeLocation.new(1, 0)
b_loc = a_loc.advance_cursor(1, source)
raise unless b_loc.inspect == "(1,1)"
c_loc = a_loc.advance_cursor(3, source)
raise unless c_loc.inspect == "(2,0)"
f_loc = c_loc.advance_cursor(4, source)
raise unless f_loc.inspect == "(3,0)"
end