Skip to content

Commit d9e44e5

Browse files
authored
fix: add error checking for vim.treesitter.get_parser() (#525)
1 parent 7a2c97c commit d9e44e5

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

lua/nvim-autopairs/ts-conds.lua

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ conds.is_endwise_node = function(nodes)
1414
if nodes == nil then return true end
1515
if #nodes == 0 then return true end
1616

17-
vim.treesitter.get_parser():parse()
17+
local parser = vim.treesitter.get_parser(nil, nil, { error = false })
18+
if parser == nil then return end
19+
parser:parse()
1820
local target = vim.treesitter.get_node({ ignore_injections = false })
1921
if target ~= nil and utils.is_in_table(nodes, target:type()) then
2022
local text = ts_get_node_text(target) or {""}
@@ -56,11 +58,6 @@ conds.is_in_range = function(callback, position)
5658
)
5759
return function(opts)
5860
log.debug('is_in_range')
59-
-- `parser` will be a table (on success) or a string (error message)
60-
local _, parser = pcall(vim.treesitter.get_parser)
61-
if not type(parser) == 'string' then
62-
return
63-
end
6461
local cursor = position()
6562
assert(
6663
type(cursor) == 'table' and #cursor == 2,
@@ -70,7 +67,8 @@ conds.is_in_range = function(callback, position)
7067
local col = cursor[2]
7168

7269
local bufnr = 0
73-
local root_lang_tree = vim.treesitter.get_parser(bufnr)
70+
local root_lang_tree = vim.treesitter.get_parser(bufnr, nil, { error = false })
71+
if root_lang_tree == nil then return end
7472
local lang_tree = root_lang_tree:language_for_range({ line, col, line, col })
7573

7674
local result
@@ -109,7 +107,9 @@ conds.is_ts_node = function(nodes)
109107
log.debug('is_ts_node')
110108
if #nodes == 0 then return end
111109

112-
vim.treesitter.get_parser():parse()
110+
local parser = vim.treesitter.get_parser(nil, nil, { error = false })
111+
if parser == nil then return end
112+
parser:parse()
113113
local target = vim.treesitter.get_node({ ignore_injections = false })
114114
if target ~= nil and utils.is_in_table(nodes, target:type()) then
115115
return true
@@ -125,7 +125,9 @@ conds.is_not_ts_node = function(nodes)
125125
log.debug('is_not_ts_node')
126126
if #nodes == 0 then return end
127127

128-
vim.treesitter.get_parser():parse()
128+
local parser = vim.treesitter.get_parser(nil, nil, { error = false })
129+
if parser == nil then return end
130+
parser:parse()
129131
local target = vim.treesitter.get_node({ ignore_injections = false })
130132
if target ~= nil and utils.is_in_table(nodes, target:type()) then
131133
return false
@@ -138,7 +140,9 @@ conds.is_not_ts_node_comment = function()
138140
log.debug('not_in_ts_node_comment')
139141
if not opts.ts_node then return end
140142

141-
vim.treesitter.get_parser():parse()
143+
local parser = vim.treesitter.get_parser(nil, nil, { error = false })
144+
if parser == nil then return end
145+
parser:parse()
142146
local target = vim.treesitter.get_node({ ignore_injections = false })
143147
if target ~= nil and utils.is_in_table(opts.ts_node, target:type()) then
144148
return false
@@ -150,6 +154,7 @@ conds.is_not_in_context = function()
150154
return function(opts)
151155
local context = require("nvim-autopairs.ts-utils")
152156
.get_language_tree_at_position({ utils.get_cursor() })
157+
if context == nil then return end
153158
if not vim.tbl_contains(opts.rule.filetypes, context:lang()) then
154159
return false
155160
end

lua/nvim-autopairs/ts-utils.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ end)()
1515

1616

1717
--- Returns the language tree at the given position.
18-
---@return LanguageTree
18+
---@return vim.treesitter.LanguageTree|nil
1919
function M.get_language_tree_at_position(position)
2020
local language_tree = vim.treesitter.get_parser()
21+
if language_tree == nil then return nil end
2122
language_tree:for_each_tree(function(_, tree)
2223
if tree:contains(flatten({ position, position })) then
2324
language_tree = tree

tests/test_utils.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ _G.Test_withfile = function(test_data, cb)
133133
vim.bo.filetype = value.filetype
134134
end
135135
end
136-
local status, parser = pcall(vim.treesitter.get_parser, 0)
137-
if status then
136+
local parser = vim.treesitter.get_parser(0, nil, { error = false })
137+
if parser ~= nil then
138138
parser:parse(true)
139139
end
140140
vim.api.nvim_buf_set_lines(

0 commit comments

Comments
 (0)