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 /test/functional/treesitter/language_spec.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 'test/functional/treesitter/language_spec.lua')
-rw-r--r-- | test/functional/treesitter/language_spec.lua | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/test/functional/treesitter/language_spec.lua b/test/functional/treesitter/language_spec.lua index 6c211049f0..b9934a2e5f 100644 --- a/test/functional/treesitter/language_spec.lua +++ b/test/functional/treesitter/language_spec.lua @@ -8,6 +8,7 @@ local exec_lua = n.exec_lua local pcall_err = t.pcall_err local matches = t.matches local insert = n.insert +local NIL = vim.NIL before_each(clear) @@ -15,10 +16,12 @@ describe('treesitter language API', function() -- error tests not requiring a parser library it('handles missing language', function() eq( - ".../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers", + '.../treesitter.lua:0: Parser not found.', pcall_err(exec_lua, "parser = vim.treesitter.get_parser(0, 'borklang')") ) + eq(NIL, exec_lua("return vim.treesitter._get_parser(0, 'borklang')")) + -- actual message depends on platform matches( "Failed to load parser for language 'borklang': uv_dlopen: .+", @@ -105,9 +108,10 @@ describe('treesitter language API', function() command('set filetype=borklang') -- Should throw an error when filetype changes to borklang eq( - ".../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers", + '.../treesitter.lua:0: Parser not found.', pcall_err(exec_lua, "new_parser = vim.treesitter.get_parser(0, 'borklang')") ) + eq(NIL, exec_lua("return vim.treesitter._get_parser(0, 'borklang')")) end ) |