## 事象 以下のコードで`.filter`の前に`.map`を入力するケースを考える。 ```ts [1, 2, 3].filter((x) => x > 5); ``` `.`と`filter`の間にカーソルを移動し、`map`と入力する。 ![[Pasted image 20240317220538.png]] そこでEnterを押すと`filter`が`map`で置き換わってしまう。 ```ts [1, 2, 3].map((x) => x > 5); ``` 期待する結果は以下。 ```ts [1, 2, 3].mapfilter((x) => x > 5); ``` ## 原因 [[nvim-cmp]]のsetupにて、`mapping`で定義したアクションの`.confirm`に`{ behavior = cmp.ConfirmBehavior.Replace }` が指定されているため。 ```lua cmp.setup({ completion = { completeopt = "menu,menuone,noinsert", }, mapping = cmp.mapping.preset.insert({ ["<CR>"] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true, }), ``` 以下は `:help nvim-cmp`より。デフォルトは`Insert`だが`Replace`もオプションで指定でき、それが明示的に設定されていた。 ```lua *cmp.confirm* (option: cmp.ConfirmOption, callback: function) Accepts the currently selected completion item. If you didn't select any item and the option table contains `select = true`, nvim-cmp will automatically select the first item. You can control how the completion item is injected into the file through the `behavior` option: `behavior=cmp.ConfirmBehavior.Insert`: inserts the selected item and moves adjacent text to the right (default). `behavior=cmp.ConfirmBehavior.Replace`: replaces adjacent text with the selected item. >lua cmp.setup { mapping = { ["<CR>"] = cmp.mapping.confirm({ select = true, behavior = cmp.ConfirmBehavior.Replace }), } } ``` ## 解決方法 `behavior`を指定せず、デフォルト値を使う。 ```lua cmp.setup({ completion = { completeopt = "menu,menuone,noinsert", }, mapping = cmp.mapping.preset.insert({ ["<CR>"] = cmp.mapping.confirm({ select = true, }), ```