Skip to content

Commit

Permalink
Check if parser installed with pcall ts.get_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
cshuaimin committed Oct 13, 2023
1 parent f6ab487 commit 35b280e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lua/ssr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function Ui.new()
local origin_buf = api.nvim_win_get_buf(self.origin_win)
local lang = ts.language.get_lang(vim.bo[origin_buf].filetype)
if not lang then
return u.notify("Treesitter parser not found, please try to install it with :TSInstall " .. self.lang)
return u.notify("Treesitter language not found")
end
self.lang = lang

Expand Down
6 changes: 4 additions & 2 deletions lua/ssr/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ end
---@param end_col number
---@return TSNode?
function M.node_for_range(buf, lang, start_row, start_col, end_row, end_col)
local parser = ts.get_parser(buf, lang)
return parser:parse()[1]:root():named_descendant_for_range(start_row, start_col, end_row, end_col)
local has_parser, parser = pcall(ts.get_parser, buf, lang)
if has_parser then
return parser:parse()[1]:root():named_descendant_for_range(start_row, start_col, end_row, end_col)
end
end

---@param buf buffer
Expand Down

0 comments on commit 35b280e

Please sign in to comment.