diff --git a/lua/Comment/ft.lua b/lua/Comment/ft.lua index 34afffa..2170645 100644 --- a/lua/Comment/ft.lua +++ b/lua/Comment/ft.lua @@ -277,7 +277,14 @@ end ---print('Lang:', tree:lang()) ---@usage ]] function ft.contains(tree, range) - for lang, child in pairs(tree:children()) do + if not tree then + return nil + end + local ok, children = pcall(tree.children, tree) + if not ok or not children then + return tree + end + for lang, child in pairs(children) do if lang ~= 'comment' and child:contains(range) then return ft.contains(child, range) end @@ -297,14 +304,21 @@ function ft.calculate(ctx) return ft.get(vim.bo.filetype, ctx.ctype) --[[ @as string ]] end - local lang = ft.contains(parser, { + local tree = ft.contains(parser, { ctx.range.srow - 1, ctx.range.scol, ctx.range.erow - 1, ctx.range.ecol, - }):lang() + }) + local lang = tree and tree:lang() - return ft.get(lang, ctx.ctype) or ft.get(vim.bo.filetype, ctx.ctype) --[[ @as string ]] + if lang then + local result = ft.get(lang, ctx.ctype) + if result then + return result + end + end + return ft.get(vim.bo.filetype, ctx.ctype) --[[ @as string ]] end ---@export ft diff --git a/lua/Comment/utils.lua b/lua/Comment/utils.lua index e95ee2c..d78322b 100644 --- a/lua/Comment/utils.lua +++ b/lua/Comment/utils.lua @@ -369,7 +369,7 @@ end ---@param ... unknown function U.catch(fn, ...) xpcall(fn, function(err) - vim.notify(string.format('[Comment.nvim] %s', err.msg), vim.log.levels.WARN) + vim.notify(string.format('[Comment.nvim] %s', tostring(err)), vim.log.levels.WARN) end, ...) end