justmao945 / vim-clang

Clang completion plugin for vim
ISC License
357 stars 47 forks source link

support async completion api #111

Open prabirshrestha opened 7 years ago

prabirshrestha commented 7 years ago

I would like to write a clang autocomplete source for asyncomplete.vim

Would it be possible to have an api that looks something like this.

function! asyncomplete#sources#vimclang#completor(opt, ctx) abort
        call ClangGetCompletionItemsAsync(function('s:on_completion', [a:opt, a:ctx])})
endfunction

function! s:on_completion(opt, ctx, candidates, startcol)
    asyncomplete#complete(a:opt['name'], a:ctx, a:startcol, a:candidates)
endfunction

function! asyncomplete#sources#vimclang#get_source_options(opts)
   return extend(extend({}, a:opts), {
       \ 'refresh_pattern': '\(\k\+$\|\.$\->$\)',
       \ })
endfunction

ClangGetCompletionItemsAsync should take a callback parameter and return candidates and startcol from where the cursor is. candidates contains list of completion items and startcol is where it should start. person.fi| if | is where the cursor is, candidates would be ['first_name', 'last_name'] and startcol would be 7. asyncomplete uses startcol to cache so if you type the r, it will reuse the cache. Since caching happens aggressively you can add your custom refresh_pattern so that it will force call the completor function any time it starts a new word or types . or '->`.

In vimrc you can then register using the following code.

au User asyncomplete_setup call asyncomplete#register_source(asyncomplete#sources#vimclang#get_source_options({
    \ 'name': 'vimclang',
    \ 'whitelist': ['c', 'c++'],
    \ 'completor': function('asyncomplete#sources#vimclang#completor'),
    \ }))
smekkley commented 3 years ago

I'm wondering if #130 helps this issue nicely. So far, I don't find it a big issue that clang call is blocking though, it seems fast enough. If clangd implementation makes it even faster with blocking mode, for me it'd be even more awesome than having asynchronous mode.