From 61a17a15792b990adb0853b1bf034f7ebb867a4f Mon Sep 17 00:00:00 2001 From: KUDO Shunsuke Date: Tue, 31 Aug 2021 00:06:00 +0900 Subject: [PATCH] implement async completion --- autoload/ddc_vim_lsp.vim | 15 ++++++++++----- denops/ddc-sources/ddc-vim-lsp.ts | 32 ++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/autoload/ddc_vim_lsp.vim b/autoload/ddc_vim_lsp.vim index c3309f0..b65b041 100644 --- a/autoload/ddc_vim_lsp.vim +++ b/autoload/ddc_vim_lsp.vim @@ -1,6 +1,5 @@ -function! ddc_vim_lsp#_callback(server, position, plugin_name, method_name, data) abort +function! ddc_vim_lsp#_callback(server, position, data) abort if lsp#client#is_error(a:data) || !has_key(a:data, 'response') || !has_key(a:data['response'], 'result') - call denops#request(a:plugin_name, a:method_name, []) return endif @@ -10,10 +9,16 @@ function! ddc_vim_lsp#_callback(server, position, plugin_name, method_name, data \ 'response': a:data['response'], \ } let lspitems = lsp#omni#get_vim_completion_items(l:options)['items'] - call denops#request(a:plugin_name, a:method_name, [lspitems]) + + if len(lspitems) > 0 + let g:ddc#source#ddc_vim_lsp#_results = lspitems + let g:ddc#source#ddc_vim_lsp#_requested = v:true + + call ddc#refresh_candidates() + endif endfunction -function! ddc_vim_lsp#request(server_name, plugin_name, method_name) abort +function! ddc_vim_lsp#request(server_name) abort let l:server = lsp#get_server_info(a:server_name) let l:position = lsp#get_position() @@ -23,6 +28,6 @@ function! ddc_vim_lsp#request(server_name, plugin_name, method_name) abort \ 'textDocument': lsp#get_text_document_identifier(), \ 'position': l:position, \ }, - \ 'on_notification': function('ddc_vim_lsp#_callback', [l:server, l:position, a:plugin_name, a:method_name]), + \ 'on_notification': function('ddc_vim_lsp#_callback', [l:server, l:position]), \ }) endfunction diff --git a/denops/ddc-sources/ddc-vim-lsp.ts b/denops/ddc-sources/ddc-vim-lsp.ts index 868f8e4..3222fb9 100644 --- a/denops/ddc-sources/ddc-vim-lsp.ts +++ b/denops/ddc-sources/ddc-vim-lsp.ts @@ -4,7 +4,9 @@ import { } from "https://deno.land/x/ddc_vim@v0.4.2/types.ts#^"; import { + batch, Denops, + vars, } from "https://deno.land/x/ddc_vim@v0.4.2/deps.ts#^"; import { @@ -12,25 +14,41 @@ import { } from "https://deno.land/x/denops_std@v1.8.1/anonymous/mod.ts"; export class Source extends BaseSource { + async onInit(args: { + denops: Denops, + }): Promise { + await batch(args.denops, async (denops) => { + vars.g.set(denops, "ddc#source#ddc_vim_lsp#_results", []); + vars.g.set(denops, "ddc#source#ddc_vim_lsp#_requested", false); + vars.g.set(denops, "ddc#source#ddc_vim_lsp#_prev_input", ""); + }); + } + async gatherCandidates(args: { denops: Denops, context: Context, completeStr: string, }): Promise { + const prevInput = await vars.g.get(args.denops, "ddc#source#ddc_vim_lsp#_prev_input"); + const requested = await vars.g.get(args.denops, "ddc#source#ddc_vim_lsp#_requested"); + if (args.context.input == prevInput && requested) { + return await vars.g.get(args.denops, "ddc#source#ddc_vim_lsp#_results"); + } const lspservers = await args.denops.call("lsp#get_allowed_servers"); if (lspservers.length === 0) { return []; } - return new Promise((resolve) => { + await batch(args.denops, async (denops) => { + vars.g.set(denops, "ddc#source#ddc_vim_lsp#_results", []); + vars.g.set(denops, "ddc#source#ddc_vim_lsp#_requested", false); + vars.g.set(denops, "ddc#source#ddc_vim_lsp#_prev_input", args.context.input); + // NOTE: choose first lsp server - args.denops.call("ddc_vim_lsp#request", lspservers[0], args.denops.name, once(args.denops, (response) => { - resolve(response); - })[0]) - }) - .then((cs: Candidate[]) => { - return cs; + denops.call("ddc_vim_lsp#request", lspservers[0]); }); + + return []; } }