From cd1e4c3f33dd533b68a3dab12f018626432029a9 Mon Sep 17 00:00:00 2001 From: Shougo Matsushita Date: Fri, 16 Jun 2023 13:35:11 +0900 Subject: [PATCH] Improve ddu#custom#load_config() --- autoload/ddu.vim | 88 ++++++++++++++++++++--------------------- autoload/ddu/custom.vim | 21 +++++++--- doc/ddu.txt | 1 + 3 files changed, 60 insertions(+), 50 deletions(-) diff --git a/autoload/ddu.vim b/autoload/ddu.vim index 2608743..7c778f4 100644 --- a/autoload/ddu.vim +++ b/autoload/ddu.vim @@ -1,4 +1,11 @@ function ddu#start(options = {}) abort + " You cannot use ddu.vim in the command line window. + if getcmdwintype() !=# '' + call ddu#util#print_error( + \ 'You cannot call ddu.vim in the command line window.') + return + endif + call ddu#_notify('start', [a:options]) endfunction function ddu#redraw(name, options = {}) abort @@ -68,17 +75,10 @@ function ddu#_request(method, args) abort return {} endif - " NOTE: If call denops#plugin#wait() in vim_starting, freezed! - if has('vim_starting') - call ddu#util#print_error( - \ 'You cannot call ddu.vim in vim_starting.') - return {} - endif - - " You cannot use ddu.vim in the command line window. - if getcmdwintype() !=# '' - call ddu#util#print_error( - \ 'You cannot call ddu.vim in the command line window.') + if !ddu#_denops_running() + " Lazy call request + execute printf('autocmd User DDUReady call ' + \ .. 'denops#request("ddu", "%s", %s)', a:method, a:args->string()) return {} endif @@ -92,18 +92,37 @@ function ddu#_notify(method, args) abort return {} endif - if ddu#_denops_running() - if denops#plugin#wait('ddu') - return {} - endif - call denops#notify('ddu', a:method, a:args) - else + if !ddu#_denops_running() " Lazy call notify execute printf('autocmd User DDUReady call ' - \ .. 'denops#notify("ddu", "%s", %s)', a:method, string(a:args)) + \ .. 'denops#notify("ddu", "%s", %s)', a:method, a:args->string()) + return {} + endif + + if denops#plugin#wait('ddu') + return {} endif + return denops#notify('ddu', a:method, a:args) +endfunction + +const s:root_dir = ''->expand()->fnamemodify(':h:h') +const s:sep = has('win32') ? '\' : '/' +function ddu#_register() abort + call denops#plugin#register('ddu', + \ [s:root_dir, 'denops', 'ddu', 'app.ts']->join(s:sep), + \ #{ mode: 'skip' }) + + autocmd ddu User DenopsClosed call s:stopped() +endfunction + +function ddu#_denops_running() abort + return 'g:loaded_denops'->exists() + \ && denops#server#status() ==# 'running' + \ && denops#plugin#is_loaded('ddu') +endfunction - return {} +function ddu#_lazy_redraw(name, args = {}) abort + call timer_start(0, { -> ddu#redraw(a:name, a:args) }) endfunction function s:init() abort @@ -133,33 +152,14 @@ function s:init() abort endif endfunction -const s:root_dir = ''->expand()->fnamemodify(':h:h') -const s:sep = has('win32') ? '\' : '/' -function ddu#_register() abort - call denops#plugin#register('ddu', - \ [s:root_dir, 'denops', 'ddu', 'app.ts']->join(s:sep), - \ #{ mode: 'skip' }) - - autocmd ddu User DenopsClosed call s:stopped() -endfunction - function s:stopped() abort unlet! s:initialized " Restore custom config - if 'g:ddu#_customs'->exists() - for custom in g:ddu#_customs - call ddu#_notify(custom.method, custom.args) - endfor - endif -endfunction - -function ddu#_denops_running() abort - return 'g:loaded_denops'->exists() - \ && denops#server#status() ==# 'running' - \ && denops#plugin#is_loaded('ddu') -endfunction - -function ddu#_lazy_redraw(name, args = {}) abort - call timer_start(0, { -> ddu#redraw(a:name, a:args) }) + for custom in g:->get('ddu#_notifies', []) + call ddu#_notify(custom.method, custom.args) + endfor + for custom in g:->get('ddu#_requests', []) + call ddu#_request(custom.method, custom.args) + endfor endfunction diff --git a/autoload/ddu/custom.vim b/autoload/ddu/custom.vim index 6fb773f..ee16c6e 100644 --- a/autoload/ddu/custom.vim +++ b/autoload/ddu/custom.vim @@ -20,7 +20,7 @@ function ddu#custom#load_config(path) abort return endif - return s:notify('loadConfig', [a:path]) + return s:request('loadConfig', [a:path]) endfunction let s:aliases = #{ @@ -97,12 +97,21 @@ function s:normalize_string_or_list(string_or_list) abort endfunction function s:notify(method, args) abort - " Save notify args - if !('g:ddu#_customs'->exists()) - let g:ddu#_customs = [] + " Save args + if !('g:ddu#_notifies'->exists()) + let g:ddu#_notifies = [] endif - - call add(g:ddu#_customs, #{ method: a:method, args: a:args }) + call add(g:ddu#_notifies, #{ method: a:method, args: a:args }) return ddu#_notify(a:method, a:args) endfunction + +function s:request(method, args) abort + " Save args + if !('g:ddu#_requests'->exists()) + let g:ddu#_requests = [] + endif + call add(g:ddu#_requests, #{ method: a:method, args: a:args }) + + return ddu#_request(a:method, a:args) +endfunction diff --git a/doc/ddu.txt b/doc/ddu.txt index 2b19359..4323320 100644 --- a/doc/ddu.txt +++ b/doc/ddu.txt @@ -419,6 +419,7 @@ ddu#custom#get_local() ddu#custom#load_config({path}) Load TypeScript configuration from {path} file. NOTE: {path} must be full path. + NOTE: The loading is asynchronous. *ddu#custom#patch_global()* ddu#custom#patch_global({option-name}, {value})