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
|
-- Test suite for vim.pos
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 dedent = t.dedent
local clear = n.clear
local exec_lua = n.exec_lua
local insert = n.insert
describe('vim.pos', function()
before_each(clear)
it('creates a position', function()
local pos, buf = exec_lua(function()
local buf = vim.api.nvim_create_buf(false, true)
return vim.pos(buf, 3, 5), buf
end)
eq(3, pos[1])
eq(5, pos[2])
eq(buf, pos[3])
end)
it('creates a position with buf=0', function()
local pos, buf = exec_lua(function()
return vim.pos(0, 3, 5), vim.api.nvim_get_current_buf()
end)
eq(3, pos[1])
eq(5, pos[2])
eq(buf, pos[3])
end)
it('creates a position from the window cursor', function()
local pos, pos_default, buf = exec_lua(function()
vim.api.nvim_buf_set_lines(0, 0, -1, true, { 'first', 'second' })
vim.api.nvim_win_set_cursor(0, { 2, 3 })
return vim.pos.cursor(0), vim.pos.cursor(), vim.api.nvim_get_current_buf()
end)
eq({ 1, 3, buf }, pos)
eq({ 1, 3, buf }, pos_default)
end)
it('is modifiable', function()
local pos, buf = exec_lua(function()
local pos = vim.pos(-1, 5, 5)
pos.buf = 0
pos.row = 4
pos.col = 2
return pos, vim.api.nvim_get_current_buf()
end)
eq({ 4, 2, buf }, pos)
end)
it('comparisons by overloaded operators', function()
local buf = exec_lua(function()
return vim.api.nvim_create_buf(false, true)
end)
eq(
true,
exec_lua(function()
return vim.pos(buf, 3, 5) < vim.pos(buf, 4, 5)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(buf, 3, 5) <= vim.pos(buf, 3, 6)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(buf, 3, 5) > vim.pos(buf, 2, 5)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(buf, 3, 5) >= vim.pos(buf, 3, 5)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(buf, 3, 5) == vim.pos(buf, 3, 5)
end)
)
eq(
true,
exec_lua(function()
return vim.pos(buf, 3, 5) ~= vim.pos(buf, 3, 6)
end)
)
end)
it('converts between vim.Pos and lsp.Position', function()
local buf = exec_lua(function()
return vim.api.nvim_get_current_buf()
end)
insert('Neovim 是 Vim 的分支,专注于扩展性和可用性。')
local lsp_pos = exec_lua(function()
local pos = vim.pos(buf, 0, 36)
return pos:to_lsp('utf-16')
end)
eq({ line = 0, character = 20 }, lsp_pos)
local pos = exec_lua(function()
return vim.pos.lsp(buf, lsp_pos, 'utf-16')
end)
eq({
0,
36,
buf,
}, pos)
end)
it('converts between vim.Pos and buffer offset', function()
local buf = exec_lua(function()
return vim.api.nvim_get_current_buf()
end)
insert(dedent [[
first
second
third
]])
local offsets = exec_lua(function()
return {
vim.pos(buf, 0, 0):to_offset(),
vim.pos(buf, 0, 3):to_offset(),
vim.pos(buf, 1, 0):to_offset(),
vim.pos(buf, 3, 0):to_offset(),
}
end)
eq({ 0, 3, 6, 19 }, offsets)
local positions = exec_lua(function()
return {
vim.pos.offset(buf, 0),
vim.pos.offset(buf, 3),
vim.pos.offset(buf, 6),
vim.pos.offset(buf, 19),
}
end)
eq({
{ 0, 0, buf },
{ 0, 3, buf },
{ 1, 0, buf },
{ 3, 0, buf },
}, positions)
end)
end)
|