diff options
author | Riley Bruins <ribru17@hotmail.com> | 2024-09-13 05:09:11 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-13 05:09:11 -0700 |
commit | b9b408a56c7e607972beaa7214719ff1494e384c (patch) | |
tree | 92c415c133cd2d591000d314df0a026c49961e17 /runtime/lua/vim/treesitter/dev.lua | |
parent | 8654a9700690a715e35baa600bb982f4ea608ead (diff) | |
download | rneovim-b9b408a56c7e607972beaa7214719ff1494e384c.tar.gz rneovim-b9b408a56c7e607972beaa7214719ff1494e384c.tar.bz2 rneovim-b9b408a56c7e607972beaa7214719ff1494e384c.zip |
feat(treesitter): start moving get_parser to return nil #30313
**Problem:** `vim.treesitter.get_parser` will throw an error if no parser
can be found.
- This means the caller is responsible for wrapping it in a `pcall`,
which is easy to forget
- It also makes it slightly harder to potentially memoize `get_parser`
in the future
- It's a bit unintuitive since many other `get_*` style functions
conventionally return `nil` if no object is found (e.g. `get_node`,
`get_lang`, `query.get`, etc.)
**Solution:** Return `nil` if no parser can be found or created
- This requires a function signature change, and some new assertions in
places where the parser will always (or should always) be found.
- This commit starts by making this change internally, since it is
breaking. Eventually it will be rolled out to the public API.
Diffstat (limited to 'runtime/lua/vim/treesitter/dev.lua')
-rw-r--r-- | runtime/lua/vim/treesitter/dev.lua | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/runtime/lua/vim/treesitter/dev.lua b/runtime/lua/vim/treesitter/dev.lua index e89aea2b85..606d5b27b9 100644 --- a/runtime/lua/vim/treesitter/dev.lua +++ b/runtime/lua/vim/treesitter/dev.lua @@ -76,10 +76,9 @@ end --- ---@package function TSTreeView:new(bufnr, lang) - local ok, parser = pcall(vim.treesitter.get_parser, bufnr or 0, lang) - if not ok then - local err = parser --[[ @as string ]] - return nil, 'No parser available for the given buffer:\n' .. err + local parser = vim.treesitter._get_parser(bufnr or 0, lang) + if not parser then + return nil, 'No parser available for the given buffer.' end -- For each child tree (injected language), find the root of the tree and locate the node within @@ -539,7 +538,7 @@ local edit_ns = api.nvim_create_namespace('treesitter/dev-edit') local function update_editor_highlights(query_win, base_win, lang) local base_buf = api.nvim_win_get_buf(base_win) local query_buf = api.nvim_win_get_buf(query_win) - local parser = vim.treesitter.get_parser(base_buf, lang) + local parser = assert(vim.treesitter._get_parser(base_buf, lang)) api.nvim_buf_clear_namespace(base_buf, edit_ns, 0, -1) local query_content = table.concat(api.nvim_buf_get_lines(query_buf, 0, -1, false), '\n') @@ -596,8 +595,8 @@ function M.edit_query(lang) end vim.cmd(cmd) - local ok, parser = pcall(vim.treesitter.get_parser, buf, lang) - if not ok then + local parser = vim.treesitter._get_parser(buf, lang) + if not parser then return nil, 'No parser available for the given buffer' end lang = parser:lang() |