hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
7.88k stars 393 forks source link

How to prevent inserting the selected suggestion into the buffer before confirming it? #1852

Closed netrolite closed 6 months ago

netrolite commented 6 months ago

I'm looking to get the same behavior as in VSCode:

https://github.com/hrsh7th/nvim-cmp/assets/67281212/72e5e073-a2e9-451c-aac9-793f68f633c4

The reason I don't want to insert the selected suggestion before confirming it is because if there's no suggestion that I need and I close the completion menu, I don't want to have the last selected suggestion inserted into the buffer.

Haven't found any options that allow me to do that. Any help? I'm using the latest version (commit 04e0ca3)

Here's my current nvim-cmp config:

return {
  "hrsh7th/nvim-cmp",
  event = "InsertEnter",
  dependencies = {
    "hrsh7th/cmp-buffer",           -- completion source for text in buffer
    "hrsh7th/cmp-path",             -- completion source for file system paths
    "hrsh7th/cmp-nvim-lsp",         -- completion source for lsp
    "saadparwaiz1/cmp_luasnip",     -- completion source for snippets
    "L3MON4D3/LuaSnip",             -- snippet engine
    "rafamadriz/friendly-snippets", -- set of useful snippets
    "onsails/lspkind.nvim",         -- vs-code like pictograms
  },
  config = function()
    local cmp = require("cmp")
    local luasnip = require("luasnip")
    local lspkind = require("lspkind")

    -- loads vscode style snippets from installed plugins (e.g. friendly-snippets)
    -- you can also specify a custom directory to load them from a non-standard location
    -- see https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file#add-snippets
    require("luasnip.loaders.from_vscode").lazy_load()
    -- load snipmate snippets (from ~/.config/nvim/snippets)
    require("luasnip.loaders.from_snipmate").lazy_load()

    cmp.setup({
      snippet = { -- configure nvim-cmp to use luasnip for expanding snippets
        expand = function(args)
          luasnip.lsp_expand(args.body)
        end,
      },
      mapping = cmp.mapping.preset.insert({
        ["<C-j>"] = cmp.mapping.scroll_docs(-4),
        ["<C-k>"] = cmp.mapping.scroll_docs(4),
        ["<Tab>"] = cmp.mapping.confirm({
          behavior = cmp.ConfirmBehavior.Insert,
          select = true
        }),
      }),
      -- sources for autocompletion
      sources = cmp.config.sources({
        { name = "nvim_lsp" }, -- lsp
        { name = "luasnip" },  -- snippets
        { name = "path" },     -- file system paths
        { name = "buffer" }    -- buffer
      }),
      -- configure lspkind for vs-code like pictograms in completion menu
      formatting = {
        format = lspkind.cmp_format({ maxwidth = 50, ellipsis_char = "..." }),
      },
    })
  end,
}
Shougo commented 6 months ago

You should use ``cmp.mapping.select_next_item instead.

https://github.com/hrsh7th/nvim-cmp/blob/main/doc/cmp.txt#L176

netrolite commented 6 months ago

I ended up going with this:

["<C-p>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }),
["<C-n>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }),

Works perfectly now. Thank you!