Skip to content

Commit

Permalink
chore(refactor): Add get_node treesitter helper
Browse files Browse the repository at this point in the history
Ensure we are always using correct language when getting the node
  • Loading branch information
kristijanhusak committed Aug 15, 2024
1 parent 56c8246 commit adf277c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lua/orgmode/colors/highlighter/stars.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local ts_utils = require('orgmode.utils.treesitter')
local config = require('orgmode.config')

---@class OrgStarsHighlighter
Expand All @@ -19,10 +20,9 @@ function OrgStars:on_line(bufnr, line)
return
end

local node = vim.treesitter.get_node({
local node = ts_utils.get_node({
bufnr = bufnr,
pos = { line, 0 },
lang = 'org',
})

if not node or node:type() ~= 'stars' then
Expand Down
2 changes: 1 addition & 1 deletion lua/orgmode/ui/virtual_indent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function VirtualIndent:set_indent(start_line, end_line, ignore_ts)
start_line = start_line - 1
end

local node_at_cursor = vim.treesitter.get_node({ lang = 'org' })
local node_at_cursor = tree_utils.get_node()
local tree_has_errors = false
if node_at_cursor then
tree_has_errors = node_at_cursor:tree():root():has_error()
Expand Down
12 changes: 9 additions & 3 deletions lua/orgmode/utils/treesitter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ function M.parse_current_file()
return vim.treesitter.get_parser(0, 'org', {}):parse()
end

---@param opts? vim.treesitter.get_node.Opts
function M.get_node(opts)
opts = opts or {}
opts.lang = opts.lang or 'org'
return vim.treesitter.get_node(opts)
end

---@param cursor? table
---@return TSNode | nil
function M.get_node_at_cursor(cursor)
M.parse_current_file()
if not cursor then
return vim.treesitter.get_node({ lang = 'org' })
return M.get_node()
end

return vim.treesitter.get_node({
return M.get_node({
bufnr = 0,
pos = { cursor[1] - 1, cursor[2] },
lang = 'org',
})
end

Expand Down

0 comments on commit adf277c

Please sign in to comment.