Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit 5c153f8

Browse files
authored
Improve trigger completion (#195)
* 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
1 parent 397cde9 commit 5c153f8

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,15 @@ let g:completion_enable_auto_popup = 0
101101
- You can manually trigger completion with mapping key by
102102

103103
```vim
104-
inoremap <silent><expr> <c-p> completion#trigger_completion() "map <c-p> to manually trigger completion
104+
"map <c-p> to manually trigger completion
105+
imap <silent> <c-p> <Plug>(completion_trigger)
105106
```
106107

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

109110
```vim
110-
function! s:check_back_space() abort
111-
let col = col('.') - 1
112-
return !col || getline('.')[col - 1] =~ '\s'
113-
endfunction
114-
115-
inoremap <silent><expr> <TAB>
116-
\ pumvisible() ? "\<C-n>" :
117-
\ <SID>check_back_space() ? "\<TAB>" :
118-
\ completion#trigger_completion()
111+
nmap <tab> <Plug>(completion_smart_tab)
112+
nmap <s-tab> <Plug>(completion_smart_s_tab)
119113
```
120114

121115
### Enable Snippets Support

autoload/completion.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ function! completion#wrap_completion() abort
1414
endif
1515
endfunction
1616

17+
" Depracated
1718
" Wrapper to get manually trigger working
1819
" Please send me a pull request if you know how to do this properly...
1920
function! completion#completion_wrapper()
2021
lua require'completion'.triggerCompletion()
2122
return ''
2223
endfunction
2324

25+
" Depracated
2426
function! completion#trigger_completion()
2527
return "\<c-r>=completion#completion_wrapper()\<CR>"
2628
endfunction
2729

30+
" Depracated
2831
" Wrapper of getting buffer variable
2932
" Avoid accessing to unavailable variable
3033
function! completion#get_buffer_variable(str)

doc/completion-nvim.txt

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,13 @@ g:completion_enable_auto_popup *g:completion_enable_auto_popup*
6666
mapping keys. For example:
6767
>
6868
" map <c-p> to manually trigger completion
69-
inoremap <silent><expr> <c-p> completion#trigger_completion()
69+
imap <silent> <c-p> <Plug>(completion_trigger)
7070
<
7171
Or you want to use <tab> to trigger completion without modifying the
7272
usage to <tab> keys.
7373
>
74-
function! s:check_back_space() abort
75-
let col = col('.') - 1
76-
return !col || getline('.')[col - 1] =~ '\s'
77-
endfunction
78-
79-
inoremap <silent><expr> <TAB>
80-
\ pumvisible() ? "\<C-n>" :
81-
\ <SID>check_back_space() ? "\<TAB>" :
82-
\ completion#trigger_completion()
74+
nmap <tab> <Plug>(completion_smart_tab)
75+
nmap <s-tab> <Plug>(completion_smart_s_tab)
8376
<
8477

8578
default value: 1

lua/completion.lua

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,43 @@ M.triggerCompletion = function()
3434
end
3535

3636
M.completionToggle = function()
37-
local enable = api.nvim_call_function('completion#get_buffer_variable', {'completion_enable'})
37+
local enable = vim.b.completion_enable
3838
if enable == nil then
3939
M.on_attach()
4040
elseif enable == 0 then
41-
api.nvim_buf_set_var(0, 'completion_enable', 1)
41+
vim.b.completion_enable = 1
4242
else
43-
api.nvim_buf_set_var(0, 'completion_enable', 0)
43+
vim.b.completion_enable = 0
4444
end
4545
end
4646

47+
------------------------------------------------------------------------
48+
-- smart tab --
49+
------------------------------------------------------------------------
50+
51+
function M.smart_tab()
52+
if vim.fn.pumvisible() ~= 0 then
53+
api.nvim_eval([[feedkeys("\<c-n>", "n")]])
54+
return
55+
end
56+
57+
local col = vim.fn.col('.') - 1
58+
if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then
59+
api.nvim_eval([[feedkeys("\<tab>", "n")]])
60+
return
61+
end
62+
63+
source.triggerCompletion(true, manager)
64+
end
65+
66+
function M.smart_s_tab()
67+
if vim.fn.pumvisible() ~= 0 then
68+
api.nvim_eval([[feedkeys("\<c-p>", "n")]])
69+
return
70+
end
71+
72+
api.nvim_eval([[feedkeys("\<s-tab>", "n")]])
73+
end
4774

4875
------------------------------------------------------------------------
4976
-- confirm completion --
@@ -132,7 +159,7 @@ end
132159

133160
-- TODO: need further refactor, very messy now:(
134161
function M.on_InsertEnter()
135-
local enable = api.nvim_call_function('completion#get_buffer_variable', {'completion_enable'})
162+
local enable = vim.b.completion_enable
136163
if enable == nil or enable == 0 then
137164
return
138165
end
@@ -235,7 +262,7 @@ M.on_attach = function(option)
235262
api.nvim_command("autocmd!")
236263
api.nvim_command("augroup end")
237264
end
238-
api.nvim_buf_set_var(0, 'completion_enable', 1)
265+
vim.b.completion_enable = 1
239266
end
240267

241268
return M

plugin/completion.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ inoremap <silent> <Plug>(completion_next_source)
122122
inoremap <silent> <Plug>(completion_prev_source)
123123
\ <cmd>lua require'completion'.prevSource()<CR>
124124
125+
inoremap <silent> <Plug>(completion_smart_tab)
126+
\ <cmd>lua require'completion'.smart_tab()<CR>
127+
128+
inoremap <silent> <Plug>(completion_smart_s_tab)
129+
\ <cmd>lua require'completion'.smart_s_tab()<CR>
130+
131+
inoremap <silent> <Plug>(completion_trigger)
132+
\ <cmd>lua require'completion'.triggerCompletion()<CR>
133+
125134
command! -nargs=0 CompletionToggle lua require'completion'.completionToggle()
126135

127136
let &cpo = s:save_cpo

0 commit comments

Comments
 (0)