Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tab does not work correctly in cmdline-completion #875

Closed
2 tasks done
rignet-bicteg opened this issue Apr 8, 2022 · 16 comments
Closed
2 tasks done

Tab does not work correctly in cmdline-completion #875

rignet-bicteg opened this issue Apr 8, 2022 · 16 comments
Labels
bug Something isn't working

Comments

@rignet-bicteg
Copy link

FAQ

  • I have checked the FAQ and it didn't resolve my problem.

Issues

  • I have checked existing issues and there are no open or closed issues with the same problem.

Neovim Version

6.1

Minimal reproducible full config

require('packer').startup(function()
  use 'wbthomason/packer.nvim'

  use {
    'hrsh7th/nvim-cmp',
    requires = {
      {'hrsh7th/cmp-nvim-lsp'},
      {'hrsh7th/cmp-buffer'},
      {'L3MON4D3/LuaSnip'},
      {'saadparwaiz1/cmp_luasnip'},
    }
  }
end)

local cmp = require('cmp')
cmp.setup({
  snippet = {
    expand = function(args)
      require('luasnip').lsp_expand(args.body)
    end,
  },
  mapping = {
    ['<C-n>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }),
    ['<C-p>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }),
    ['<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,
    }),
  },
  sources = cmp.config.sources(
    {
      { name = 'nvim_lsp' },
      { name = 'luasnip' },
    },
    {
      {
        name = 'buffer',
        option = {
          get_bufnrs = function()
            return vim.api.nvim_list_bufs()
          end
        }
      },
    }
  )
})

Description

Hello,

When I open a file and do :e <Tab> I see a I^ char inserted. No completion.

I'll upload a video to showcase as well.

Steps to reproduce

nvim -u min.lua
:e <Tab>

Expected behavior

See autocompletion of commands and paths

Actual behavior

^I inserted and no autocompletion

Additional context

No response

@rignet-bicteg rignet-bicteg added the bug Something isn't working label Apr 8, 2022
@hrsh7th
Copy link
Owner

hrsh7th commented Apr 8, 2022

Please do NOT use packer.nvim for reproduction.
I'll translate it to vim-plug version and check this.

@hrsh7th hrsh7th changed the title Autocompletion from Vim doesn't work Tab does not work correctly in cmdline-completion Apr 8, 2022
@hrsh7th
Copy link
Owner

hrsh7th commented Apr 8, 2022

Reproduced. I'll check it.

@rignet-bicteg
Copy link
Author

rignet-bicteg commented Apr 8, 2022

Thank you, here's the reproduction with vim-plug

call plug#begin()
    Plug 'neovim/nvim-lspconfig'
    Plug 'hrsh7th/cmp-nvim-lsp'
    Plug 'hrsh7th/cmp-buffer'
    Plug 'hrsh7th/nvim-cmp'
    Plug 'L3MON4D3/LuaSnip'
    Plug 'saadparwaiz1/cmp_luasnip'
call plug#end()

set completeopt=menu,menuone,noselect

lua <<EOF
    local cmp = require('cmp')

    cmp.setup({
    snippet = {
        expand = function(args)
        require('luasnip').lsp_expand(args.body)
        end,
    },
    mapping = {
        ['<C-n>'] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }),
        ['<C-p>'] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }),
        ['<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,
        }),
    },
    sources = cmp.config.sources(
        {
        { name = 'nvim_lsp' },
        { name = 'luasnip' },
        },
        {
        {
            name = 'buffer',
            option = {
            get_bufnrs = function()
                return vim.api.nvim_list_bufs()
            end
            }
        },
        }
    )
    })

    local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
EOF

@hrsh7th hrsh7th closed this as completed in 3192a0c Apr 8, 2022
@axieax
Copy link

axieax commented Apr 10, 2022

Hi @hrsh7th, not sure why this doesn't fix it for me. I've been trying to debug this for a couple of days now, and I've found that the specific commit which broke my config and caused similar behaviour is 801a9f9. My cmp setup config can be found here.

@gleachkr
Copy link

I'm affected by this as well. I think it has something to do with attaching a cmp.mapping to <Tab>. Removing this mapping in my configuration hides the problem. I notice that @axieax's example config also seems to map <Tab>.

@wookayin
Copy link
Contributor

wookayin commented Apr 13, 2022

3192a0c broke keymapping... See #888 and #880 for required changes (kinda workaround) in the configuration.

@axieax
Copy link

axieax commented Apr 13, 2022

@wookayin is the only workaround to disable the tab mapping completely? Although this fixes the ^I issue, it also means that I cannot use my <Tab> key in cmd mode anymore, preventing me from selecting entries for cmp-path and cmp-buffer in the command line (which was previously possible).

EDIT: note this still doesn't work despite the new breaking changes #231 (comment) (separating my config for cmdline mode with only preset command line mappings -> pressing <Tab> used to print ^I, but now it does nothing, not even bringing up the pmenu).

@vanarok
Copy link

vanarok commented Jun 22, 2022

This bug still exists even with the latest changes.

@kovasap
Copy link

kovasap commented Jul 29, 2022

This bug still seems to exist for me too, can this issue be reopened?

@doums
Copy link

doums commented Aug 14, 2022

I confirm, I still have this bug too. I use the following setup:

cmp.setup({
  mapping = {
    ['<tab>'] = tab_key,
    ['<S-tab>'] = stab_key,
    ['<Down>'] = cmp.mapping(
      cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }),
      { 'i' }
    ),
    ['<Up>'] = cmp.mapping(
      cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }),
      { 'i' }
    ),
    ['<M-p>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
    ['<M-o>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
    ['<C-space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
    ['<C-e>'] = cmp.mapping({
      i = cmp.mapping.abort(),
      c = cmp.mapping.close(),
    }),
    ['<cr>'] = cmp.mapping.confirm({ select = true }),
  },
  snippet = {
    expand = function(args)
      ls.lsp_expand(args.body)
    end,
  },
  completion = { completeopt = 'menu,menuone,noinsert' },
  sources = {
    { name = 'luasnip' },
    { name = 'nvim_lsp' },
    { name = 'nvim_lua' },
    { name = 'path' },
    { name = 'buffer' },
  },
  window = {
    completion = { border = nil },
    documentation = { border = { '', '', '', ' ', '', '', '', ' ' } },
  },
  formatting = {
    format = function(entry, vim_item)
      vim_item.menu = ({
        buffer = '⌈buf⌋',
        nvim_lsp = '⌈lsp⌋',
        luasnip = '⌈snip⌋',
        nvim_lua = '⌈lua⌋',
        path = '⌈path⌋',
      })[entry.source.name]
      return vim_item
    end,
  },
})

Edit Im able to fix the bug using the following changes

-- ...
local tab_key = cmp.mapping(function(fallback)
  if cmp.visible() then
    cmp.select_next_item()
  elseif ls.expand_or_jumpable() then
    ls.expand_or_jump()
  elseif has_word_before() then
    cmp.complete()
  else
    fallback()
  end
end, {
  'i',
  c = cmp.config.disable,  -- fix cmdline autocompletion, workaround
  's',
})

-- ...

-- ADD THIS
cmp.setup.cmdline(':', {
  mapping = cmp.mapping.preset.cmdline(), -- important!
  sources = {
    { name = 'nvim_lua' },
    { name = 'cmdline' },
  },
})
cmp.setup.cmdline('/', {
  mapping = cmp.mapping.preset.cmdline(), -- important!
  sources = {
    { name = 'buffer' },
  },
})

@topaxi
Copy link

topaxi commented Feb 23, 2023

Ended up here as well as cmdline command mode completion doesn't seem to work for me.

@camoz
Copy link

camoz commented Feb 23, 2023

I'm experiencing this, too. Btw I'm not using cmp's : completion (i.e. no cmp.setup.cmdline(':', {...})), but the native one.

For me, the issue only shows up after a while. When starting a new neovim instance, it always works as expected, but at some point the issue appears. I'm not able to find what exactly triggers it...

Can this issue be re-opened, please?

@hrsh7th
Copy link
Owner

hrsh7th commented Feb 23, 2023

Please creaye new issue with reproduction config.

I can't do anything without new indormation.

I'll ignore this thread if anyone does not provide reproduction full config.

@axieax
Copy link

axieax commented Feb 25, 2023

@camoz I’m still experiencing similar issues in my issue #1454 which @hrsh7th recently closed but is still unresolved. Perhaps it’s relevant to yours?

@mar-muel
Copy link

Hi all! I don't have any deep understanding of why this bug (?) occurs, so I'm not 100% sure whether the following would not break something else in your setup, but it worked for me!

My initial config (broken):

local lsp = require('lsp-zero')
local cmp = require('cmp')
local luasnip = require('luasnip')
local keymap = require('cmp.utils.keymap')

lsp.setup_nvim_cmp({
    mapping = {
        -- from: https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#intellij-like-mapping
        ["<Tab>"] = cmp.mapping(
            function(fallback)
                -- This little snippet will confirm with tab, and if no entry is selected, will confirm the first item
                if cmp.visible() then
                    local entry = cmp.get_selected_entry()
                    if not entry then
                        cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
                    else
                        cmp.confirm()
                    end
                elseif luasnip.expand_or_jumpable() then
                    luasnip.expand_or_jump()
                else
                    fallback()
                end
            end, {"i","s","c",}),
        ["<S-Tab>"] = cmp.mapping(function(fallback)
            if cmp.visible() then
                cmp.select_prev_item()
            elseif luasnip.jumpable(-1) then
                luasnip.jump(-1)
            else
                fallback()
            end
        end, {"i", "s"}),
        ['<CR>'] = cmp.mapping.confirm(),
        ['<C-n>'] = cmp.mapping.select_next_item(),
        ['<C-p>'] = cmp.mapping.select_prev_item(),
    }
})

Fixed version:

lsp.setup_nvim_cmp({
    mapping = {
        -- from: https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#intellij-like-mapping
        ["<Tab>"] = cmp.mapping(
            function(fallback)
                -- This little snippet will confirm with tab, and if no entry is selected, will confirm the first item
                if cmp.visible() then
                    print("cmp visible");
                    local entry = cmp.get_selected_entry()
                    if not entry then
                        cmp.select_next_item({ behavior = cmp.SelectBehavior.Select })
                    else
                        cmp.confirm()
                    end
                elseif luasnip.expand_or_jumpable() then
                    luasnip.expand_or_jump()
                else
                    if vim.fn.pumvisible() == 0 then
                        vim.api.nvim_feedkeys(keymap.t('<C-z>'), 'in', true)
                    else
                        vim.api.nvim_feedkeys(keymap.t('<C-n>'), 'in', true)
                    end
                end
            end, {"i","s","c",}),
        ["<S-Tab>"] = cmp.mapping(function(fallback)
            if cmp.visible() then
                cmp.select_prev_item()
            elseif luasnip.jumpable(-1) then
                luasnip.jump(-1)
            else
                if vim.fn.pumvisible() == 0 then
                    vim.api.nvim_feedkeys(keymap.t('<C-z><C-p><C-p>'), 'in', true)
                else
                    vim.api.nvim_feedkeys(keymap.t('<C-p>'), 'in', true)
                end
            end
        end, {"i", "s"}),
        ['<CR>'] = cmp.mapping.confirm(),
        ['<C-n>'] = cmp.mapping.select_next_item(),
        ['<C-p>'] = cmp.mapping.select_prev_item(),
    }
})

It's taken from the fix commit here 3192a0c

@lutalli
Copy link

lutalli commented May 8, 2023

I'm still experiencing this issue randomly. When I type commands like :e in command mode and then press <Tab> attempting to auto complete the file path, sometimes it only produces ^I.


EDIT: Sorry it was me. As this Reddit comment suggests, as well as README, I have to enable cmdline for :, which I hadn't done before.

I still wonder why the bug was kinda randomly occuring though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests