Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Created November 23, 2016 06:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prabirshrestha/0b3b7b4e2112c662979bfeeec4e1edb7 to your computer and use it in GitHub Desktop.
Save prabirshrestha/0b3b7b4e2112c662979bfeeec4e1edb7 to your computer and use it in GitHub Desktop.
async omnifunc
" save all the completions
let s:completions = []
function! asyncomni#omnifunc(findstart, base)
if a:findstart
let l:line_string = getline('.')
let l:line = line('.')
let l:col = col('.')
" locate start of the word
let l:start = l:col -1
while l:start > 0 && l:line_string[l:start -1] =~ '\a'
let l:start -= 1
endwhile
echom 'l:start'.l:start
return l:start
endif
return s:completions
endfunction
let s:timer = -1
function! s:complete()
if s:timer == -1
let s:timer = timer_start(1000, function('s:loaded_completions'))
endif
endfunction
function! s:loaded_completions(timer)
let s:completions = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
" since this is a callback we got s:complete asynchrounsly
" refresh the list since this is async
if !empty(s:completions)
setlocal completeopt-=longest
setlocal completeopt+=menuone
setlocal completeopt-=menu
if &completeopt !~# 'noinsert\|noselect'
setlocal completeopt+=noselect
endif
call feedkeys("\<C-x>\<C-o>", 'n')
endif
endfunction
augroup asyncomni_completor
autocmd!
autocmd TextChangedI * call s:complete()
augroup end
if !exists('g:loaded_asyncomni')
let g:loaded_asyncomni = 1
endif
let g:loaded_asyncomni = 1
if exists('&omnifunc')
setl omnifunc+=asyncomni#omnifunc
endif
@Olical
Copy link

Olical commented Mar 30, 2019

This is a really interesting approach! Thanks for sharing! Glad I found it years later 😛 going to see if I can get it working in https://github.com/Olical/conjure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment