Skip to content

Commit e135361

Browse files
authored
fix(todo_comments): handle nil parser from get_parser on nvim 0.12 (#338)
Since neovim/neovim#37276 (released in 0.12), vim.treesitter.get_parser() returns nil instead of throwing when no parser is installed for the language. The pcall therefore succeeded with parser == nil, and the subsequent parser:parse() errored with "attempt to index local 'parser' (a nil value)" on any buffer whose filetype has no treesitter grammar. Drop the pcall and check the return value directly. Pass `{ error = false }` so 0.11 also returns nil instead of throwing, on 0.12+ the option is ignored.
1 parent 899e93f commit e135361

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lua/null-ls/builtins/diagnostics/todo_comments.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ local function get_document_root(bufnr, filetype)
1818
return
1919
end
2020

21-
local has_parser, parser = pcall(vim.treesitter.get_parser, bufnr, lang)
22-
if not has_parser then
21+
-- `error = false` is only needed for nvim 0.11; on 0.12+ get_parser never
22+
-- throws and returns nil when no parser is available.
23+
local parser = vim.treesitter.get_parser(bufnr, lang, { error = false })
24+
if not parser then
2325
log:debug("no parser available for lang " .. lang)
2426
return
2527
end

0 commit comments

Comments
 (0)