Skip to content

Commit

Permalink
Improve ddu#custom#load_config()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Jun 16, 2023
1 parent 78f2143 commit cd1e4c3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 50 deletions.
88 changes: 44 additions & 44 deletions autoload/ddu.vim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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 = '<sfile>'->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
Expand Down Expand Up @@ -133,33 +152,14 @@ function s:init() abort
endif
endfunction

const s:root_dir = '<sfile>'->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
21 changes: 15 additions & 6 deletions autoload/ddu/custom.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 = #{
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions doc/ddu.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down

0 comments on commit cd1e4c3

Please sign in to comment.