hrsh7th / nvim-cmp

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

Tab cant fallback #1436

Closed huwqchn closed 1 year ago

huwqchn commented 1 year ago

FAQ

Announcement

Minimal reproducible full config

  local cmp_types = require("cmp.types.cmp")
  local ConfirmBehavior = cmp_types.ConfirmBehavior
  local SelectBehavior = cmp_types.SelectBehavior

  local cmp = require("cmp")
  local luasnip = require("luasnip")
  local cmp_window = require("cmp.config.window")
  local cmp_mapping = require("cmp.config.mapping")
  cmp.setup({
    mapping = cmp_mapping.preset.insert({
      ["<Tab>"] = cmp_mapping(function(fallback)
        if cmp.visible() then
          local entry = cmp.get_selected_entry()
          if not entry then
            cmp.select_next_item({ behavior = SelectBehavior.Select })
          end
          cmp.confirm()
        elseif luasnip.expand_or_locally_jumpable() then
          luasnip.expand_or_jump()
        elseif jumpable(1) then
          luasnip.jump(1)
        elseif has_words_before() then
          -- cmp.complete()
          fallback()
        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", "c" }),
  })

Description

Tab can't fallback after update to latest commit

Steps to reproduce

set mapping for tab will make tab never fallback

Expected behavior

Tab fallback when cmp not visible

Actual behavior

tab never fallback

Additional context

No response

hrsh7th commented 1 year ago

How to reproduce this?

comiluv commented 1 year ago

I'm having a similar issue with a setup identical to https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip, pressing Tab key on an empty line does nothing: whereas pressing Shift-Tab acts as if pressing a Tab key (goes forward by a tab)

huwqchn commented 1 year ago

I'm having a similar issue with a setup identical to https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip, pressing Tab key on an empty line does nothing: whereas pressing Shift-Tab acts as if pressing a Tab key (goes forward by a tab)

Some bug

huwqchn commented 1 year ago

How to reproduce this?

just setup cmp config exampple https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip will reproduct this, tab cant fallback, but S-tab act as tab

comiluv commented 1 year ago

I've hidden my comment because it was due to another plugin Exafunction/codeium.vim

hrsh7th commented 1 year ago

Please provide a complete reproducible configuration example in the issue. With your example configuration, I got an error that the function jumpable doesn't exist. It takes a lot of time and effort.

I believe the steps to reproduce are still not provided. "It doesn't work" doesn't mean anything.

Anyway, I tried the below setting, but <Tab> was inserted on an empty line.

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 !filereadable(s:plug_dir .. '/plug.vim')
  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/nvim-cmp'
Plug 'L3MON4D3/LuaSnip'
Plug 'neovim/nvim-lspconfig'
Plug 'saadparwaiz1/cmp_luasnip'
call plug#end()
PlugInstall | quit

" Setup global configuration. More on configuration below.
lua << EOF

local has_words_before = function()
  unpack = unpack or table.unpack
  local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end

local cmp = require "cmp"
local luasnip = require('luasnip')
cmp.setup {
  snippet = {
    expand = function(args)
      vim.fn["vsnip#anonymous"](args.body)
    end,
  },

  mapping = cmp.mapping.preset.insert({
    ["<Tab>"] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      elseif luasnip.expand_or_jumpable() then
        luasnip.expand_or_jump()
      elseif has_words_before() then
        cmp.complete()
      else
        fallback()
      end
    end, { "i", "s" }),
    ["<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" }),
  }),

  sources = cmp.config.sources({
    { name = "nvim_lsp" },
    { name = "buffer" },
  }),
}
EOF
huwqchn commented 1 year ago

Sorry, It's not a bug, In Insert mode, ctrl + i is the same as tab, I was mapping Ctrl + i to some function in insert mode...