Skip to content

Commit

Permalink
feat(autocomplete): add nvim-cmp support
Browse files Browse the repository at this point in the history
  • Loading branch information
wsdjeg committed Jan 1, 2022
1 parent 8e41831 commit 6dbd970
Show file tree
Hide file tree
Showing 104 changed files with 9,061 additions and 18 deletions.
7 changes: 3 additions & 4 deletions autoload/SpaceVim.vim
Expand Up @@ -315,7 +315,9 @@ let g:spacevim_realtime_leader_guide = 1
" let g:spacevim_enable_key_frequency = 1
" <
let g:spacevim_enable_key_frequency = 0
if (has('python3')
if has('nvim-0.5.0')
let g:spacevim_autocomplete_method = 'nvim-cmp'
elseif (has('python3')
\ && (SpaceVim#util#haspy3lib('neovim')
\ || SpaceVim#util#haspy3lib('pynvim'))) &&
\ (has('nvim') || (has('patch-8.0.0027')))
Expand Down Expand Up @@ -1384,16 +1386,13 @@ function! SpaceVim#end() abort
elseif g:spacevim_vim_help_language ==# 'ja'
let &helplang = 'jp'
endif
""
" generate tags for SpaceVim
let help = fnamemodify(g:_spacevim_root_dir, ':p:h') . '/doc'
try
exe 'helptags ' . help
catch
call SpaceVim#logger#warn('Failed to generate helptags for SpaceVim')
endtry

""
" set language
if !empty(g:spacevim_language)
silent exec 'lan ' . g:spacevim_language
Expand Down
2 changes: 1 addition & 1 deletion autoload/SpaceVim/api/logger.vim
Expand Up @@ -52,7 +52,7 @@ endfunction
function! s:self._build_msg(msg, l) abort
let msg = a:msg
let time = strftime('%H:%M:%S')
let log = printf('[ %s ] [%s] [%00.3f] [ %s ] %s',
let log = printf('[ %s ] [%s] [%00.3f] [ %5s ] %s',
\ self.name,
\ time,
\ reltimefloat(reltime(self.clock)),
Expand Down
1 change: 1 addition & 0 deletions autoload/SpaceVim/autocmds.vim
Expand Up @@ -12,6 +12,7 @@ let s:VIM = SpaceVim#api#import('vim')

"autocmds
function! SpaceVim#autocmds#init() abort
call SpaceVim#logger#debug('init SpaceVim_core autocmd group')
augroup SpaceVim_core
au!
autocmd BufWinEnter quickfix nnoremap <silent> <buffer>
Expand Down
2 changes: 2 additions & 0 deletions autoload/SpaceVim/default.vim
Expand Up @@ -147,6 +147,7 @@ endfunction
"}}}

function! SpaceVim#default#layers() abort
call SpaceVim#logger#debug('init default layer list.')
call SpaceVim#layers#load('autocomplete')
call SpaceVim#layers#load('checkers')
call SpaceVim#layers#load('format')
Expand All @@ -159,6 +160,7 @@ function! SpaceVim#default#layers() abort
endfunction

function! SpaceVim#default#keyBindings() abort
call SpaceVim#logger#debug('init default key bindings.')
" yank and paste
if has('unnamedplus')
xnoremap <Leader>y "+y
Expand Down
23 changes: 21 additions & 2 deletions autoload/SpaceVim/layers/autocomplete.vim
Expand Up @@ -86,6 +86,22 @@ function! SpaceVim#layers#autocomplete#plugins() abort
\ 'on_event' : 'InsertEnter',
\ 'loadconf' : 1,
\ }])
elseif g:spacevim_autocomplete_method ==# 'nvim-cmp'
" use bundle nvim-cmp
call add(plugins, [g:_spacevim_root_dir . 'bundle/nvim-cmp', {
\ 'merged' : 0,
\ 'loadconf' : 1,
\ }])
call add(plugins, [g:_spacevim_root_dir . 'bundle/cmp-buffer', {
\ 'merged' : 0,
\ }])
call add(plugins, [g:_spacevim_root_dir . 'bundle/cmp-path', {
\ 'merged' : 0,
\ }])
call add(plugins, [g:_spacevim_root_dir . 'bundle/lspkind-nvim', {
\ 'merged' : 0,
\ 'loadconf' : 1,
\ }])
elseif g:spacevim_autocomplete_method ==# 'asyncomplete'
call add(plugins, ['prabirshrestha/asyncomplete.vim', {
\ 'loadconf' : 1,
Expand Down Expand Up @@ -120,8 +136,11 @@ function! SpaceVim#layers#autocomplete#plugins() abort
\ 'on_event' : 'CompleteDone',
\ 'loadconf_before' : 1,
\ }])
call add(plugins, [g:_spacevim_root_dir . 'bundle/CompleteParameter.vim',
\ { 'merged' : 0}])
if g:spacevim_autocomplete_method !=# 'nvim-cmp'
" this plugin use same namespace as nvim-cmp
call add(plugins, [g:_spacevim_root_dir . 'bundle/CompleteParameter.vim',
\ { 'merged' : 0}])
endif
endif
return plugins
endfunction
Expand Down
16 changes: 10 additions & 6 deletions autoload/SpaceVim/layers/lsp.vim
Expand Up @@ -51,12 +51,12 @@ endfunction


function! SpaceVim#layers#lsp#setup() abort
lua << EOF
local nvim_lsp = require('lspconfig')
lua << EOF
local nvim_lsp = require('lspconfig')

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
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

Expand Down Expand Up @@ -95,8 +95,8 @@ for _, lsp in ipairs(servers) do
on_attach = on_attach,
flags = {
debounce_text_changes = 150,
}
}
}
end
EOF
endfunction
Expand All @@ -108,6 +108,10 @@ function! SpaceVim#layers#lsp#plugins() abort
call add(plugins, [g:_spacevim_root_dir . 'bundle/nvim-lspconfig', {'merged' : 0, 'loadconf' : 1}])
if g:spacevim_autocomplete_method ==# 'deoplete'
call add(plugins, [g:_spacevim_root_dir . 'bundle/deoplete-lsp', {'merged' : 0}])
elseif g:spacevim_autocomplete_method ==# 'nvim-cmp'
call add(plugins, [g:_spacevim_root_dir . 'bundle/cmp-nvim-lsp', {
\ 'merged' : 0,
\ }])
endif
elseif SpaceVim#layers#isLoaded('autocomplete') && get(g:, 'spacevim_autocomplete_method') ==# 'coc'
" nop
Expand Down
1 change: 1 addition & 0 deletions autoload/SpaceVim/mapping/g.vim
Expand Up @@ -7,6 +7,7 @@
"=============================================================================

function! SpaceVim#mapping#g#init() abort
call SpaceVim#logger#debug('init g key bindings')
nnoremap <silent><nowait> [G] :<c-u>LeaderGuide "g"<CR>
nmap g [G]
let g:_spacevim_mappings_g = {}
Expand Down
1 change: 1 addition & 0 deletions autoload/SpaceVim/mapping/leader.vim
Expand Up @@ -199,6 +199,7 @@ function! SpaceVim#mapping#leader#getName(key) abort
endfunction

function! SpaceVim#mapping#leader#defindKEYs() abort
call SpaceVim#logger#debug('defind SPC h k prefixs')
let g:_spacevim_mappings_prefixs = {}
if !g:spacevim_vimcompatible && !empty(g:spacevim_windows_leader)
let g:_spacevim_mappings_prefixs[g:spacevim_windows_leader] = {'name' : '+Window prefix'}
Expand Down
1 change: 1 addition & 0 deletions autoload/SpaceVim/mapping/space.vim
Expand Up @@ -11,6 +11,7 @@ let s:BUF = SpaceVim#api#import('vim#buffer')
let s:file = expand('<sfile>:~')
let s:funcbeginline = expand('<slnum>') + 1
function! SpaceVim#mapping#space#init() abort
call SpaceVim#logger#debug('init SPC key bindings')
let g:_spacevim_mappings_space = {}
let g:_spacevim_mappings_prefixs['[SPC]'] = {'name' : '+SPC prefix'}
let g:_spacevim_mappings_space.t = {'name' : '+Toggles'}
Expand Down
10 changes: 9 additions & 1 deletion autoload/SpaceVim/mapping/tab.vim
Expand Up @@ -20,8 +20,16 @@ if g:spacevim_snippet_engine ==# 'neosnippet'
elseif neosnippet#expandable_or_jumpable() && getline('.')[col('.')-2] !=#'('
return "\<plug>(neosnippet_expand_or_jump)"
elseif pumvisible()
\ ||
\ (
\ g:spacevim_autocomplete_method ==# 'nvim-cmp'
\ && luaeval("require('cmp').visible()")
\ )
return "\<C-n>"
elseif has('patch-7.4.774') && complete_parameter#jumpable(1) && getline('.')[col('.')-2] !=# ')'
elseif has('patch-7.4.774')
\ && g:spacevim_autocomplete_method !=# 'nvim-cmp'
\ && complete_parameter#jumpable(1)
\ && getline('.')[col('.')-2] !=# ')'
return "\<plug>(complete_parameter#goto_next_parameter)"
else
return "\<tab>"
Expand Down
1 change: 1 addition & 0 deletions autoload/SpaceVim/mapping/z.vim
Expand Up @@ -7,6 +7,7 @@
"=============================================================================

function! SpaceVim#mapping#z#init() abort "{{{
call SpaceVim#logger#debug('init z key bindings')
nnoremap <silent><nowait> [Z] :<c-u>LeaderGuide "z"<CR>
nmap z [Z]
let g:_spacevim_mappings_z = {}
Expand Down
10 changes: 6 additions & 4 deletions autoload/SpaceVim/plugins.vim
Expand Up @@ -18,9 +18,10 @@ function! SpaceVim#plugins#load() abort

endfunction
function! s:load_plugins() abort
for group in SpaceVim#layers#get()
let g:_spacevim_plugin_layer = group
for plugin in s:getLayerPlugins(group)
for layer in SpaceVim#layers#get()
call SpaceVim#logger#debug('init ' . layer . ' layer plugins list.')
let g:_spacevim_plugin_layer = layer
for plugin in s:getLayerPlugins(layer)
if len(plugin) == 2
call SpaceVim#plugins#add(plugin[0], extend(plugin[1], {'overwrite' : 1}))
if SpaceVim#plugins#tap(split(plugin[0], '/')[-1]) && get(plugin[1], 'loadconf', 0 )
Expand All @@ -33,7 +34,7 @@ function! s:load_plugins() abort
call SpaceVim#plugins#add(plugin[0], {'overwrite' : 1})
endif
endfor
call s:loadLayerConfig(group)
call s:loadLayerConfig(layer)
endfor
unlet g:_spacevim_plugin_layer
for plugin in g:spacevim_custom_plugins
Expand All @@ -55,6 +56,7 @@ function! s:getLayerPlugins(layer) abort
endfunction

function! s:loadLayerConfig(layer) abort
call SpaceVim#logger#debug('load ' . a:layer . ' layer config.')
try
call SpaceVim#layers#{a:layer}#config()
catch /^Vim\%((\a\+)\)\=:E117/
Expand Down
1 change: 1 addition & 0 deletions bundle/README.md
Expand Up @@ -16,3 +16,4 @@ In `bundle/` directory, there are two kinds of plugins: forked plugins without c
- [indent-blankline.nvim](https://github.com/lukas-reineke/indent-blankline.nvim/tree/17a83ea765831cb0cc64f768b8c3f43479b90bbe)
- [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig/tree/507f8a570ac2b8b8dabdd0f62da3b3194bf822f8)
- [deoplete-lsp](https://github.com/deoplete-plugins/deoplete-lsp/tree/6299a22bedfb4f814d95cb0010291501472f8fd0)
- [nvim-cmp](https://github.com/hrsh7th/nvim-cmp/tree/1cfe2f7dfdd877b54c0f4b0f9a15f525e7a3ea01)
21 changes: 21 additions & 0 deletions bundle/cmp-buffer/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 hrsh7th

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
62 changes: 62 additions & 0 deletions bundle/cmp-buffer/README.md
@@ -0,0 +1,62 @@
# cmp-buffer

nvim-cmp source for buffer words.

# Setup

```lua
require'cmp'.setup {
sources = {
{ name = 'buffer' }
}
}
```

# Configuration

The below source configuration are available.


### keyword_length (type: number)

_Default:_ `3`

Specify word length to gather.


### keyword_pattern (type: string)

_Default:_ `[[\%(-\?\d\+\%(\.\d\+\)\?\|\h\w*\%([\-.]\w*\)*\)]]`

A vim's regular expression for creating a word list from buffer content.

You can set this to `\k\+` if you want to use the `iskeyword` option for recognizing words.


### get_bufnrs (type: fun(): number[])

_Default:_ `function() return { vim.api.nvim_get_current_buf() } end`

A function that specifies the buffer numbers to complete.

You can use the following pre-defined recipes.

##### All buffers

```lua
get_bufnrs = function()
return vim.api.nvim_list_bufs()
end
```

##### Visible buffers

```lua
get_bufnrs = function()
local bufs = {}
for _, win in ipairs(vim.api.nvim_list_wins()) do
bufs[vim.api.nvim_win_get_buf(win)] = true
end
return vim.tbl_keys(bufs)
end
```
2 changes: 2 additions & 0 deletions bundle/cmp-buffer/after/plugin/cmp_buffer.lua
@@ -0,0 +1,2 @@
require'cmp'.register_source('buffer', require'cmp_buffer'.new())

0 comments on commit 6dbd970

Please sign in to comment.