quangnguyen30192 / cmp-nvim-ultisnips

nvim-cmp source for ultisnips
Apache License 2.0
144 stars 19 forks source link

No snippets in completion list? #65

Closed Xunius closed 2 years ago

Xunius commented 2 years ago

Hi all,

I'm migrating from my old vim+Ultisnips+supertab to the newer neovim+Ultisnips+nvim-cmp config. I've spent hours and still can't get snippets to work. Please help!

Expected behavior:

Open a .py file, type some keyword, e.g. class, then a completion list would appear listing a class snippet entry.

Actual behavior:

The completion list shows only these:

class   Keyword
class   Keyword
class   Keyword
classmethod   Class
classmethod   Class
classmethod   Class

(Why the same thing repeats 3 times, btw?)

My specs:

nvim --version:

NVIM v0.6.1
Build type: Release
LuaJIT 2.0.5
Compiled by builduser

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

How to reproduce

Below is my init.vim:

"--------------------- vim-plug --------------------- {{{
call plug#begin(stdpath('config') . '/plugged')

" LSP {
Plug 'neovim/nvim-lspconfig'
Plug 'williamboman/nvim-lsp-installer'
" LSP }

" treesitter
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}

" nvim-cmp {
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'
" nvim-cmp }

" snippet engine
Plug 'SirVer/ultisnips'
" default snippets
"Plug 'honza/vim-snippets'
Plug 'quangnguyen30192/cmp-nvim-ultisnips'

call plug#end()
call plug#helptags()

"--------------------- vim-plug --------------------- }}}

"--------------------- LSP --------------------- {{{
lua <<EOF
local lsp_installer = require("nvim-lsp-installer")
lsp_installer.on_server_ready(function(server)
    local opts = {}
    server:setup(opts)
end)

local nvim_lsp = require('lspconfig')

local on_attach = function(client, bufnr)
  require('completion').on_attach()
  local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
  local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
  buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')

  -- Mappings
  local opts = { noremap=true, silent=true }
  buf_set_keymap('n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts)
  buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
  buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
end

  local servers = {'pyright'}
  for _, lsp in ipairs(servers) do
    nvim_lsp[lsp].setup {
      on_attach = on_attach,
    }
  end
EOF
"--------------------- LSP --------------------- }}}

"--------------------- nvim-cmp ---------------------- {{{
lua <<EOF
  local cmp = require'cmp'
  local ultisnips_mappings = require("cmp_nvim_ultisnips.mappings")

  cmp.setup({
    snippet = {
      expand = function(args)
        vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
      end,
    },
    mapping = {
      ["<Tab>"] = cmp.mapping(function(fallback)
        ultisnips_mappings.expand_or_jump_forwards(fallback)
        end),

      ["<S-Tab>"] = function(fallback)
          if cmp.visible() then
              cmp.select_prev_item()
          else
              fallback()
          end
        end,
    },

    sources = cmp.config.sources({
      { name = 'ultisnips' }, -- For ultisnips users.
      { name = 'nvim_lsp' },
      { name = 'buffer' },
    })
  })

  -- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
  cmp.setup.cmdline('/', {
    sources = {
      { name = 'buffer' }
    }
  })

  -- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
  cmp.setup.cmdline(':', {
    sources = cmp.config.sources({
      { name = 'path' }
    }, {
      { name = 'cmdline' }
    })
  })

  -- Setup lspconfig.
  local capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
  -- Replace <YOUR_LSP_SERVER> with each lsp server you've enabled.
  require('lspconfig')['pyright'].setup {
    capabilities = capabilities
  }
EOF

"--------------------- nvim-cmp ---------------------- }}}

"------------------- UltiSnips ------------------- {{{
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-j>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"
let g:UltiSnipsListSnippets="<leader>l"
let g:ultisnips_python_style="google"
"------------------- UltiSnips ------------------- }}}

"----------------------Others---------------------- {{{
set number
set autoindent
set hls
set autochdir
set ic!
set termguicolors
set splitbelow splitright
set wildmode=longest,list,full
syntax on
filetype plugin indent on
filetype plugin on
set tabstop=4 softtabstop=4 expandtab shiftwidth=4
set mouse=a
set completeopt=menu,noinsert,menuone,noselect
set wildmenu
set t_Co=256
"----------------------Others---------------------- }}}
smjonas commented 2 years ago

Hey, I assume you have an UltiSnips snippet for class? Does that snippet work when you type class and hit <Tab> manually?

If you see the same entry multiple times, it means that cmp.config.sources has been called more than once or the same source was added twice. This can happen if you source the same Lua file repeatedly.

Can you check if LSP snippets work by typing function in a .lua file and see if sumneko-lua suggests you the function snippet? Make sure to install that language server.

Xunius commented 2 years ago

@smjonas Many thanks for taking the effort to reply.

I actually wanted to close this issue earlier today but I guess my internet was too bad it didn't commit. I'm very new to neovim and configuring using lua so I've been creating a bit mess. It turns out that I had duplicate language servers running in the background, and I think that's the reason for those duplicated entries, and it also caused complete freezes of nvim.

I somehow managed to get it work, but I don't really know now (sigh), that's why I'd like to close this (or even better, delete it).

smjonas commented 2 years ago

No worries at all, thanks for your update on the issue!