Code Monkey home page Code Monkey logo

Comments (25)

hrsh7th avatar hrsh7th commented on July 30, 2024 6
require'cmp'.setup {
  event = {
    on_confirm_done = function(entry)
      -- do something
    end
  }
}

I've added the API for it.

from nvim-cmp.

windwp avatar windwp commented on July 30, 2024 1
video.mp4

hi i want this feature it is similar to vscode when you select a item by press <cr>.
if that item kind is a 'function' it will automatic add bracket (

from nvim-cmp.

uga-rosa avatar uga-rosa commented on July 30, 2024 1

I'm not sure why, but mapping the <CR> from nvim-autopairs worked.

-    ["<cr>"] = cmp.mapping.confirm({
-      behavior = cmp.ConfirmBehavior.Replace,
-      select = true,
-    }),

+require("nvim-autopairs").setup({})
+require("nvim-autopairs.completion.cmp").setup({
+  map_cr = true,
+  map_complete = true,
+})

@windwp Thx!!

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024 1

I've fixed it! Thank you.
5cc6c61

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

I think you don't need any glue setting.

local npairs = require('nvim-autopairs')
npairs.setup {}
_G.cmp_npairs_cr = function ()
  return npairs.autopairs_cr()
end

vim.api.nvim_set_keymap("i", "<cr>", "v:lua.cmp_npairs_cr()", {expr = true, noremap = true})

The above configuration should be worked.

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

@uga-rosa
And please set up mapping option with require'cmp'.setup { ... }. It's specified in README.md

from nvim-cmp.

uga-rosa avatar uga-rosa commented on July 30, 2024

Don't work...
cmp

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

My minimal vimrc works as expected.

if has('vim_starting')
  set encoding=utf-8
endif
scriptencoding utf-8

if &compatible
  set nocompatible
endif

let s:plug_dir = expand('/tmp/plugged/vim-plug')
if !isdirectory(s:plug_dir)
  execute printf('!curl -fLo %s/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim', s:plug_dir)
end

execute 'set runtimepath+=' . s:plug_dir
call plug#begin(s:plug_dir)
Plug 'windwp/nvim-autopairs'
Plug 'hrsh7th/vim-vsnip'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-buffer'
call plug#end()
PlugInstall | quit


lua <<EOF
local cmp = require'cmp'
cmp.setup {
  -- You should change this example to your chosen snippet engine.
  snippet = {
    expand = function(args)
      -- You must install `vim-vsnip` if you set up as same as the following.
      vim.fn['vsnip#anonymous'](args.body)
    end
  },

  -- You must set mapping.
  mapping = {
    ['<C-p>'] = cmp.mapping.item.prev(),
    ['<C-n>'] = cmp.mapping.item.next(),
    ['<C-d>'] = cmp.mapping.scroll.up(),
    ['<C-f>'] = cmp.mapping.scroll.down(),
    ['<C-Space>'] = cmp.mapping.complete(),
    ['<C-e>'] = cmp.mapping.close(),
    ['<CR>'] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    })
  },

  -- You should specify your *installed* sources.
  sources = {
    { name = 'buffer' }
  },
}

require('nvim-autopairs').setup({
  disable_filetype = { "TelescopePrompt" , "vim" },
})

local remap = vim.api.nvim_set_keymap
local npairs = require('nvim-autopairs')

-- skip it, if you use another global object
_G.MUtils = {}

MUtils.completion_confirm=function()
  return npairs.autopairs_cr()
end


remap('i' , '<CR>','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true})
EOF

from nvim-cmp.

uga-rosa avatar uga-rosa commented on July 30, 2024

Hmmm. I can't find any strange settings, but it doesn't work.
I tried many things, but the only thing I could think of as the cause was packer.nvim. It doesn't seem to be a problem here.

from nvim-cmp.

windwp avatar windwp commented on July 30, 2024

@hrsh7th hi I use this code to auto add ( when the complete item is a function

is there any way i can do a similar thing with ncim-cmp?

https://github.com/windwp/nvim-autopairs/blob/d71b3f6060a056dd4d3830b6406fe7143691d631/lua/nvim-autopairs/completion/compe.lua#L72

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

I think it isn't necessary on currently nvim-cmp.

from nvim-cmp.

windwp avatar windwp commented on July 30, 2024

i can make a PR to make autocmd and export the last confirm item after confirm function is execute

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

@windwp What do you want? I think it isn't necessary.

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

OK. I've understand.
I think it should be supported in nvim-cmp itself (by user customizing).

require'cmp'.setup {
  item = {
    custom = function(completion_item)
      if completion_item.kind == cmp.lsp.CompletionItemKind.Method or Function then
        completion_item.insertText = completion_item.insertText + '($0)'
        completion_item.insertTextFormat = cmp.lsp.InsertTextFormat.Snippet
      end
      return completion_item
    end
  }
}

But I will add CmpConfirmDone autocmd.

from nvim-cmp.

windwp avatar windwp commented on July 30, 2024

ok thank i will checking it

from nvim-cmp.

uga-rosa avatar uga-rosa commented on July 30, 2024

I create minimal rc.
Can you see if you can reproduce it?

init.lua

local fn = vim.fn
local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"

if fn.empty(fn.glob(install_path)) > 0 then
  fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
end

require'plugins'
vim.cmd([[autocmd BufWritePost plugins.lua source <afile> | PackerCompile]])

lua/plugins.lua

vim.cmd([[packadd packer.nvim]])

require("packer").startup(function()
  use("wbthomason/packer.nvim")
  use({
    "hrsh7th/nvim-cmp",
    requires = {
      "hrsh7th/cmp-buffer",
    },
    config = function()
      local cmp = require("cmp")
      cmp.setup({
        mapping = {
          ["<C-p>"] = cmp.mapping.prev_item(),
          ["<C-n>"] = cmp.mapping.next_item(),
          ["<C-e>"] = cmp.mapping.close(),
          ["<cr>"] = cmp.mapping.confirm({
            behavior = cmp.ConfirmBehavior.Replace,
            select = true,
          }),
        },
        sources = {
          { name = "buffer" },
        },
      })
    end,
  })
  use({
    "windwp/nvim-autopairs",
    config = function()
      local npairs = require("nvim-autopairs")
      npairs.setup({})

      _G.MUtils = _G.MUtils or {}
      MUtils.completion_confirm = function()
        return npairs.autopairs_cr()
      end

      vim.api.nvim_set_keymap("i", "<cr>", "v:lua.MUtils.completion_confirm()", { expr = true, noremap = true })
    end,
  })
end)

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

I can't reproduce (Sorry. I don't use packer.nvim so I change it as vim-plug).

if has('vim_starting')
  set encoding=utf-8
endif
scriptencoding utf-8

if &compatible
  set nocompatible
endif

let s:plug_dir = expand('/tmp/plugged/vim-plug')
if !isdirectory(s:plug_dir)
  execute printf('!curl -fLo %s/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim', s:plug_dir)
end

execute 'set runtimepath+=' . s:plug_dir
call plug#begin(s:plug_dir)
Plug 'hrsh7th/vim-vsnip'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-buffer'
Plug 'windwp/nvim-autopairs'
call plug#end()
PlugInstall | quit

lua <<EOF

-- nvim-cmp
local cmp = require("cmp")
cmp.setup({
  mapping = {
    ["<C-p>"] = cmp.mapping.prev_item(),
    ["<C-n>"] = cmp.mapping.next_item(),
    ["<C-e>"] = cmp.mapping.close(),
    ["<cr>"] = cmp.mapping.confirm({
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    }),
  },
  sources = {
    { name = "buffer" },
  },
})

local npairs = require("nvim-autopairs")
npairs.setup({})

_G.MUtils = _G.MUtils or {}
MUtils.completion_confirm = function()
  return npairs.autopairs_cr()
end

vim.api.nvim_set_keymap("i", "<cr>", "v:lua.MUtils.completion_confirm()", { expr = true, noremap = true })
EOF

from nvim-cmp.

uga-rosa avatar uga-rosa commented on July 30, 2024

I was short on words.
I also wrote this using vim-plug and the problem did not occur. So I think it might be a compatibility issue with packer.nvim, and I want you to check it in other environments.

from nvim-cmp.

saadparwaiz1 avatar saadparwaiz1 commented on July 30, 2024

just tried this. can reproduce with packer.nvim but not vim-plug

from nvim-cmp.

windwp avatar windwp commented on July 30, 2024

hi i create a new setup function for nvim-cmp
just check readme of nvim-autopairs
i don't use packer but it should working

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

Hm... I will try with packer.nvim

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

I tried to use packer.nvim with the following config and it works as expected.

local install_path = vim.fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"

if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
  vim.fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
end

vim.cmd([[autocmd BufWritePost plugins.lua source <afile> | PackerCompile]])vim.cmd([[packadd packer.nvim]])

require("packer").startup(function()
  use("wbthomason/packer.nvim")
  use({
    "hrsh7th/nvim-cmp",
    requires = {
      "hrsh7th/cmp-buffer",
    },
    config = function()
      local cmp = require("cmp")
      cmp.setup({
        mapping = {
          ["<C-p>"] = cmp.mapping.prev_item(),
          ["<C-n>"] = cmp.mapping.next_item(),
          ["<C-e>"] = cmp.mapping.close(),
          ["<cr>"] = cmp.mapping.confirm({
            behavior = cmp.ConfirmBehavior.Replace,
            select = true,
          }),
        },
        sources = {
          { name = "buffer" },
        },
      })
    end,
  })
  use({
    "windwp/nvim-autopairs",
    config = function()
      local npairs = require("nvim-autopairs")
      npairs.setup({})

      _G.MUtils = _G.MUtils or {}
      MUtils.completion_confirm = function()
        return npairs.autopairs_cr()
      end

      vim.api.nvim_set_keymap("i", "<cr>", "v:lua.MUtils.completion_confirm()", { expr = true, noremap = true })
    end,
  })
end)

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

Hm... I still can't reproduce it with lua/plugins.lua pattern.

-- init.mini.lua
local fn = vim.fn
local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"

if fn.empty(fn.glob(install_path)) > 0 then
  fn.system({ "git", "clone", "https://github.com/wbthomason/packer.nvim", install_path })
end

require'plugins'
vim.cmd([[autocmd BufWritePost plugins.lua source <afile> | PackerCompile]])
-- lua/plugins.lua
vim.cmd([[packadd packer.nvim]])

require("packer").startup(function()
  use("wbthomason/packer.nvim")
  use({
    "hrsh7th/nvim-cmp",
    requires = {
      "hrsh7th/cmp-buffer",
    },
    config = function()
      local cmp = require("cmp")
      cmp.setup({
        mapping = {
          ["<C-p>"] = cmp.mapping.prev_item(),
          ["<C-n>"] = cmp.mapping.next_item(),
          ["<C-e>"] = cmp.mapping.close(),
          ["<cr>"] = cmp.mapping.confirm({
            behavior = cmp.ConfirmBehavior.Replace,
            select = true,
          }),
        },
        sources = {
          { name = "buffer" },
        },
      })
    end,
  })
  use({
    "windwp/nvim-autopairs",
    config = function()
      local npairs = require("nvim-autopairs")
      npairs.setup({})

      _G.MUtils = _G.MUtils or {}
      MUtils.completion_confirm = function()
        return npairs.autopairs_cr()
      end

      vim.api.nvim_set_keymap("i", "<cr>", "v:lua.MUtils.completion_confirm()", { expr = true, noremap = true })
    end,
  })
end)

from nvim-cmp.

hrsh7th avatar hrsh7th commented on July 30, 2024

Ah. I found the cause. I will fix it.

from nvim-cmp.

jabuxas avatar jabuxas commented on July 30, 2024

I still have this issue using lsp zero, which depends on nvim cmp.

-- ....
local cmp = require('cmp')
local cmp_select = { behavior = cmp.SelectBehavior.Select }
local cmp_mappings = lsp.defaults.cmp_mappings({
    ['<C-p>'] = cmp.mapping.select_prev_item(cmp_select),
    ['<C-n>'] = cmp.mapping.select_next_item(cmp_select),
    ['<CR>'] = cmp.mapping.confirm({ select = true }),
    ["<C-Space>"] = cmp.mapping.complete(),
})

-- .....

local cmp_autopairs = require("nvim-autopairs.completion.cmp")
local cmp_status_ok, cmp = pcall(require, "cmp")
if not cmp_status_ok then
    return
end
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done({}))

lsp.setup()

seems to be possible to use based on this guy's setup https://github.com/redxtech/dotfiles/blob/master/.config/nvim/lua/lsp.lua#L70, but i have no clue what is going wrong.

http://0x0.st/o7qA.mp4 video showing error

from nvim-cmp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.