bug: source:get_trigger_characters() returns nil instead of empty array
### Prerequisites
- [X] I am using the latest stable or nightly neovim version
- [X] I am using the latest version of the plugin
### Neovim Version
NVIM v0.11.0-dev-1326+g3bb2d02759-Homebrew
### care.nvim setup
```Lua
return {
"max397574/care.nvim",
dependencies = {
{
{ "romgrk/fzy-lua-native", name = "fzy" },
"max397574/care-cmp",
"saadparwaiz1/cmp_luasnip",
"L3MON4D3/LuaSnip",
"rafamadriz/friendly-snippets",
-- { "petertriho/cmp-git", opts = {}},
}
},
opts = {
ui = {
menu = { border = "none" },
docs_view = { border = "none" },
ghost_text = { position = "inline" },
},
snippet_expansion = function(body)
require("luasnip").lsp_expand(body)
end,
preselect = false,
},
init = function()
vim.keymap.set("i", "<C-space>", function() require("care").api.complete() end, { desc = "open completion menu" })
vim.keymap.set("i", "<CR>", function()
if require("care").api.get_index() ~= 0 then
require("care").api.confirm()
else
vim.api.nvim_feedkeys(vim.keycode("<CR>"), "n", false)
end
end)
vim.keymap.set("i", "<C-e>", "<Plug>(CareClose)", { desc = "Care menu close" })
vim.keymap.set("i", "<TAB>", "<Plug>(CareSelectNext)", { desc = "Care menu select next" })
vim.keymap.set("i", "<S-TAB>", "<Plug>(CareSelectPrev)", { desc = "Care menu select previous" })
vim.keymap.set("i", "<c-x><c-f>",
function()
require("care").api.complete(function(name) return name == "cmp_path" end)
end)
require("luasnip.loaders.from_vscode").lazy_load()
require("luasnip.loaders.from_snipmate").lazy_load()
end,
}
```
### Actual behavior
```
Error detected while processing TextChangedI Autocommands for "*":
Error executing lua callback: vim/shared.lua:0: t: expected table, got nil
stack traceback:
[C]: in function 'error'
vim/shared.lua: in function 'validate'
vim/shared.lua: in function 'tbl_contains'
...em/.local/share/nvim/lazy/care.nvim/lua/care/sources.lua:33: in function 'complete'
...ochem/.local/share/nvim/lazy/care.nvim/lua/care/core.lua:35: in function 'complete'
...ochem/.local/share/nvim/lazy/care.nvim/lua/care/core.lua:195: in function 'on_change'
...ochem/.local/share/nvim/lazy/care.nvim/lua/care/core.lua:162: in function <...ochem/.local/share/nvim/lazy/care.nvim/lua/care/core.lua:160>
```
### Expected behavior
No error
### Healthcheck
```markdown
care: require("care.health").check()
care.nvim ~
- Checking configuration...
- Format entry function:
- Format entry function returns correct value
- Alignments:
- All alignments are correct
-
- Checking dependencies...
- ERROR Critical: dependency 'fzy' not found
- Check installation instructions for you package manager in the documentation
```
### Steps to reproduce
1. Using [jqls](https://github.com/wader/jq-lsp)
2. nvim test.jq
3. LspStart
4. Start typing
### Log
On Insert Enter
Setting up core
Core: on_change
Context:
Core: Context not changed
Core: on_change
Context: c
### Other information
Although the comment in internal_source.lua says otherwise:
"(...) if the method exists on the source and otherwise just returns an empty table."
And the source code in source.lua doesn't seem like it could return nil:
```lua
function Source:get_trigger_characters()
local trigger_characters = {}
if self.source.get_trigger_characters then
return self.source.get_trigger_characters()
end
return trigger_characters
end
```
This fixes the error:
```diff
diff --git a/lua/care/sources.lua b/lua/care/sources.lua
index e5d6116..5714717 100644
--- a/lua/care/sources.lua
+++ b/lua/care/sources.lua
@@ -30,7 +30,7 @@ function care_sources.complete(context, source, callback)
---@type lsp.CompletionContext
local completion_context
if context.reason == 1 then
- if vim.tbl_contains(source:get_trigger_characters(), last_char) then
+ if vim.tbl_contains(source:get_trigger_characters() or {}, last_char) then
completion_context = {
triggerKind = 2,
triggerCharacter = last_char,
@@ -73,7 +73,7 @@ function care_sources.complete(context, source, callback)
end
elseif context.reason == 3 then
local char_before = string.match(context.line_before_cursor, "(.)%s*$")
- if vim.tbl_contains(source:get_trigger_characters(), char_before) then
+ if vim.tbl_contains(source:get_trigger_characters() or {}, char_before) then
completion_context = {
triggerKind = 2,
triggerCharacter = char_before,
```
### Minimal config
```Lua
vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()
require("lazy.minit").repro({
spec = {
{
"max397574/care.nvim",
-- dependencies = {
-- -- sources
-- -- if you have issues with luarocks
-- "romgrk/fzy-lua-native"
-- },
config = function()
-- keymappings, `require"care.config".setup(...)`
end
},
},
-- if you have issues with luarocks
-- pkg = { sources = { nil } },
})
```
关闭于 2024-12-10 2 条评论