feat(lua): conversion between vim and lsp position/range

This commit is contained in:
Yi Ming
2025-08-17 11:54:48 +08:00
parent 98f8224c19
commit 9f5b309d82
5 changed files with 251 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ local eq = t.eq
local clear = n.clear
local exec_lua = n.exec_lua
local insert = n.insert
describe('vim.pos', function()
before_each(clear)
@@ -67,4 +68,24 @@ describe('vim.pos', function()
end)
)
end)
it('supports conversion 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(0, 36, { buf = buf })
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({
buf = buf,
row = 0,
col = 36,
}, pos)
end)
end)