Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add api to get async completion item results #140

Open
prabirshrestha opened this issue May 5, 2017 · 9 comments
Open

add api to get async completion item results #140

prabirshrestha opened this issue May 5, 2017 · 9 comments

Comments

@prabirshrestha
Copy link

I would like to create asyncomplete.vim source using tsuquyomi. I have currently implemented it for tscompletejob at https://github.com/prabirshrestha/asyncomplete-tscompletejob.vim.

Would it be possible to implement a methods so that I can create these sources:

function! asyncomplete#sources#tsuquyomi#completor(opt, ctx) abort
    if tsuquyomi#isRunning()
        let l:typed = a:ctx['typed']
        let l:startcol = tsuquyomi#get_complete_position(l:typed)
        call tsuquyomi#get_completions(l:startcol, {matches->s:on_completion(a:opt, a:ctx, l:startcol, items)})
    endif
endfunction

function! s:on_completion(opt, ctx, startcol, matches)
    asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches)
endfunction
@Quramy
Copy link
Owner

Quramy commented May 7, 2017

Hi @prabirshrestha .

First, I can add an method tsuquyomi#get_completions such as

function! tsuquyuomi#get_completions(callback)
  " delegate tsuquyomi#complete and creates matches list 
  call a:callback(matches)
endfunction

However, tsuquyomi#complete communicates with tsserver synchronously and to make this async is a big burden for me(I want to get down someday).
So all I can do is provide the completion method(tsuquyomi#get_completions). Is it OK?

Second, I don't imagine what tsuquyomi#get_complete_position(l:typed) returns. Would you explain this function's spec?

@prabirshrestha
Copy link
Author

get_complete_position is not required but nice to have. It is up to you whether you want to provide this or not. Given the following code my cursor is at |, so when I call get_complete_position it would return me col number 7 (after period). Then I can ask get_completions to return me all the completions after col 7. This means I can tell to get completions from any where besides where my cursor currently is. Most likely no one would use this but you could reuse this method for vim's completefunc/omnifunc. This is not a requirement.

server.st|art

It is possible to only have get_completions too which could return the list of candidates only. Then startcol would either be exactly where the cursor was or by using \k and detecting a keyword start. I prefer the internal autocomplete engine to provide this position so it is more accurate.

startcol is used for caching. s| As soon as you type your first letter get_completions method will be called and it will be cached based on the start col unless it hits a new word or use 'refresh_pattern': '\(\k\+$\|\.$\)' to change when it should trigger. So next time I type se| get_completions will not be called and will use the asyncomplete cache making autocomplete feel very fast. (This is how VS Code works too and I have explained it in more details at roxma/nvim-completion-manager#30 (comment)). If in certain cases only partial autocomplete results are provided and you would like to trigger get_completions in next key press you can pass 1 as the last parameter call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches, 1). Useful for providing package.json autocomplete candidates. asyncomplete is smart enough to ignore results that are slow so it doesn't mater when you call asyncomplete#complete. This also means you can all it synchronously. In fact neosnippet, buffer, ultsnips are examples of synchronous completion sources.

@Quramy if you provide a branch for me to play around that has this api tsuquyuomi#get_completions(callback) I can go ahead and create asyncomplete sources and provide feedback on the PR. You might want to add error in callback although not required for asyncomplete, it is useful for logging when something goes wrong.

@prabirshrestha
Copy link
Author

@Quramy any updates on this?

Thinking again only tsuquyomi#get_completions_async function is required and it should return the completions at current position. callback should contain the candidates and startcol.

function! asyncomplete#sources#tsuquyomi#completor(opt, ctx) abort
    if tsuquyomi#is_running()
        call tsuquyomi#get_completions_async(function('s:on_completion', [a:opt, a:ctx])})
    endif
endfunction

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

@Quramy
Copy link
Owner

Quramy commented May 11, 2017

@prabirshrestha, sorry for my late reply 🙇
I'm busy with my work, but I'll provide tsuquyomi#get_completions_async.

@Quramy
Copy link
Owner

Quramy commented May 11, 2017

BTW, tsuquyomi#is_running() can be replaced with tsuquyomi#statusServer() != 'dead'.

@ghost
Copy link

ghost commented Nov 2, 2017

@prabirshrestha anything need to be done to get Tsuquyomi completion in asyncomplete.vim? Or is it not supported yet?

@prabirshrestha
Copy link
Author

@electric-ladyland Tsuquyomi would require a huge refactor to make it async.

I use language server protocol in vim so I can get async completion as well as other features such as goto definition, rename, find references and autocomplete.

Here are the relevant docs on how to configure it.

@ghost
Copy link

ghost commented Nov 4, 2017

@prabirshrestha Thanks for links, I just tried LSP and it seems to work well, especially async autocomplete for TS. My dilemma is auto-import capability, which Tsuquyomi supports. Any thoughts on this (do you use Tsuquyomi and LSP at the same time)? BTW thanks for a great set of plugins (also thanks to you @Quramy).

@prabirshrestha
Copy link
Author

@electric-ladyland vim-lsp and asyncomplete.vim doesn't support for auto-import capabilities. LSP does specify textDocument/codeAction but it hasn't been implemented yet in vim-lsp. Once it gets implemented it will be more powerful than just auto imports since any code actions such as tslint fixes, or any new code actions added by typescript will just work. I will look at this sometime early next year so in the mean time I'm open to PRs.

I have gone async for all my vim plugins and so I no longer use Tsuquyomi. I'm willing to sacrifice auto import and other great capabilities that Tsuquyomi provider over async. I strongly believe in LSP as the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants