fix(lua): inspect_pos respect bufnr when get syntax info (#23098)

This commit is contained in:
Raphael 2023-04-16 17:50:32 +08:00 committed by GitHub
parent 6ca4fba97f
commit 2f779b94e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View File

@ -70,15 +70,18 @@ function vim.inspect_pos(bufnr, row, col, filter)
if filter.treesitter then
for _, capture in pairs(vim.treesitter.get_captures_at_pos(bufnr, row, col)) do
capture.hl_group = '@' .. capture.capture .. '.' .. capture.lang
table.insert(results.treesitter, resolve_hl(capture))
results.treesitter[#results.treesitter + 1] = resolve_hl(capture)
end
end
-- syntax
if filter.syntax then
for _, i1 in ipairs(vim.fn.synstack(row + 1, col + 1)) do
table.insert(results.syntax, resolve_hl({ hl_group = vim.fn.synIDattr(i1, 'name') }))
end
if filter.syntax and vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_buf_call(bufnr, function()
for _, i1 in ipairs(vim.fn.synstack(row + 1, col + 1)) do
results.syntax[#results.syntax + 1] =
resolve_hl({ hl_group = vim.fn.synIDattr(i1, 'name') })
end
end)
end
-- namespace id -> name map

View File

@ -12,17 +12,21 @@ describe('vim.inspect_pos', function()
it('it returns items', function()
local ret = exec_lua([[
local buf = vim.api.nvim_create_buf(true, false)
local buf1 = vim.api.nvim_create_buf(true, false)
local ns1 = vim.api.nvim_create_namespace("ns1")
local ns2 = vim.api.nvim_create_namespace("")
vim.api.nvim_set_current_buf(buf)
vim.api.nvim_buf_set_lines(0, 0, -1, false, {"local a = 123"})
vim.api.nvim_buf_set_lines(buf1, 0, -1, false, {"--commentline"})
vim.api.nvim_buf_set_option(buf, "filetype", "lua")
vim.api.nvim_buf_set_option(buf1, "filetype", "lua")
vim.api.nvim_buf_set_extmark(buf, ns1, 0, 10, { hl_group = "Normal" })
vim.api.nvim_buf_set_extmark(buf, ns2, 0, 10, { hl_group = "Normal" })
vim.cmd("syntax on")
return {buf, vim.inspect_pos(0, 0, 10)}
return {buf, vim.inspect_pos(0, 0, 10), vim.inspect_pos(buf1, 0, 10).syntax }
]])
local buf, items = unpack(ret)
local buf, items, other_buf_syntax = unpack(ret)
eq('', eval('v:errmsg'))
eq({
buffer = buf,
@ -73,6 +77,13 @@ describe('vim.inspect_pos', function()
},
},
}, items)
eq({
{
hl_group = 'luaComment',
hl_group_link = 'Comment',
},
}, other_buf_syntax)
end)
end)