Skip to content

Commit 56c8246

Browse files
authored
fix: explicitly pass treesitter language to get_node() (#795)
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()`. Co-authored-by: troiganto <[email protected]>
1 parent 7226791 commit 56c8246

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

lua/orgmode/colors/highlighter/stars.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function OrgStars:on_line(bufnr, line)
2222
local node = vim.treesitter.get_node({
2323
bufnr = bufnr,
2424
pos = { line, 0 },
25+
lang = 'org',
2526
})
2627

2728
if not node or node:type() ~= 'stars' then

lua/orgmode/ui/virtual_indent.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function VirtualIndent:set_indent(start_line, end_line, ignore_ts)
9595
start_line = start_line - 1
9696
end
9797

98-
local node_at_cursor = vim.treesitter.get_node()
98+
local node_at_cursor = vim.treesitter.get_node({ lang = 'org' })
9999
local tree_has_errors = false
100100
if node_at_cursor then
101101
tree_has_errors = node_at_cursor:tree():root():has_error()

lua/orgmode/utils/treesitter/init.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local query_cache = {}
66
function M.restart_highlights(bufnr)
77
bufnr = bufnr or 0
88
vim.treesitter.stop(bufnr)
9-
vim.treesitter.start(bufnr)
9+
vim.treesitter.start(bufnr, 'org')
1010
end
1111

1212
function M.parse_current_file()
@@ -18,12 +18,13 @@ end
1818
function M.get_node_at_cursor(cursor)
1919
M.parse_current_file()
2020
if not cursor then
21-
return vim.treesitter.get_node()
21+
return vim.treesitter.get_node({ lang = 'org' })
2222
end
2323

2424
return vim.treesitter.get_node({
2525
bufnr = 0,
2626
pos = { cursor[1] - 1, cursor[2] },
27+
lang = 'org',
2728
})
2829
end
2930

0 commit comments

Comments
 (0)