mirror of
https://github.com/neovim/neovim.git
synced 2026-04-18 13:30:42 +00:00
feat(vim.pos)!: require buf param on vim.pos, vim.range #38665
Problem: `buf` is optional even though its needed to perform conversions and the ordering of `(buf, row, col)` is not consistent. Solution: make `buf` mandatory on `vim.range` and `vim.pos` and enforce the `buf, row, col` ordering
This commit is contained in:
@@ -10,19 +10,10 @@ local insert = n.insert
|
||||
describe('vim.pos', function()
|
||||
before_each(clear)
|
||||
|
||||
it('creates a position with or without optional fields', function()
|
||||
local pos = exec_lua(function()
|
||||
return vim.pos(3, 5)
|
||||
end)
|
||||
eq(3, pos[1])
|
||||
eq(5, pos[2])
|
||||
eq(nil, pos[3])
|
||||
|
||||
local buf = exec_lua(function()
|
||||
return vim.api.nvim_create_buf(false, true)
|
||||
end)
|
||||
pos = exec_lua(function()
|
||||
return vim.pos(3, 5, { buf = buf })
|
||||
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])
|
||||
@@ -30,40 +21,43 @@ describe('vim.pos', function()
|
||||
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(3, 5) < vim.pos(4, 5)
|
||||
return vim.pos(buf, 3, 5) < vim.pos(buf, 4, 5)
|
||||
end)
|
||||
)
|
||||
eq(
|
||||
true,
|
||||
exec_lua(function()
|
||||
return vim.pos(3, 5) <= vim.pos(3, 6)
|
||||
return vim.pos(buf, 3, 5) <= vim.pos(buf, 3, 6)
|
||||
end)
|
||||
)
|
||||
eq(
|
||||
true,
|
||||
exec_lua(function()
|
||||
return vim.pos(3, 5) > vim.pos(2, 5)
|
||||
return vim.pos(buf, 3, 5) > vim.pos(buf, 2, 5)
|
||||
end)
|
||||
)
|
||||
eq(
|
||||
true,
|
||||
exec_lua(function()
|
||||
return vim.pos(3, 5) >= vim.pos(3, 5)
|
||||
return vim.pos(buf, 3, 5) >= vim.pos(buf, 3, 5)
|
||||
end)
|
||||
)
|
||||
eq(
|
||||
true,
|
||||
exec_lua(function()
|
||||
return vim.pos(3, 5) == vim.pos(3, 5)
|
||||
return vim.pos(buf, 3, 5) == vim.pos(buf, 3, 5)
|
||||
end)
|
||||
)
|
||||
eq(
|
||||
true,
|
||||
exec_lua(function()
|
||||
return vim.pos(3, 5) ~= vim.pos(3, 6)
|
||||
return vim.pos(buf, 3, 5) ~= vim.pos(buf, 3, 6)
|
||||
end)
|
||||
)
|
||||
end)
|
||||
@@ -74,7 +68,7 @@ describe('vim.pos', function()
|
||||
end)
|
||||
insert('Neovim 是 Vim 的分支,专注于扩展性和可用性。')
|
||||
local lsp_pos = exec_lua(function()
|
||||
local pos = vim.pos(0, 36, { buf = buf })
|
||||
local pos = vim.pos(buf, 0, 36)
|
||||
return pos:to_lsp('utf-16')
|
||||
end)
|
||||
eq({ line = 0, character = 20 }, lsp_pos)
|
||||
@@ -95,25 +89,25 @@ describe('vim.pos', function()
|
||||
insert('Some text')
|
||||
local extmark_pos = {
|
||||
exec_lua(function()
|
||||
local pos = vim.pos(1, 0, { buf = buf })
|
||||
local pos = vim.pos(buf, 1, 0)
|
||||
return pos:to_extmark()
|
||||
end),
|
||||
}
|
||||
eq({ 0, 9 }, extmark_pos)
|
||||
local pos = exec_lua(function()
|
||||
return vim.pos.extmark(extmark_pos[1], extmark_pos[2], { buf = buf })
|
||||
return vim.pos.extmark(buf, extmark_pos[1], extmark_pos[2])
|
||||
end)
|
||||
eq({ 0, 9, buf }, pos)
|
||||
|
||||
local extmark_pos2 = {
|
||||
exec_lua(function()
|
||||
local pos2 = vim.pos(0, 9, { buf = buf })
|
||||
local pos2 = vim.pos(buf, 0, 9)
|
||||
return pos2:to_extmark()
|
||||
end),
|
||||
}
|
||||
eq({ 0, 9 }, extmark_pos2)
|
||||
local pos2 = exec_lua(function()
|
||||
return vim.pos.extmark(extmark_pos2[1], extmark_pos2[2], { buf = buf })
|
||||
return vim.pos.extmark(buf, extmark_pos2[1], extmark_pos2[2])
|
||||
end)
|
||||
eq({ 0, 9, buf }, pos2)
|
||||
end)
|
||||
|
||||
@@ -10,40 +10,27 @@ local insert = n.insert
|
||||
describe('vim.range', function()
|
||||
before_each(clear)
|
||||
|
||||
it('creates a range with or without optional fields', function()
|
||||
local range = exec_lua(function()
|
||||
return vim.range(3, 5, 4, 6)
|
||||
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(nil, range[5])
|
||||
local buf = exec_lua(function()
|
||||
return vim.api.nvim_create_buf(false, true)
|
||||
end)
|
||||
range = exec_lua(function()
|
||||
return vim.range(3, 5, 4, 6, { buf = buf })
|
||||
end)
|
||||
eq(buf, range[5])
|
||||
end)
|
||||
|
||||
it('creates a range from two positions when optional fields are not matched', function()
|
||||
local range = exec_lua(function()
|
||||
return vim.range(vim.pos(3, 5), vim.pos(4, 6))
|
||||
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(nil, range[5])
|
||||
|
||||
local buf1 = exec_lua(function()
|
||||
return vim.api.nvim_create_buf(false, true)
|
||||
end)
|
||||
range = exec_lua(function()
|
||||
return vim.range(vim.pos(3, 5, { buf = buf1 }), vim.pos(4, 6, { buf = buf1 }))
|
||||
end)
|
||||
eq(buf1, range[5])
|
||||
|
||||
local buf2 = exec_lua(function()
|
||||
@@ -51,7 +38,7 @@ describe('vim.range', function()
|
||||
end)
|
||||
local success = exec_lua(function()
|
||||
return pcall(function()
|
||||
return vim.range(vim.pos(3, 5, { buf = buf1 }), vim.pos(4, 6, { buf = buf2 }))
|
||||
return vim.range(vim.pos(buf1, 3, 5), vim.pos(buf2, 4, 6))
|
||||
end)
|
||||
end)
|
||||
eq(success, false)
|
||||
@@ -63,7 +50,7 @@ describe('vim.range', function()
|
||||
end)
|
||||
insert('Neovim 是 Vim 的分支,专注于扩展性和可用性。')
|
||||
local lsp_range = exec_lua(function()
|
||||
local range = vim.range(0, 10, 0, 36, { buf = buf })
|
||||
local range = vim.range(buf, 0, 10, 0, 36)
|
||||
return range:to_lsp('utf-16')
|
||||
end)
|
||||
eq({
|
||||
@@ -86,7 +73,8 @@ describe('vim.range', function()
|
||||
eq(
|
||||
true,
|
||||
exec_lua(function()
|
||||
return vim.range(0, 0, 1, 5):has(vim.pos(0, 1))
|
||||
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)
|
||||
@@ -95,7 +83,8 @@ describe('vim.range', function()
|
||||
eq(
|
||||
false,
|
||||
exec_lua(function()
|
||||
return vim.range(0, 0, 0, 4):has(vim.range(0, 0, 0, 0))
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
return vim.range(buf, 0, 0, 0, 4):has(vim.range(buf, 0, 0, 0, 0))
|
||||
end)
|
||||
)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user