L3MON4D3 / LuaSnip

Snippet Engine for Neovim written in Lua.
Apache License 2.0
3.32k stars 237 forks source link

Strange behavior with nvim-cmp, gopls and virtualedit=all #302

Closed mbenford closed 2 years ago

mbenford commented 2 years ago

I have recently migrated from vim-snip to LuaSnip (paired with nvim-cmp) and I noticed a strange behavior when completing suggestions from gopls with virtualedit=all. I guess ~an image~ a gif is worth a thousand words:

luasnip-1

That doesn't happen with other language servers, as far as I can tell (I've tested it with tsserver and sumneko).

The behavior shown in the gif above can be reproduced with the following configuration:

vim.opt.signcolumn = 'no'
vim.opt.number = true
vim.opt.completeopt = { 'menu', 'menuone', 'noinsert' }
vim.opt.virtualedit = 'all'

vim.cmd('packadd packer.nvim')
require('packer').startup(function(use)
  use 'wbthomason/packer.nvim'
  use {'williamboman/nvim-lsp-installer',
    config = function()
      local installer = require('nvim-lsp-installer')
      installer.on_server_ready(function(server)
        server:setup({
          capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities()),
        })
      end)
    end
  }
  use {'hrsh7th/nvim-cmp',
    config = function()
      local cmp = require('cmp')
      require('cmp').setup({
        sources = cmp.config.sources({
          { name = 'nvim_lsp' },
          { name = 'luasnip '},
        }),
        mapping = {
         ['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
         ['<CR>'] = cmp.mapping.confirm({ select = true }),
        },
        snippet = {
          expand = function(args)
            require('luasnip').lsp_expand(args.body)
          end,
        },
      })
    end
  }
  use {'hrsh7th/cmp-nvim-lsp'}
  use {'L3MON4D3/LuaSnip'}
  use {'saadparwaiz1/cmp_luasnip'}
end)

I'm not sure whether this is a bug in LuaSnip or nvim-cmp, but since it works correctly when using vim-snip, I think it might be something with LuaSnip.

mbenford commented 2 years ago

OK, I did more testing and realized it has nothing to do with gopls: it's the file indentation that causes the problem. It stops happening when I disable expandtab so spaces are used instead of tabs.

I also noticed that by changing tabstop (with expandtab off) to different values the cursor is placed at different positions after a suggestion is selected.

L3MON4D3 commented 2 years ago

I think I see what's going wrong, virtualedit makes it so every individual column of a tab has to be moved over, the one <right> we do for the entire tab isn't enough. I'll definitely look into that one, thank you for opening this👍