## 事象
`{}` と入力して左にカーソルを移動すると、[[nvim-cmp]]の候補として[[LuaSnip]]の候補だけがサジェストされてしまう。
![[2025-02-05-21-24-19.webm]]
- [[バッファ (Vim)|バッファ]]などの補完は表示されない
- 再現確率は環境にもよるが、10~20%くらい
候補の出現に気づかず `Enter` で改行をすると色々と面倒なことになる。
### 環境
| 対象 | バージョン |
| ------------ | ----------- |
| [[Ubuntu]] | 24.04.1 LTS |
| [[Neovim]] | 0.10.3 |
| [[nvim-cmp]] | `1250990` |
| [[LuaSnip]] | `c9b9a22` |
### 設定
[[nvim-cmp]]と[[LuaSnip]]に関する設定の[[Lua]]ファイル。
```lua
{
"hrsh7th/nvim-cmp",
dependencies = {
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
{ "hrsh7th/cmp-buffer" },
{ "hrsh7th/cmp-path" },
{ "hrsh7th/cmp-cmdline" },
{ "b0o/schemastore.nvim" },
{ "onsails/lspkind.nvim" },
},
event = "InsertEnter", -- Telescope起動時にロードされるが、逆にBufreadの負荷が分散されてGOOD
config = function()
local cmp = require("cmp")
local luasnip = require("luasnip")
require("luasnip.loaders.from_snipmate").lazy_load()
require("luasnip.loaders.from_lua").lazy_load()
cmp.setup({
completion = {
completeopt = "menu,menuone,noinsert",
},
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
["<F5>"] = cmp.mapping.complete(),
["<CR>"] = cmp.mapping.confirm({
select = true,
}),
["<C-p>"] = cmp.mapping.abort(),
["<Tab>"] = cmp.mapping(function(fallback)
if luasnip.expand_or_locally_jumpable() then
luasnip.expand_or_jump()
else
fallback()
end
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" }),
}),
sources = cmp.config.sources({
{
name = "nvim_lsp",
},
{ name = "luasnip" },
{ name = "buffer" },
{ name = "path" },
{ name = "lazydev", group_index = 0 },
}),
formatting = {
format = function(entry, item)
local color_item = require("nvim-highlight-colors").format(entry, { kind = item.kind })
item = require("lspkind").cmp_format({
mode = "symbol",
maxwidth = 50,
ellipsis_char = "...",
show_labelDetails = true,
})(entry, item)
if color_item.abbr_hl_group then
item.kind_hl_group = color_item.abbr_hl_group
item.kind = color_item.abbr
end
return item
end,
},
})
cmp.setup.cmdline(":", {
completion = {
completeopt = "menu,menuone,noinsert,noselect",
},
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" },
}, {
{
name = "cmdline",
option = {
ignore_cmds = { "Man", "!" },
},
},
}),
})
end,
}
```
## 原因
なぜ[[LuaSnip]]の候補だけが出現するのかは不明...。
## 解決方法
[[enabled (nvim-cmp)|enabled]]で暴発する括弧のパターンでは補完を表示しないようにする。
```lua
cmp.setup({
enabled = function()
local line = vim.api.nvim_get_current_line()
local col = vim.fn.col(".") - 1
if col <= 0 then
return true
end
local char_before_cursor = line:sub(col, col)
-- LuaSnipが暴発するので括弧に対しては補完が出ないようにする
if char_before_cursor:match("[{}()%[%]]") then
return false
end
return true
end,
})
```