Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Add completion #6

Merged
merged 3 commits into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions autoload/minisnip.vim
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,56 @@ function! s:SelectPlaceholder()
" restore old value of s register
let @s = l:old_s
endfunction

function! minisnip#complete() abort
" Locate the start of the word
let l:line = getline('.')
let l:start = col('.') - 1
while l:start > 0 && l:line[l:start - 1] =~? '\a'
let l:start -= 1
endwhile
let l:base = l:line[l:start : col('.')-1]
if l:base is# ' '
let l:base = ''
endif

" Load all snippets that match.
let l:filetypes = split(&filetype, '\.')
let l:all = []
for l:dir in split(g:minisnip_dir, ':')
for l:path in glob(l:dir . '/*', 0, 1)
let l:f = fnamemodify(l:path, ':t')
let l:ft = l:f[1:stridx(l:f[1:], '_')]
let l:name = l:f

" Filetype snippet
if l:f[0] is# '_'
if index(l:filetypes, l:ft) is -1
continue
endif
let l:name = l:f[stridx(l:f[1:], '_') + 2:]
endif

if l:name !~? '^' . l:base
continue
endif
let l:all += [[l:name, readfile(l:path)]]
endfor
endfor
call sort(l:all, {i1, i2 -> l:i1[0] == l:i2[0] ? 0 : l:i1[0] > l:i2[0] ? 1 : -1})

" Format how complete() expects it.
let l:res = []
for l:m in l:all
call add(l:res, {
\ 'icase': 1,
\ 'word': l:m[0],
\ 'abbr': l:m[0],
\ 'menu': l:m[1][0],
\ 'info': join(l:m[1], "\n"),
\ })
endfor

call complete(l:start + 1, l:res)
return ''
endfunction
6 changes: 6 additions & 0 deletions doc/minisnip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ MAPPINGS *minisnip-mappings*
placeholder and enter select mode.
In select mode, jump to the next placeholder.

-------------------------------------------------------------------------------
*minisnip_<C-x><C-t>*
<C-x><C-t> Start |ins-completion| for the snippet. Press <C-t> again
to go to the next snippet; press <CR> or <Tab> to complete
it.

===============================================================================
CONFIGURATION *minisnip-configuration*

Expand Down
19 changes: 13 additions & 6 deletions plugin/minisnip.vim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if exists("g:loaded_minisnip")
if exists('g:loaded_minisnip')
finish
endif

Expand All @@ -18,15 +18,22 @@ let g:minisnip_delimpat = '\V' . g:minisnip_startdelim . '\.\{-}' . g:minisnip_e
" plug mappings
" the eval/escape charade is to convert ex. <Tab> into a literal tab, first
" making it \<Tab> and then eval'ing that surrounded by double quotes
inoremap <script> <expr> <Plug>Minisnip minisnip#ShouldTrigger() ?
inoremap <script> <expr> <Plug>(minisnip) minisnip#ShouldTrigger() ?
\"x\<bs>\<esc>:call \minisnip#Minisnip()\<cr>" :
\eval('"' . escape(g:minisnip_trigger, '\"<') . '"')
snoremap <script> <expr> <Plug>Minisnip minisnip#ShouldTrigger() ?
snoremap <script> <expr> <Plug>(minisnip) minisnip#ShouldTrigger() ?
\"\<esc>:call \minisnip#Minisnip()\<cr>" :
\eval('"' . escape(g:minisnip_trigger, '\"<') . '"')
inoremap <silent> <Plug>(minisnip-complete) <C-r>=minisnip#complete()<CR>

" add the default mappings if the user hasn't defined any
if !hasmapto('<Plug>Minisnip')
execute 'imap <unique> ' . g:minisnip_trigger . ' <Plug>Minisnip'
execute 'smap <unique> ' . g:minisnip_trigger . ' <Plug>Minisnip'
if !hasmapto('<Plug>(minisnip)')
execute 'imap <unique> ' . g:minisnip_trigger . ' <Plug>(minisnip)'
execute 'smap <unique> ' . g:minisnip_trigger . ' <Plug>(minisnip)'
endif
" Completion
if !hasmapto('<Plug>(minisnip-complete)')
imap <C-x><C-t> <Plug>(minisnip-complete)
inoremap <expr> <C-t> pumvisible() ? "\<C-n>" : "\<C-t>"
imap <expr> <CR> pumvisible() ? "\<Tab>" : "\<CR>"
endif