Browse Source

implement async completion

pull/7/head
KUDO Shunsuke 3 years ago
parent
commit
61a17a1579
  1. 15
      autoload/ddc_vim_lsp.vim
  2. 32
      denops/ddc-sources/ddc-vim-lsp.ts

15
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

32
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<void> {
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<Candidate[]> {
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 [];
}
}

Loading…
Cancel
Save