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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
-- Test suite for vim.range
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local describe, it, before_each = t.describe, t.it, t.before_each
local eq = t.eq
local clear = n.clear
local exec_lua = n.exec_lua
local insert = n.insert
describe('vim.range', function()
before_each(clear)
it('creates a range', function()
local range, buf = exec_lua(function()
local buf = vim.api.nvim_create_buf(false, true)
return vim.range(buf, 3, 5, 4, 6), buf
end)
eq(3, range[1])
eq(5, range[2])
eq(4, range[3])
eq(6, range[4])
eq(buf, range[5])
end)
it('creates a range with buf=0', function()
local range, buf = exec_lua(function()
return vim.range(0, 3, 5, 4, 6), vim.api.nvim_get_current_buf()
end)
eq(3, range[1])
eq(5, range[2])
eq(4, range[3])
eq(6, range[4])
eq(buf, range[5])
end)
it('creates a range from two positions', function()
local range, buf1 = exec_lua(function()
local buf = vim.api.nvim_create_buf(false, true)
return vim.range(vim.pos(buf, 3, 5), vim.pos(buf, 4, 6)), buf
end)
eq(3, range[1])
eq(5, range[2])
eq(4, range[3])
eq(6, range[4])
eq(buf1, range[5])
local buf2 = exec_lua(function()
return vim.api.nvim_create_buf(false, true)
end)
local success = exec_lua(function()
return pcall(function()
return vim.range(vim.pos(buf1, 3, 5), vim.pos(buf2, 4, 6))
end)
end)
eq(false, success)
end)
it('is modifiable', function()
local range, buf = exec_lua(function()
local range = vim.range(-1, 5, 5, 5, 5)
range.buf = 0
range.start_row = 4
range.start_col = 2
range.end_row = 6
range.end_col = 8
return range, vim.api.nvim_get_current_buf()
end)
eq({ 4, 2, 6, 8, buf }, range)
end)
it('converts between vim.Range and lsp.Range', function()
local buf = exec_lua(function()
return vim.api.nvim_get_current_buf()
end)
insert('Neovim 是 Vim 的分支,专注于扩展性和可用性。')
local lsp_range = exec_lua(function()
local range = vim.range(buf, 0, 10, 0, 36)
return range:to_lsp('utf-16')
end)
eq({
['start'] = { line = 0, character = 8 },
['end'] = { line = 0, character = 20 },
}, lsp_range)
local range = exec_lua(function()
return vim.range.lsp(buf, lsp_range, 'utf-16')
end)
eq({
0,
10,
0,
36,
buf,
}, range)
end)
it('converts between inclusive mark ranges ending on multibyte characters', function()
insert('🙂')
local range, mark_range = exec_lua(function()
vim.o.selection = 'inclusive'
local range = vim.range.mark(0, 1, 0, 1, 0)
return { range[1], range[2], range[3], range[4] }, { range:to_mark() }
end)
eq({ 0, 0, 0, 4 }, range)
eq({ 1, 0, 1, 0 }, mark_range)
end)
it('uses the preceding line for inclusive marks ending at column zero', function()
insert('abcdef\nx')
eq(
{ 1, 0, 1, 5 },
exec_lua(function()
vim.o.selection = 'inclusive'
return { vim.range(0, 0, 0, 1, 0):to_mark() }
end)
)
end)
it("converts between vim.Range and extmark on buffer's last line", function()
local buf = exec_lua(function()
return vim.api.nvim_get_current_buf()
end)
insert('Some text')
local extmark_range = {
exec_lua(function()
local range = vim.range(buf, 0, 0, 1, 0)
return range:to_extmark()
end),
}
eq({ 0, 0, 0, 9 }, extmark_range)
local range = exec_lua(function()
return vim.range.extmark(
buf,
extmark_range[1],
extmark_range[2],
extmark_range[3],
extmark_range[4]
)
end)
eq({ 0, 0, 0, 9, buf }, range)
local extmark_range2 = {
exec_lua(function()
local range2 = vim.range(buf, 0, 0, 0, 9)
return range2:to_extmark()
end),
}
eq({ 0, 0, 0, 9 }, extmark_range2)
local range2 = exec_lua(function()
return vim.range.extmark(
buf,
extmark_range2[1],
extmark_range2[2],
extmark_range2[3],
extmark_range2[4]
)
end)
eq({ 0, 0, 0, 9, buf }, range2)
end)
it('checks whether a range contains a position', function()
eq(
true,
exec_lua(function()
local buf = vim.api.nvim_create_buf(false, true)
return vim.range(buf, 0, 0, 1, 5):has(vim.pos(buf, 0, 1))
end)
)
end)
it('a range does not contain an empty range just outside it', function()
eq(
false,
exec_lua(function()
return vim.range(0, 0, 0, 0, 4):has(vim.range(0, 0, 0, 0, 0))
end)
)
eq(
false,
exec_lua(function()
return vim.range(0, 0, 0, 0, 4):has(vim.range(0, 0, 4, 0, 4))
end)
)
end)
it('an empty range contains no other range', function()
eq(
false,
exec_lua(function()
return vim.range(0, 1, 0, 1, 0):has(vim.range(0, 1, 0, 1, 0))
end)
)
eq(
false,
exec_lua(function()
return vim.range(0, 1, 0, 1, 0):has(vim.range(0, 1, 0, 2, 0))
end)
)
eq(
false,
exec_lua(function()
return vim.range(0, 1, 0, 1, 0):has(vim.range(0, 0, 0, 1, 0))
end)
)
end)
it('preserves single-character intersections', function()
insert('abc')
eq(
{ 0, 1, 0, 2 },
exec_lua(function()
local overlap = vim.range(0, 0, 0, 0, 2):intersect(vim.range(0, 0, 1, 0, 3))
return overlap and { overlap:to_extmark() } or nil
end)
)
end)
it('an empty range intersercts with no other range', function()
eq(
nil,
exec_lua(function()
return vim.range(0, 1, 0, 1, 0):intersect(vim.range(0, 1, 0, 1, 0))
end)
)
eq(
nil,
exec_lua(function()
return vim.range(0, 1, 0, 1, 0):intersect(vim.range(0, 1, 0, 2, 0))
end)
)
eq(
nil,
exec_lua(function()
return vim.range(0, 1, 0, 1, 0):intersect(vim.range(0, 0, 0, 1, 0))
end)
)
end)
it('empty range comparison semantics', function()
eq(
true,
exec_lua(function()
return vim.range(0, 0, 0, 0, 0) < vim.range(0, 0, 0, 0, 1)
end)
)
eq(
true,
exec_lua(function()
return vim.range(0, 1, 0, 1, 0) < vim.range(0, 1, 0, 1, 1)
end)
)
eq(
true,
exec_lua(function()
return vim.range(0, 1, 1, 1, 1) > vim.range(0, 1, 0, 1, 1)
end)
)
end)
it('1 byte wide range is not empty', function()
eq(
false,
exec_lua(function()
return vim.range(0, 1, 0, 1, 1):is_empty()
end)
)
end)
end)
|