Skip to content

Commit

Permalink
Merge pull request #392 from vim-denops/event
Browse files Browse the repository at this point in the history
💪 use `denops#_internal#event#emit()` instead `doautocmd`
  • Loading branch information
lambdalisue authored Jul 13, 2024
2 parents 0e33a62 + 8bbcde4 commit 9ce289c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 186 deletions.
3 changes: 3 additions & 0 deletions autoload/denops/_internal/event.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function denops#_internal#event#emit(name) abort
execute 'doautocmd <nomodeline> User' a:name
endfunction
12 changes: 6 additions & 6 deletions autoload/denops/_internal/plugin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function! s:DenopsSystemPluginPre() abort
const l:name = matchstr(expand('<amatch>'), 'DenopsSystemPluginPre:\zs.*')
let l:plugin = denops#_internal#plugin#get(l:name)
let l:plugin.state = s:STATE_LOADING
execute printf('doautocmd <nomodeline> User DenopsPluginPre:%s', l:name)
call denops#_internal#event#emit(printf('DenopsPluginPre:%s', l:name))
endfunction

function! s:DenopsSystemPluginPost() abort
Expand All @@ -76,37 +76,37 @@ function! s:DenopsSystemPluginPost() abort
for l:Callback in l:callbacks
call l:Callback()
endfor
execute printf('doautocmd <nomodeline> User DenopsPluginPost:%s', l:name)
call denops#_internal#event#emit(printf('DenopsPluginPost:%s', l:name))
endfunction

function! s:DenopsSystemPluginFail() abort
const l:name = matchstr(expand('<amatch>'), 'DenopsSystemPluginFail:\zs.*')
let l:plugin = denops#_internal#plugin#get(l:name)
let l:plugin.state = s:STATE_FAILED
let l:plugin.callbacks = []
execute printf('doautocmd <nomodeline> User DenopsPluginFail:%s', l:name)
call denops#_internal#event#emit(printf('DenopsPluginFail:%s', l:name))
endfunction

function! s:DenopsSystemPluginUnloadPre() abort
const l:name = matchstr(expand('<amatch>'), 'DenopsSystemPluginUnloadPre:\zs.*')
let l:plugin = denops#_internal#plugin#get(l:name)
let l:plugin.state = s:STATE_UNLOADING
execute printf('doautocmd <nomodeline> User DenopsPluginUnloadPre:%s', l:name)
call denops#_internal#event#emit(printf('DenopsPluginUnloadPre:%s', l:name))
endfunction

function! s:DenopsSystemPluginUnloadPost() abort
const l:name = matchstr(expand('<amatch>'), 'DenopsSystemPluginUnloadPost:\zs.*')
let l:plugin = denops#_internal#plugin#get(l:name)
let l:plugin.state = s:STATE_RESERVED
execute printf('doautocmd <nomodeline> User DenopsPluginUnloadPost:%s', l:name)
call denops#_internal#event#emit(printf('DenopsPluginUnloadPost:%s', l:name))
endfunction

function! s:DenopsSystemPluginUnloadFail() abort
const l:name = matchstr(expand('<amatch>'), 'DenopsSystemPluginUnloadFail:\zs.*')
let l:plugin = denops#_internal#plugin#get(l:name)
let l:plugin.state = s:STATE_RESERVED
let l:plugin.callbacks = []
execute printf('doautocmd <nomodeline> User DenopsPluginUnloadFail:%s', l:name)
call denops#_internal#event#emit(printf('DenopsPluginUnloadFail:%s', l:name))
endfunction

augroup denops_autoload_plugin_internal
Expand Down
2 changes: 1 addition & 1 deletion autoload/denops/_internal/server/chan.vim
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function! s:on_close(options) abort
let s:chan = v:null
call s:clear_force_close_delayer()
call denops#_internal#echo#debug(printf('Channel closed (%s)', s:addr))
doautocmd <nomodeline> User DenopsSystemClosed
call denops#_internal#event#emit('DenopsSystemClosed')
if s:chan isnot# v:null || !a:options.reconnect_on_close || s:closed_on_purpose || s:exiting
return
endif
Expand Down
6 changes: 3 additions & 3 deletions autoload/denops/_internal/server/proc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function! s:start(options) abort
\ 'on_exit': { _job, status, _event -> s:on_exit(a:options, status) },
\})
call denops#_internal#echo#debug(printf('Server started: %s', l:args))
doautocmd <nomodeline> User DenopsSystemProcessStarted
call denops#_internal#event#emit('DenopsSystemProcessStarted')
endfunction

function! s:on_stdout(store, data) abort
Expand All @@ -81,7 +81,7 @@ function! s:on_stdout(store, data) abort
let a:store.prepared = 1
let l:addr = substitute(a:data, '\r\?\n$', '', 'g')
call denops#_internal#echo#debug(printf('Server listen: %s', l:addr))
execute printf('doautocmd <nomodeline> User DenopsSystemProcessListen:%s', l:addr)
call denops#_internal#event#emit(printf('DenopsSystemProcessListen:%s', l:addr))
endfunction

function! s:on_stderr(data) abort
Expand All @@ -95,7 +95,7 @@ endfunction
function! s:on_exit(options, status) abort
let s:job = v:null
call denops#_internal#echo#debug(printf('Server stopped: %s', a:status))
execute printf('doautocmd <nomodeline> User DenopsSystemProcessStopped:%s', a:status)
call denops#_internal#event#emit(printf('DenopsSystemProcessStopped:%s', a:status))
if s:job isnot# v:null || !a:options.restart_on_exit || s:stopped_on_purpose || s:exiting
return
endif
Expand Down
11 changes: 4 additions & 7 deletions autoload/denops/server.vim
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function! s:disconnect(...) abort
endfunction

function! s:DenopsSystemProcessStarted() abort
doautocmd <nomodeline> User DenopsProcessStarted
call denops#_internal#event#emit('DenopsProcessStarted')
endfunction

function! s:DenopsSystemProcessListen(expr) abort
Expand All @@ -229,7 +229,7 @@ function! s:DenopsSystemReady() abort
call l:Callback()
endfor
finally
doautocmd <nomodeline> User DenopsReady
call denops#_internal#event#emit('DenopsReady')
endtry
endfunction

Expand All @@ -253,7 +253,7 @@ function! s:DenopsSystemClosed() abort
endif
let s:local_addr = ""
finally
doautocmd <nomodeline> User DenopsClosed
call denops#_internal#event#emit('DenopsClosed')
endtry
endfunction

Expand All @@ -268,10 +268,7 @@ function! s:DenopsSystemProcessStopped(expr) abort
call denops#server#start()
endif
finally
execute printf(
\ 'doautocmd <nomodeline> User DenopsProcessStopped:%s',
\ l:status,
\)
call denops#_internal#event#emit(printf('DenopsProcessStopped:%s', l:status))
endtry
endfunction

Expand Down
2 changes: 1 addition & 1 deletion denops/@denops-private/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function createScriptSuffix(script: string): string {
/** NOTE: `emit()` is never throws or rejects. */
async function emit(denops: Denops, name: string): Promise<void> {
try {
await denops.cmd(`doautocmd <nomodeline> User ${name}`);
await denops.call("denops#_internal#event#emit", name);
} catch (e) {
console.error(`Failed to emit ${name}: ${e}`);
}
Expand Down
Loading

0 comments on commit 9ce289c

Please sign in to comment.