Skip to content

Commit

Permalink
Improve trigger completion (nvim-lua#195)
Browse files Browse the repository at this point in the history
* Added smart tab functions and mappings

* Remove unneeded vim functions

* Adjust documentation

* As requested re-adding vim functions

For backwards compatibility purposes

* Mention new functions in readme

* Mention new function in help as well

* Added also documentation and mapping for trigger

* Bonus: use new vim.b feature

With the new api `vim.b` you can get and set buffer variables directly
Also add deprecated comment to vim functions
  • Loading branch information
tricktux authored Sep 24, 2020
1 parent 397cde9 commit 5c153f8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
14 changes: 4 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,15 @@ let g:completion_enable_auto_popup = 0
- You can manually trigger completion with mapping key by

```vim
inoremap <silent><expr> <c-p> completion#trigger_completion() "map <c-p> to manually trigger completion
"map <c-p> to manually trigger completion
imap <silent> <c-p> <Plug>(completion_trigger)
```

- Or you want to use `<Tab>` as trigger keys

```vim
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~ '\s'
endfunction
inoremap <silent><expr> <TAB>
\ pumvisible() ? "\<C-n>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ completion#trigger_completion()
nmap <tab> <Plug>(completion_smart_tab)
nmap <s-tab> <Plug>(completion_smart_s_tab)
```

### Enable Snippets Support
Expand Down
3 changes: 3 additions & 0 deletions autoload/completion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ function! completion#wrap_completion() abort
endif
endfunction

" Depracated
" Wrapper to get manually trigger working
" Please send me a pull request if you know how to do this properly...
function! completion#completion_wrapper()
lua require'completion'.triggerCompletion()
return ''
endfunction

" Depracated
function! completion#trigger_completion()
return "\<c-r>=completion#completion_wrapper()\<CR>"
endfunction

" Depracated
" Wrapper of getting buffer variable
" Avoid accessing to unavailable variable
function! completion#get_buffer_variable(str)
Expand Down
13 changes: 3 additions & 10 deletions doc/completion-nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,13 @@ g:completion_enable_auto_popup *g:completion_enable_auto_popup*
mapping keys. For example:
>
" map <c-p> to manually trigger completion
inoremap <silent><expr> <c-p> completion#trigger_completion()
imap <silent> <c-p> <Plug>(completion_trigger)
<
Or you want to use <tab> to trigger completion without modifying the
usage to <tab> keys.
>
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~ '\s'
endfunction
inoremap <silent><expr> <TAB>
\ pumvisible() ? "\<C-n>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ completion#trigger_completion()
nmap <tab> <Plug>(completion_smart_tab)
nmap <s-tab> <Plug>(completion_smart_s_tab)
<

default value: 1
Expand Down
37 changes: 32 additions & 5 deletions lua/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,43 @@ M.triggerCompletion = function()
end

M.completionToggle = function()
local enable = api.nvim_call_function('completion#get_buffer_variable', {'completion_enable'})
local enable = vim.b.completion_enable
if enable == nil then
M.on_attach()
elseif enable == 0 then
api.nvim_buf_set_var(0, 'completion_enable', 1)
vim.b.completion_enable = 1
else
api.nvim_buf_set_var(0, 'completion_enable', 0)
vim.b.completion_enable = 0
end
end

------------------------------------------------------------------------
-- smart tab --
------------------------------------------------------------------------

function M.smart_tab()
if vim.fn.pumvisible() ~= 0 then
api.nvim_eval([[feedkeys("\<c-n>", "n")]])
return
end

local col = vim.fn.col('.') - 1
if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
api.nvim_eval([[feedkeys("\<tab>", "n")]])
return
end

source.triggerCompletion(true, manager)
end

function M.smart_s_tab()
if vim.fn.pumvisible() ~= 0 then
api.nvim_eval([[feedkeys("\<c-p>", "n")]])
return
end

api.nvim_eval([[feedkeys("\<s-tab>", "n")]])
end

------------------------------------------------------------------------
-- confirm completion --
Expand Down Expand Up @@ -132,7 +159,7 @@ end

-- TODO: need further refactor, very messy now:(
function M.on_InsertEnter()
local enable = api.nvim_call_function('completion#get_buffer_variable', {'completion_enable'})
local enable = vim.b.completion_enable
if enable == nil or enable == 0 then
return
end
Expand Down Expand Up @@ -235,7 +262,7 @@ M.on_attach = function(option)
api.nvim_command("autocmd!")
api.nvim_command("augroup end")
end
api.nvim_buf_set_var(0, 'completion_enable', 1)
vim.b.completion_enable = 1
end

return M
Expand Down
9 changes: 9 additions & 0 deletions plugin/completion.vim
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ inoremap <silent> <Plug>(completion_next_source)
inoremap <silent> <Plug>(completion_prev_source)
\ <cmd>lua require'completion'.prevSource()<CR>
inoremap <silent> <Plug>(completion_smart_tab)
\ <cmd>lua require'completion'.smart_tab()<CR>
inoremap <silent> <Plug>(completion_smart_s_tab)
\ <cmd>lua require'completion'.smart_s_tab()<CR>
inoremap <silent> <Plug>(completion_trigger)
\ <cmd>lua require'completion'.triggerCompletion()<CR>
command! -nargs=0 CompletionToggle lua require'completion'.completionToggle()

let &cpo = s:save_cpo
Expand Down

0 comments on commit 5c153f8

Please sign in to comment.