Skip to content

Commit

Permalink
fix: explicitly pass treesitter language to get_node()
Browse files Browse the repository at this point in the history
Fixes chipsenkbeil/org-roam.nvim#49

Org-roam offers a "node buffer", a sidebar that shows information such
as backlinks from other locations to the current headline/file. The node
buffer allows previewing the location of such a backlink. This inserts
orgmode syntax into a buffer with filetype `org-roam-node-buffer`.

If virtual indent is enabled, it'll attempt to deduce the virtual indent
of the preview. This in turn calls `closest_headline_node()`, which in
turn calls `get_node_at_cursor`, which in turn calls
`vim.treesitter.get_node()`.

If `get_node()` is called without an explicit treesitter language, it'll
attempt to deduce that language from the filetype. The filetype
`org-roam-node-buffer` isn't associated with the `org` language (and
I don't think it *should*), so this call fails.

This fixes the issue by explicitly passing `{ lang = 'org' }` in all
instances where the language *may* be deduced. I think this is
acceptable because if someone calls
`orgmode.utils.treesitter.get_node()` AFAIK is only ever called on text
that is org syntax.

While making this change, I grepped for other treesitter calls with an
optional language argument. Where I found them, I passed it explicitly
as well. AFAICT, this is only `Stars:on_line()` and
`ts_utils.restart_highlighting()`.
  • Loading branch information
nmadysa committed Aug 15, 2024
1 parent 7226791 commit 7a4d87d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions lua/orgmode/colors/highlighter/stars.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function OrgStars:on_line(bufnr, line)
local node = vim.treesitter.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()
local node_at_cursor = vim.treesitter.get_node({ lang = 'org' })
local tree_has_errors = false
if node_at_cursor then
tree_has_errors = node_at_cursor:tree():root():has_error()
Expand Down
5 changes: 3 additions & 2 deletions lua/orgmode/utils/treesitter/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local query_cache = {}
function M.restart_highlights(bufnr)
bufnr = bufnr or 0
vim.treesitter.stop(bufnr)
vim.treesitter.start(bufnr)
vim.treesitter.start(bufnr, 'org')
end

function M.parse_current_file()
Expand All @@ -18,12 +18,13 @@ end
function M.get_node_at_cursor(cursor)
M.parse_current_file()
if not cursor then
return vim.treesitter.get_node()
return vim.treesitter.get_node({ lang = 'org' })
end

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

Expand Down

0 comments on commit 7a4d87d

Please sign in to comment.