hrsh7th / nvim-cmp

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

utils/keymap.lua attempt to index a nil value #254

Closed frankimhof closed 3 years ago

frankimhof commented 3 years ago

Info about the system I'm using:

iTerm 3.3.9 Neovim v0.5 vim-plug nvim-lspconfig nvim-cmp (configured as recommended) tsserver

What I expected:

What happened:

hrsh7th commented 3 years ago

Please your config.

frankimhof commented 3 years ago

Hey Thx for the quick reply. With config you mean my init.vim? (cmp config and lsp config are default) Here is my init.vim


if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

" PLUGINS
call plug#begin('~/.vim/plugged')
Plug 'junegunn/fzf', { 'do': {-> fzf#install() } } "FUZZY FINDER
Plug 'junegunn/fzf.vim' "FUZZY FINDER
Plug 'tpope/vim-surround' " SURROUND
"syntax
Plug 'https://github.com/mxw/vim-jsx.git' " JSX
Plug 'https://github.com/pangloss/vim-javascript.git' " JAVASCRIPT
Plug 'ianks/vim-tsx' " TSX
Plug 'leafgarland/typescript-vim' " TS
Plug 'lervag/vimtex' " LATEX
Plug 'tomlion/vim-solidity' "SOLIDITY
"git
Plug 'https://github.com/tpope/vim-fugitive.git' " GIT WRAPPER
"colors
Plug 'vim-airline/vim-airline'
Plug 'lifepillar/vim-solarized8'
Plug 'lifepillar/vim-colortemplate'
Plug 'joshdick/onedark.vim'
"lsp and completion
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip'

call plug#end()

" BASIC STUFF
let mapleader = ","
filetype indent on "loads filetype-specific indentation file from /indent
set number relativenumber
syntax on
set scrolloff=3
" indentation
set softtabstop=2
set shiftwidth=2
set expandtab
set cursorline "highlight current line
set wildmenu "shows files and folders when :e (some Path here) <TAB>
set showmatch "highlight matchin [{()}]
set matchtime=5 "for how long the highlight appears
set splitbelow splitright "splits open at the bottom&right
autocmd BufWritePost ?* mkview "save folds automatically
autocmd BufWinEnter ?* silent! loadview "load folds automatically
" FIND OPTIONS (with when searching with /)
set incsearch
set hlsearch " highlights matches

" NETRW
let g:netrw_preview=1
let g:netrw_alto=0
let g:netrw_liststyle=2
let g:netrw_list_hide='.DS_Store'
let g:netrw_sort_direction="reverse"

" COLOR SCHEME
"let g:onedark_terminal_italics=1
set termguicolors
set background=dark
"autocmd vimenter * ++nested
colorscheme mycustom "this is a customized solarized8 colorscheme
let g:solarized_old_cursor_style=1 "make the cursor grey (not blue)

highlight MatchParen guifg=red guibg=bg
highlight EndOfBuffer guifg=bg
highlight FoldColumn guibg=NONE
highlight CursorLineNr guifg=#E00000
set guicursor=n-v-c-sm:block-cursor-Cursor,i-ci-ve:ver50-Cursor,r-cr-o:hor20

" INSERT MODE MAPS
inoremap  jj <Esc>
inoremap ,o <Esc>o<Esc>
inoremap ,O <Esc>O<Esc>
inoremap <C-h> <left>
inoremap <C-l> <right>

" NORMAL MODE MAPS
" Text Navigation, change "LOW" and "HIGH" to "TOP" and "BOTTOM"
nnoremap <C-f> :FZF<CR>
let g:fzf_action = {
  \ 'ctrl-t':'tab split',
  \ 'ctrl-i':'split',
  \ 'ctrl-v':'vsplit'}
nnoremap K {
nnoremap J }
" Buffer navigation
nnoremap <c-l> :bn<return>
nnoremap <c-h> :bp<return>

" Inserting tabs
nnoremap <tab> >>
nnoremap <s-tab> <<
"after searching for words, press space to remove hilighting of words
nnoremap <Space><Space> :nohlsearch <return>
" vim-fugitive git shortcuts. gdh get from left (target), gdh get from right (feature)
nnoremap <leader>gdh :diffget //2<cr> :diffupdate<cr>
nnoremap <leader>gdl :diffget //3<cr> :diffupdate<cr>

" VISUAL MODE MAPS
vnoremap <Space><Space> <Esc>
" Inserting tabs, keeping selection
vnoremap <Tab> >><ESC>gv
vnoremap <S-Tab> <<<Esc>gv
" Text Navigation
vnoremap K {
vnoremap J }

" AIRLINE
" install powerline fonts from github and change font to Hack (in iTerm2 prefs>profile)
let g:airline_powerline_fonts = 1
let g:airline_section_z = 0
let g:airline_theme='onedark'
"let g:airline_solarized_bg='dark'
"let g:airline#extensions#tabline#enabled = 1
"let g:airline#extensions#tabline#buffer_nr_show = 1

" LATEX
let g:tex_flavor  = 'latex'
let g:tex_conceal = ''
let g:vimtex_fold_manual = 1
let g:vimtex_latexmk_continuous = 1
let g:vimtex_compiler_projname = 'nvr'
" newly added for minted package
let g:vimtex_compiler_latexmk = {
  \'options' : [
    \'-shell-escape',
  \],
  \}

'' LSP
set completeopt=menu,menuone,noselect

lua <<EOF
  -- Setup nvim-cmp.
  local cmp = require'cmp'

  cmp.setup({
    snippet = {
      expand = function(args)
        vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` user.
      end,
    },
    mapping = {
      ['<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({ select = true }),
    },
    sources = {
      { name = 'nvim_lsp' },
      { name = 'vsnip' },
      { name = 'buffer' },
    }
  })

  -- Setup lspconfig.
  require('lspconfig').tsserver.setup {
    capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
  }
EOF```
hrsh7th commented 3 years ago

I can't reproduce with your config. I'm using this for testing.

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

" PLUGINS
call plug#begin(s:plug_dir)
Plug 'junegunn/fzf', { 'do': {-> fzf#install() } } "FUZZY FINDER
Plug 'junegunn/fzf.vim' "FUZZY FINDER
Plug 'tpope/vim-surround' " SURROUND
"syntax
Plug 'https://github.com/mxw/vim-jsx.git' " JSX
Plug 'https://github.com/pangloss/vim-javascript.git' " JAVASCRIPT
Plug 'ianks/vim-tsx' " TSX
Plug 'leafgarland/typescript-vim' " TS
Plug 'lervag/vimtex' " LATEX
Plug 'tomlion/vim-solidity' "SOLIDITY
"git
Plug 'https://github.com/tpope/vim-fugitive.git' " GIT WRAPPER
"colors
Plug 'vim-airline/vim-airline'
Plug 'lifepillar/vim-solarized8'
Plug 'lifepillar/vim-colortemplate'
Plug 'joshdick/onedark.vim'
"lsp and completion
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip'
call plug#end()

" BASIC STUFF
let mapleader = ","
filetype indent on "loads filetype-specific indentation file from /indent
set number relativenumber
syntax on
set scrolloff=3
" indentation
set softtabstop=2
set shiftwidth=2
set expandtab
set cursorline "highlight current line
set wildmenu "shows files and folders when :e (some Path here) <TAB>
set showmatch "highlight matchin [{()}]
set matchtime=5 "for how long the highlight appears
set splitbelow splitright "splits open at the bottom&right
autocmd BufWritePost ?* mkview "save folds automatically
autocmd BufWinEnter ?* silent! loadview "load folds automatically
" FIND OPTIONS (with when searching with /)
set incsearch
set hlsearch " highlights matches

" NETRW
let g:netrw_preview=1
let g:netrw_alto=0
let g:netrw_liststyle=2
let g:netrw_list_hide='.DS_Store'
let g:netrw_sort_direction="reverse"

" COLOR SCHEME
"let g:onedark_terminal_italics=1
set termguicolors
set background=dark
"autocmd vimenter * ++nested
colorscheme mycustom "this is a customized solarized8 colorscheme
let g:solarized_old_cursor_style=1 "make the cursor grey (not blue)

highlight MatchParen guifg=red guibg=bg
highlight EndOfBuffer guifg=bg
highlight FoldColumn guibg=NONE
highlight CursorLineNr guifg=#E00000
set guicursor=n-v-c-sm:block-cursor-Cursor,i-ci-ve:ver50-Cursor,r-cr-o:hor20

" INSERT MODE MAPS
inoremap  jj <Esc>
inoremap ,o <Esc>o<Esc>
inoremap ,O <Esc>O<Esc>
inoremap <C-h> <left>
inoremap <C-l> <right>

" NORMAL MODE MAPS
" Text Navigation, change "LOW" and "HIGH" to "TOP" and "BOTTOM"
nnoremap <C-f> :FZF<CR>
let g:fzf_action = {
  \ 'ctrl-t':'tab split',
  \ 'ctrl-i':'split',
  \ 'ctrl-v':'vsplit'}
nnoremap K {
nnoremap J }
" Buffer navigation
nnoremap <c-l> :bn<return>
nnoremap <c-h> :bp<return>

" Inserting tabs
nnoremap <tab> >>
nnoremap <s-tab> <<
"after searching for words, press space to remove hilighting of words
nnoremap <Space><Space> :nohlsearch <return>
" vim-fugitive git shortcuts. gdh get from left (target), gdh get from right (feature)
nnoremap <leader>gdh :diffget //2<cr> :diffupdate<cr>
nnoremap <leader>gdl :diffget //3<cr> :diffupdate<cr>

" VISUAL MODE MAPS
vnoremap <Space><Space> <Esc>
" Inserting tabs, keeping selection
vnoremap <Tab> >><ESC>gv
vnoremap <S-Tab> <<<Esc>gv
" Text Navigation
vnoremap K {
vnoremap J }

" AIRLINE
" install powerline fonts from github and change font to Hack (in iTerm2 prefs>profile)
let g:airline_powerline_fonts = 1
let g:airline_section_z = 0
let g:airline_theme='onedark'
"let g:airline_solarized_bg='dark'
"let g:airline#extensions#tabline#enabled = 1
"let g:airline#extensions#tabline#buffer_nr_show = 1

" LATEX
let g:tex_flavor  = 'latex'
let g:tex_conceal = ''
let g:vimtex_fold_manual = 1
let g:vimtex_latexmk_continuous = 1
let g:vimtex_compiler_projname = 'nvr'
" newly added for minted package
let g:vimtex_compiler_latexmk = {
  \'options' : [
    \'-shell-escape',
  \],
  \}

set completeopt=menu,menuone,noselect

lua <<EOF
  -- Setup nvim-cmp.
  local cmp = require'cmp'

  cmp.setup({
    snippet = {
      expand = function(args)
        vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` user.
      end,
    },
    mapping = {
      ['<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({ select = true }),
    },
    sources = {
      { name = 'nvim_lsp' },
      { name = 'vsnip' },
      { name = 'buffer' },
    }
  })

  -- Setup lspconfig.
  require('lspconfig').tsserver.setup {
    capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities())
  }
EOF
frankimhof commented 3 years ago

Thanks @hrsh7th I recognized that the error only shows up when I'm editing one specific App.tsx-file. When I rename the file to App2.tsx, the error is gone. But as soon as I change the name back to App.tsx, the error shows up again. Very strange. Do you have any idea what I could do to further investigate? best regards

hrsh7th commented 3 years ago

Please reproduce steps. I didn't understand what is App.tsx-file. I think you shouldn't use such filename...

frankimhof commented 3 years ago

I'm sorry It is probably not a nvim-cmp related problem, so you can close the issue. Thx anyways. I still explain my problem here in case anyone else has similar problems. I am not able to reproduce it though.

Problem

(Not all files are affected, only a few.)

hrsh7th commented 3 years ago

Thank you. Hm... It's strange... and I can't figure out the cause. If you understand the cause. Feel free to re-open this issue. Thanks.

frankimhof commented 3 years ago

I found a way of reproducing it.

Reproducing steps

Possible source of error

I think it has something to do with the following settings regarding folds

autocmd BufWritePost ?* mkview "save folds automatically
autocmd BufWinEnter ?* silent! loadview "load folds automatically

After saving the file with the folds, nvim creates a file in /.local/share/nvim/view that holds information about the folds. When deleting the corresponding file in /.local/share/nvim/view, the error disappears (but also all the info about folds).

L3afMe commented 3 years ago

I've also been getting this error.

Seems to be reproducible by opening a file with something folded and pressing enter.

sriramkandukuri commented 3 years ago

Hi,

Plugin installed today from master branch

No foldings....

config as below

local has_words_before = function()                                                                                                                                                                                                                                                                     
    if vim.api.nvim_buf_get_option(0, "buftype") == "prompt" then                                                                                                                                                                                                                                       
        return false                                                                                                                                                                                                                                                                                    
    end                                                                                                                                                                                                                                                                                                 
    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 feedkey = function(key)                                                                                                                                                                                                                                                                           
    vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, true, true), "n", true)                                                                                                                                                                                                             
end      

local luasnip = require("luasnip")                                                                                                                                                                                                                                                                      

local cmp = require'cmp'

cmp.setup {                                                                                                                                                                                                                                                                                             
    snippet = {                                                                                                                                                                                                                                                                                         
        expand = function(args)                                                                                                                                                                                                                                                                         
            -- For `vsnip` user.                                                                                                                                                                                                                                                                        
            -- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` user.                                                                                                                                                                                                                                

            -- For `luasnip` user.                                                                                                                                                                                                                                                                      
            require('luasnip').lsp_expand(args.body)                                                                                                                                                                                                                                                    

            -- For `ultisnips` user.                                                                                                                                                                                                                                                                    
            -- vim.fn["UltiSnips#Anon"](args.body)                                                                                                                                                                                                                                                      
        end                                                                                                                                                                                                                                                                                             
    },                                                                                                                                                                                                                                                                                                  
    mapping = {                                                                                                                                                                                                                                                                                         
        ['<C-d>'] = cmp.mapping.scroll_docs(-4),                                                                                                                                                                                                                                                        
        ['<C-f>'] = cmp.mapping.scroll_docs(4),                                                                                                                                                                                                                                                         
        ['<C-Space>'] = cmp.mapping.complete(),                                                                                                                                                                                                                                                         
        ['<C-e>'] = cmp.mapping.close(),                                                                                                                                                                                                                                                                
        ["<Tab>"] = cmp.mapping(function(fallback)                                                                                                                                                                                                                                                      
            if vim.fn.pumvisible() == 1 then                                                                                                                                                                                                                                                            
                feedkey("<down>")                                                                                                                                                                                                                                                                       
            elseif luasnip.expand_or_jumpable() then                                                                                                                                                                                                                                                    
                luasnip.expand_or_jump()                                                                                                                                                                                                                                                                
            elseif has_words_before() then                                                                                                                                                                                                                                                              
                cmp.complete()                                                                                                                                                                                                                                                                          
            else                                                                                                                                                                                                                                                                                        
                fallback() -- The fallback function sends a already mapped key. In this case, it's probably `<Tab>`.                                                                                                                                                                                    
            end                                                                                                                                                                                                                                                                                         
        end, { "i", "s" }),                                                                                                                                                                                                                                                                             

        ["<S-Tab>"] = cmp.mapping(function(fallback)                                                                                                                                                                                                                                                    
            if vim.fn.pumvisible() == 1 then                                                                                                                                                                                                                                                            
                feedkey("<up>")                                                                                                                                                                                                                                                                         
            elseif luasnip.jumpable(-1) then                                                                                                                                                                                                                                                            
                luasnip.jump(-1)                                                                                                                                                                                                                                                                        
            else                                                                                                                                                                                                                                                                                        
                fallback()                                                                                                                                                                                                                                                                              
            end                                                                                                                                                                                                                                                                                         
        end, { "i", "s" }),                                                                                                                                                                                                                                                                             
        ['<CR>'] = cmp.mapping(function(fallback)                                                                                                                                                                                                                                                       
            if luasnip.jumpable() then                                                                                                                                                                                                                                                                  
                luasnip.expand_or_jump()                                                                                                                                                                                                                                                                
            else                                                                                                                                                                                                                                                                                        
                fallback()                                                                                                                                                                                                                                                                              
            end                                                                                                                                                                                                                                                                                         
        end, { "i", "s" }),                                                                                                                                                                                                                                                                             
    },                                                                                                                                                                                                                                                                                                  
    sources = {                                                                                                                                                                                                                                                                                         
        { name = 'nvim_lsp' },                                                                                                                                                                                                                                                                          
        -- For vsnip user.                                                                                                                                                                                                                                                                              
        -- { name = 'vsnip' },                                                                                                                                                                                                                                                                          
        -- For luasnip user.                                                                                                                                                                                                                                                                            
        { name = 'luasnip' },                                                                                                                                                                                                                                                                           
        -- For ultisnips user.                                                                                                                                                                                                                                                                          
        -- { name = 'ultisnips' },                                                                                                                                                                                                                                                                      
        { name = 'buffer' },                                                                                                                                                                                                                                                                            
        { name = "path" },                                                                                                                                                                                                                                                                              
        { name = "crates" },                                                                                                                                                                                                                                                                            
        { name = "nvim_lua" },                                                                                                                                                                                                                                                                          
    }                                                                                                                                                                                                                                                                                                   
}  

Getting same error from keymap.lua:150 line number

Config taken from https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings

image

Please help

sriramkandukuri commented 3 years ago

Not observing issue when file is opened first time. (no error seen by pressing tab) When ever I close and re-open same file again then above error is coming

L3afMe commented 3 years ago

Very temporary fix because it's rather annoying to do: closing and reopening Neovim seems to temporarily fix it.

sriramkandukuri commented 3 years ago

Very temporary fix because it's rather annoying to do: closing and reopening Neovim seems to temporarily fix it.

no use :(

VladimirBeliakov commented 3 years ago

I think it has something to do with the following settings regarding folds autocmd BufWritePost ? mkview "save folds automatically autocmd BufWinEnter ? silent! loadview "load folds automatically

I also have this feature enabled and had the same problem. By default, viewoptions=folds,options,cursor,curdir. Changing it to viewoptions=folds,cursor,curdir solved the problem for me.

diegotoral commented 3 years ago

I'm facing similar problem but I'm not sure when it happens. So far the I workaround this problem by deleting the current session (created with rmagatti/auto-session) and restarting nvim.

As far as I can tell my config does not include anything regarding folds. :thinking:

TimB87 commented 3 years ago

I still got that error when my vim config contains the following two lines

"autocmd BufWrite * mkview                                                                                                                                                                                                             
"autocmd BufRead * silent! loadview      
TimB87 commented 3 years ago

Even without the autocmds, running :loadview will ruin that nvim instance. @hrsh7th do you want me to create another issue for that? I could prepare a minimal example init.lua.

Chaitanyabsprip commented 3 years ago

I am facing the same issue, and I think this has something to do with sessions. I am using rmagatti/auto-session and whenever I delete the session file and reopen neovim, the keymapping works fine, but once the session is created and loaded after starting neovim, I believe, the nvim-cmp keymaps are not loaded

TimB87 commented 3 years ago

I don't use auto-session, just views, depending how session works, this might be still be the same. I haven't had time to investigate anything around it so far so I deleted views for now.

hrsh7th commented 3 years ago

Perhaps this can be fixed. I'll push the fix so can you test it?

hrsh7th commented 3 years ago

@TimB87 Could you test with latest nvim-cmp? (https://github.com/hrsh7th/nvim-cmp/commit/68a37a669b9e1738f13ec8849d136f32be9b32d9

Thank you for your information.

TimB87 commented 3 years ago

Works for me!

hrsh7th commented 3 years ago

🥰

TimB87 commented 3 years ago

@hrsh7th it's back since.. yesterday I think. Turning off views fixes it again, let me know if I can help test things :smiley: