Skip to content

Commit 7627727

Browse files
committed
fix: dont panic if not treesitter-parser is found
1 parent 1c315a0 commit 7627727

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lua/Comment/ft.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local A = vim.api
2+
13
---Common commentstring shared b/w mutliple languages
24
local M = {
35
cxx_l = '//%s',
@@ -82,8 +84,13 @@ end
8284
---@param ctx Ctx
8385
---@return string
8486
function ft.calculate(ctx)
85-
local buf = vim.api.nvim_get_current_buf()
86-
local langtree = vim.treesitter.get_parser(buf)
87+
local buf = A.nvim_get_current_buf()
88+
local ok, langtree = pcall(vim.treesitter.get_parser, buf)
89+
local buf_type = A.nvim_buf_get_option(buf, 'filetype')
90+
91+
if not ok then
92+
return ft.get(buf_type, ctx.ctype)
93+
end
8794

8895
local range = {
8996
ctx.range.srow - 1,
@@ -102,7 +109,7 @@ function ft.calculate(ctx)
102109
end
103110
end
104111

105-
return found or ft.get(vim.api.nvim_buf_get_option(buf, 'filetype'), ctx.ctype)
112+
return found or ft.get(buf_type, ctx.ctype)
106113
end
107114

108115
return setmetatable(ft, {

0 commit comments

Comments
 (0)