Skip to content

Commit

Permalink
Use built-in ts function to get node at cursor position
Browse files Browse the repository at this point in the history
  • Loading branch information
kristijanhusak committed Sep 17, 2022
1 parent bf53c20 commit 4be6263
Showing 1 changed file with 6 additions and 27 deletions.
33 changes: 6 additions & 27 deletions lua/orgmode/utils/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,13 @@ end
---@param winnr? number
---@param ignore_injected_langs? boolean
function M.get_node_at_cursor(cursor, winnr, ignore_injected_langs)
winnr = winnr or 0
cursor = cursor or vim.api.nvim_win_get_cursor(winnr)
local cursor_range = { cursor[1] - 1, cursor[2] }

local buf = vim.api.nvim_win_get_buf(winnr)
local root_lang_tree = parsers.get_parser(buf)
if not root_lang_tree then
return
end

local root
if ignore_injected_langs then
for _, tree in ipairs(root_lang_tree:trees()) do
local tree_root = tree:root()
if tree_root and ts_utils.is_in_node_range(tree_root, cursor_range[1], cursor_range[2]) then
root = tree_root
break
end
end
else
root = ts_utils.get_root_for_position(cursor_range[1], cursor_range[2], root_lang_tree)
end

if not root then
return
if not cursor then
return ts_utils.get_node_at_cursor(winnr, ignore_injected_langs)
end

return root:named_descendant_for_range(cursor_range[1], cursor_range[2], cursor_range[1], cursor_range[2])
local buf = vim.api.nvim_win_get_buf(winnr or 0)
return vim.treesitter.get_node_at_position(buf, cursor[1] - 1, cursor[2], {
ignore_injections = ignore_injected_langs,
})
end

-- walks the tree to find a headline
Expand Down

0 comments on commit 4be6263

Please sign in to comment.