mirror of
https://github.com/neovim/neovim.git
synced 2024-12-19 18:55:14 -07:00
fix(treesitter): set cursor position when opening inspector
This commit is contained in:
parent
a096165977
commit
315c711700
@ -181,6 +181,42 @@ local function set_dev_properties(w, b)
|
|||||||
vim.bo[b].filetype = 'query'
|
vim.bo[b].filetype = 'query'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Updates the cursor position in the inspector to match the node under the cursor.
|
||||||
|
---
|
||||||
|
--- @param pg TSTreeView
|
||||||
|
--- @param lang string
|
||||||
|
--- @param source_buf integer
|
||||||
|
--- @param inspect_buf integer
|
||||||
|
--- @param inspect_win integer
|
||||||
|
--- @param pos? { [1]: integer, [2]: integer }
|
||||||
|
local function set_inspector_cursor(pg, lang, source_buf, inspect_buf, inspect_win, pos)
|
||||||
|
api.nvim_buf_clear_namespace(inspect_buf, pg.ns, 0, -1)
|
||||||
|
|
||||||
|
local cursor_node = vim.treesitter.get_node({
|
||||||
|
bufnr = source_buf,
|
||||||
|
lang = lang,
|
||||||
|
pos = pos,
|
||||||
|
ignore_injections = false,
|
||||||
|
})
|
||||||
|
if not cursor_node then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local cursor_node_id = cursor_node:id()
|
||||||
|
for i, v in pg:iter() do
|
||||||
|
if v.id == cursor_node_id then
|
||||||
|
local start = v.depth
|
||||||
|
local end_col = start + #v.text
|
||||||
|
api.nvim_buf_set_extmark(inspect_buf, pg.ns, i - 1, start, {
|
||||||
|
end_col = end_col,
|
||||||
|
hl_group = 'Visual',
|
||||||
|
})
|
||||||
|
api.nvim_win_set_cursor(inspect_win, { i, 0 })
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
--- Write the contents of this View into {bufnr}.
|
--- Write the contents of this View into {bufnr}.
|
||||||
---
|
---
|
||||||
---@param bufnr integer Buffer number to write into.
|
---@param bufnr integer Buffer number to write into.
|
||||||
@ -307,6 +343,9 @@ function M.inspect_tree(opts)
|
|||||||
|
|
||||||
pg:draw(b)
|
pg:draw(b)
|
||||||
|
|
||||||
|
local cursor = api.nvim_win_get_cursor(win)
|
||||||
|
set_inspector_cursor(pg, opts.lang, buf, b, w, { cursor[1] - 1, cursor[2] })
|
||||||
|
|
||||||
api.nvim_buf_clear_namespace(buf, pg.ns, 0, -1)
|
api.nvim_buf_clear_namespace(buf, pg.ns, 0, -1)
|
||||||
api.nvim_buf_set_keymap(b, 'n', '<CR>', '', {
|
api.nvim_buf_set_keymap(b, 'n', '<CR>', '', {
|
||||||
desc = 'Jump to the node under the cursor in the source buffer',
|
desc = 'Jump to the node under the cursor in the source buffer',
|
||||||
@ -398,30 +437,7 @@ function M.inspect_tree(opts)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
api.nvim_buf_clear_namespace(b, pg.ns, 0, -1)
|
set_inspector_cursor(pg, opts.lang, buf, b, w)
|
||||||
|
|
||||||
local cursor_node = vim.treesitter.get_node({
|
|
||||||
bufnr = buf,
|
|
||||||
lang = opts.lang,
|
|
||||||
ignore_injections = false,
|
|
||||||
})
|
|
||||||
if not cursor_node then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local cursor_node_id = cursor_node:id()
|
|
||||||
for i, v in pg:iter() do
|
|
||||||
if v.id == cursor_node_id then
|
|
||||||
local start = v.depth
|
|
||||||
local end_col = start + #v.text
|
|
||||||
api.nvim_buf_set_extmark(b, pg.ns, i - 1, start, {
|
|
||||||
end_col = end_col,
|
|
||||||
hl_group = 'Visual',
|
|
||||||
})
|
|
||||||
api.nvim_win_set_cursor(w, { i, 0 })
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user