hrsh7th / nvim-cmp

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

How To Use TAB to Confirm The First Result :( #1031

Closed NEX-S closed 2 years ago

NEX-S commented 2 years ago

FAQ

Announcement

Minimal reproducible full config

Im Freshman For neovim in Lua, and my English is bad :(, I was wondering how to use TAB to confirm compeletion in new configs. Maybe this sounds little bit foolish but i would appreaciate if any one can help me with this <3.

local present, cmp = pcall(require, "cmp")

if not present then
    return
end

vim.opt.completeopt = "menuone,noselect"

local function border(hl_name)
    return {
        { "╭", hl_name },
        { "─", hl_name },
        { "╮", hl_name },
        { "│", hl_name },
        { "╯", hl_name },
        { "─", hl_name },
        { "╰", hl_name },
        { "│", hl_name },
    }
end

local cmp_window = require "cmp.utils.window"

cmp_window.info_ = cmp_window.info
cmp_window.info = function(self)
    local info = self:info_()
    info.scrollable = false
    return info
end

local options = {
    window = {
        completion = {
            border = border "CmpBorder",
            winhighlight = "Normal:CmpPmenu,CursorLine:PmenuSel,Search:None",
        },
        documentation = {
            border = border "CmpDocBorder",
        },
    },
    snippet = {
        expand = function(args)
            require("luasnip").lsp_expand(args.body)
        end,
    },
    formatting = {
        format = function(_, vim_item)
            local icons = {
                Text = "",
                Method = "",
                Function = "",
                Constructor = "",
                Field = "ﰠ",
                Variable = "",
                Class = "ﴯ",
                Interface = "",
                Module = "",
                Property = "ﰠ",
                Unit = "塞",
                Value = "",
                Enum = "",
                Keyword = "",
                Snippet = "",
                Color = "",
                File = "",
                Reference = "",
                Folder = "",
                EnumMember = "",
                Constant = "",
                Struct = "פּ",
                Event = "",
                Operator = "",
                TypeParameter = "",
                Table = " ",
                Object = "",
                Tag = " ",
                Array = " ",
                Boolean = "蘒",
                Number = "",
                String = "",
                Calendar = " ",
                Watch = "",
            }
            vim_item.kind = string.format("%s %s", icons[vim_item.kind], vim_item.kind)

            return vim_item
        end,
    },
    mapping = {
        ["<A-k>"] = cmp.mapping.select_prev_item(),
        ["<A-j>"] = cmp.mapping.select_next_item(),
        ["<C-d>"] = cmp.mapping.scroll_docs(-4),
        ["<C-f>"] = cmp.mapping.scroll_docs(4),
        ["<C-Space>"] = cmp.mapping.complete(),
        ["<C-e>"] = cmp.mapping.close(),
        ["<CR>"] = cmp.mapping.confirm {
            behavior = cmp.ConfirmBehavior.Insert,
            select = true,
        },
        ["<Tab>"] = cmp.mapping(function(fallback)
            if cmp.visible() then
                cmp.select_next_item()
            elseif require("luasnip").expand_or_jumpable() then
                vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<Plug>luasnip-expand-or-jump", true, true, true), "")
            else
                fallback()
            end
        end, {
        "i",
        "s",
    }),
    ["<S-Tab>"] = cmp.mapping(function(fallback)
        if cmp.visible() then
            cmp.select_prev_item()
        elseif require("luasnip").jumpable(-1) then
            vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<Plug>luasnip-jump-prev", true, true, true), "")
        else
            fallback()
        end
    end, {
    "i",
    "s",
}),
   },
   sources = {
       { name = "luasnip" },
       { name = "nvim_lsp" },
       { name = "buffer" },
       { name = "nvim_lua" },
       { name = "path" },
   },
}

cmp.setup(options)

Description

.

Steps to reproduce

.

Expected behavior

.

Actual behavior

.

Additional context

.

NEX-S commented 2 years ago

sorry i dont know how to change lable in github...

edwarmv commented 2 years ago

You can try changing your TAB mapping with this.

["<Tab>"] = cmp.mapping(function(fallback)
  if cmp.visible() then
    cmp.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = true })
  elseif require("luasnip").expand_or_jumpable() then
    vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<Plug>luasnip-expand-or-jump", true, true, true), "")
  else
    fallback()
  end
end, {
  "i",
  "s",
})
NEX-S commented 2 years ago

okay, i have changed my nvim-cmp's config and got this,

Error executing vim.schedule lua callback: ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:180: Expected lua string                                                           
stack traceback:
        [C]: in function 'nvim_buf_get_keymap'
        ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:180: in function 'get_map'
        ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:104: in function 'listen'
        ...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/core.lua:143: in function 'prepare'
        ...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/init.lua:292: in function 'callback'
        .../site/pack/packer/start/nvim-cmp/lua/cmp/utils/async.lua:122: in function <.../site/pack/packer/start/nvim-cmp/lua/cmp/utils/async.lua:120>

my config is


vim.opt.completeopt = 'menu,menuone,noselect'

-- Setup nvim-cmp.
local cmp = require'cmp'

cmp.setup {
    snippet = {
        -- REQUIRED - you must specify a snippet engine
        expand = function(args)
            require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
        end,
    },
    window = {
        -- completion = cmp.config.window.bordered(),
        -- documentation = cmp.config.window.bordered(),
    },
    mapping = cmp.mapping.preset.insert({
        ['<A-j>'] = cmp.mapping.select_prev_item(),
        ['<A-k>'] = cmp.mapping.select_next_item(),
        ['<A-l>'] = cmp.mapping.confirm({ select = true }),

        ['<C-e>'] = cmp.mapping({
            i = cmp.mapping.abort(),
            c = cmp.mapping.close(),
        }),

        ['<C-d>'] = cmp.mapping.scroll_docs(-4),
        ['<C-f>'] = cmp.mapping.scroll_docs(4),

        ['<A-Space>'] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
        ['<CR>'] = cmp.mapping.confirm {
            select = false,
        },
        ["<Tab>"] = cmp.mapping(
        function(fallback)
            if cmp.visible() then
                cmp.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = true })
            elseif require("luasnip").expand_or_jumpable() then
                vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<Plug>luasnip-expand-or-jump", true, true, true), "")
            else
                fallback()
            end
        end, { "i", "s" }
        ),
        sources = cmp.config.sources({
            { name = 'nvim_lsp' },      -- LSP COMPLETION
            { name = 'path' },          -- PATH COMPLETION
            { name = 'nvim_lua' },      -- LUA API COMPLETION
            { name = 'buffer' },        -- BUFFER COMPLETION

            -- { name = 'vsnip' }, -- For vsnip users.
            -- { name = 'luasnip' }, -- For luasnip users.
            -- { name = 'ultisnips' }, -- For ultisnips users.
            -- { name = 'snippy' }, -- For snippy users.
        })
    })
}

I've tried to use comment to locate the which config cause the error and found that if i comment the

        sources = cmp.config.sources({
            { name = 'nvim_lsp' },      -- LSP COMPLETION
            { name = 'path' },          -- PATH COMPLETION
            { name = 'nvim_lua' },      -- LUA API COMPLETION
            { name = 'buffer' },        -- BUFFER COMPLETION

the error has been fixed, buf my compeletion is not working... Am i config the nvim-cmp correct? Thanks for your reply, best wish.

NEX-S commented 2 years ago

fixed thank you <3.

JuanZoran commented 2 years ago

May I asking you for how to fixed that? I seem have the same problem now....

NEX-S commented 2 years ago

May I asking you for how to fixed that? I seem have the same problem now....

I've forgotten how to fix that :( it was a long time ago and now I'm using my own plugin, but you can still check out my old cmp's config in

https://github.com/NEX-S/nvim-config/blob/main/lua/plugins/nvim-cmp.lua

that should work now too.

if you still have questions, you can ask me on WeChat veperx good luck :smile_cat:

JuanZoran commented 2 years ago

Thank you for that, and I have figured out the problem. once I remap the for cmp, then the bug happend, I don't konw why.... and I just disabled the for cmp keymap....

leongit78 commented 1 month ago

fixed thank you <3.

okay, i have changed my nvim-cmp's config and got this,

Error executing vim.schedule lua callback: ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:180: Expected lua string                                                           
stack traceback:
        [C]: in function 'nvim_buf_get_keymap'
        ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:180: in function 'get_map'
        ...site/pack/packer/start/nvim-cmp/lua/cmp/utils/keymap.lua:104: in function 'listen'
        ...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/core.lua:143: in function 'prepare'
        ...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/init.lua:292: in function 'callback'
        .../site/pack/packer/start/nvim-cmp/lua/cmp/utils/async.lua:122: in function <.../site/pack/packer/start/nvim-cmp/lua/cmp/utils/async.lua:120>

my config is


vim.opt.completeopt = 'menu,menuone,noselect'

-- Setup nvim-cmp.
local cmp = require'cmp'

cmp.setup {
    snippet = {
        -- REQUIRED - you must specify a snippet engine
        expand = function(args)
            require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
        end,
    },
    window = {
        -- completion = cmp.config.window.bordered(),
        -- documentation = cmp.config.window.bordered(),
    },
    mapping = cmp.mapping.preset.insert({
        ['<A-j>'] = cmp.mapping.select_prev_item(),
        ['<A-k>'] = cmp.mapping.select_next_item(),
        ['<A-l>'] = cmp.mapping.confirm({ select = true }),

        ['<C-e>'] = cmp.mapping({
            i = cmp.mapping.abort(),
            c = cmp.mapping.close(),
        }),

        ['<C-d>'] = cmp.mapping.scroll_docs(-4),
        ['<C-f>'] = cmp.mapping.scroll_docs(4),

        ['<A-Space>'] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
        ['<CR>'] = cmp.mapping.confirm {
            select = false,
        },
        ["<Tab>"] = cmp.mapping(
        function(fallback)
            if cmp.visible() then
                cmp.confirm({ behavior = cmp.ConfirmBehavior.Insert, select = true })
            elseif require("luasnip").expand_or_jumpable() then
                vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<Plug>luasnip-expand-or-jump", true, true, true), "")
            else
                fallback()
            end
        end, { "i", "s" }
        ),
        sources = cmp.config.sources({
            { name = 'nvim_lsp' },      -- LSP COMPLETION
            { name = 'path' },          -- PATH COMPLETION
            { name = 'nvim_lua' },      -- LUA API COMPLETION
            { name = 'buffer' },        -- BUFFER COMPLETION

            -- { name = 'vsnip' }, -- For vsnip users.
            -- { name = 'luasnip' }, -- For luasnip users.
            -- { name = 'ultisnips' }, -- For ultisnips users.
            -- { name = 'snippy' }, -- For snippy users.
        })
    })
}

I've tried to use comment to locate the which config cause the error and found that if i comment the

        sources = cmp.config.sources({
            { name = 'nvim_lsp' },      -- LSP COMPLETION
            { name = 'path' },          -- PATH COMPLETION
            { name = 'nvim_lua' },      -- LUA API COMPLETION
            { name = 'buffer' },        -- BUFFER COMPLETION

the error has been fixed, buf my compeletion is not working... Am i config the nvim-cmp correct? Thanks for your reply, best wish.

Thanks a lot man! I really needed this keybindings