Skip to content

Commit 4cd759f

Browse files
authored
Merge pull request #342 from vim-denops/cache
👍 Add `denops#cache#update()` function to update Deno module cache of denops itself and denops plugins in `runtimepath`
2 parents 52bff98 + 1245bb3 commit 4cd759f

File tree

5 files changed

+136
-87
lines changed

5 files changed

+136
-87
lines changed

autoload/denops.vim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
function! denops#notify(plugin, method, params) abort
1+
function! denops#notify(name, method, params) abort
22
call denops#_internal#server#chan#notify(
33
\ 'invoke',
4-
\ ['dispatch', [a:plugin, a:method, a:params]],
4+
\ ['dispatch', [a:name, a:method, a:params]],
55
\)
66
endfunction
77

8-
function! denops#request(plugin, method, params) abort
8+
function! denops#request(name, method, params) abort
99
return denops#_internal#server#chan#request(
1010
\ 'invoke',
11-
\ ['dispatch', [a:plugin, a:method, a:params]],
11+
\ ['dispatch', [a:name, a:method, a:params]],
1212
\)
1313
endfunction
1414

15-
function! denops#request_async(plugin, method, params, success, failure) abort
15+
function! denops#request_async(name, method, params, success, failure) abort
1616
let l:success = denops#callback#register(a:success, {
1717
\ 'once': v:true,
1818
\})
@@ -21,7 +21,7 @@ function! denops#request_async(plugin, method, params, success, failure) abort
2121
\})
2222
return denops#_internal#server#chan#notify(
2323
\ 'invoke',
24-
\ ['dispatchAsync', [a:plugin, a:method, a:params, l:success, l:failure]],
24+
\ ['dispatchAsync', [a:name, a:method, a:params, l:success, l:failure]],
2525
\)
2626
endfunction
2727

autoload/denops/_internal/plugin.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function! denops#_internal#plugin#collect() abort
2+
let l:pattern = denops#_internal#path#join(['denops', '*', 'main.ts'])
3+
let l:plugins = []
4+
for l:script in globpath(&runtimepath, l:pattern, 1, 1, 1)
5+
let l:name = fnamemodify(l:script, ':h:t')
6+
if l:name[:0] ==# '@' || !filereadable(l:script)
7+
continue
8+
endif
9+
call add(l:plugins, #{ name: l:name, script: l:script })
10+
endfor
11+
return l:plugins
12+
endfunction
13+
14+
function! denops#_internal#plugin#find(name) abort
15+
let l:pattern = denops#_internal#path#join(['denops', a:name, 'main.ts'])
16+
for l:script in globpath(&runtimepath, l:pattern, 1, 1, 1)
17+
let l:name = fnamemodify(l:script, ':h:t')
18+
if l:name[:0] ==# '@' || !filereadable(l:script)
19+
continue
20+
endif
21+
return #{ name: l:name, script: l:script }
22+
endfor
23+
throw printf('No denops plugin for "%s" exists', a:name)
24+
endfunction

autoload/denops/cache.vim

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
let s:root = expand('<sfile>:p:h:h:h')
2+
let s:job = v:null
3+
4+
function! denops#cache#update(...) abort
5+
let l:options = extend(#{ reload: v:false }, a:0 ? a:1 : {})
6+
let l:entryfiles = extend([
7+
\ denops#_internal#path#join([s:root, 'denops', '@denops-private', 'mod.ts']),
8+
\], map(denops#_internal#plugin#collect(), { _, v -> v.script }))
9+
10+
let l:args = [g:denops#deno, 'cache']
11+
12+
if l:options.reload
13+
let l:args = add(l:args, '--reload')
14+
echomsg '[denops] Forcibly update cache of the following files.'
15+
else
16+
echomsg '[denops] Update cache of the following files. Call `denops#cache#update(#{reload: v:true})` to forcibly update.'
17+
endif
18+
19+
for l:entryfile in l:entryfiles
20+
echomsg printf('[denops] %s', l:entryfile)
21+
endfor
22+
let l:args = extend(l:args, l:entryfiles)
23+
24+
let s:job = denops#_internal#job#start(l:args, #{
25+
\ on_stderr: funcref('s:on_stderr'),
26+
\ on_exit: funcref('s:on_exit'),
27+
\ env: #{
28+
\ NO_COLOR: 1,
29+
\ DENO_NO_PROMPT: 1,
30+
\ },
31+
\})
32+
call denops#_internal#wait#for(60 * 1000, { -> s:job is# v:null }, 100)
33+
echomsg '[denops] Deno cache is updated.'
34+
endfunction
35+
36+
function! s:on_stderr(job, data, event) abort
37+
echohl Comment
38+
for l:line in split(a:data, '\n')
39+
echomsg printf('[denops] %s', substitute(l:line, '\t', ' ', 'g'))
40+
endfor
41+
echohl None
42+
endfunction
43+
44+
function! s:on_exit(job, status, event) abort
45+
let s:job = v:null
46+
endfunction

autoload/denops/plugin.vim

Lines changed: 26 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
let s:loaded_plugins = {}
22
let s:load_callbacks = {}
33

4-
function! denops#plugin#is_loaded(plugin) abort
5-
return has_key(s:loaded_plugins, a:plugin)
4+
function! denops#plugin#is_loaded(name) abort
5+
return has_key(s:loaded_plugins, a:name)
66
endfunction
77

8-
function! denops#plugin#wait(plugin, ...) abort
8+
function! denops#plugin#wait(name, ...) abort
99
let l:options = extend({
1010
\ 'interval': g:denops#plugin#wait_interval,
1111
\ 'timeout': g:denops#plugin#wait_timeout,
@@ -16,40 +16,40 @@ function! denops#plugin#wait(plugin, ...) abort
1616
if !l:options.silent
1717
call denops#_internal#echo#error(printf(
1818
\ 'Failed to wait for "%s" to start. Denops server itself is not started.',
19-
\ a:plugin,
19+
\ a:name,
2020
\))
2121
endif
2222
return -2
2323
endif
24-
if has_key(s:loaded_plugins, a:plugin)
25-
return s:loaded_plugins[a:plugin]
24+
if has_key(s:loaded_plugins, a:name)
25+
return s:loaded_plugins[a:name]
2626
endif
2727
let l:ret = denops#_internal#wait#for(
2828
\ l:options.timeout,
29-
\ { -> has_key(s:loaded_plugins, a:plugin) },
29+
\ { -> has_key(s:loaded_plugins, a:name) },
3030
\ l:options.interval,
3131
\)
3232
if l:ret is# -1
3333
if !l:options.silent
3434
call denops#_internal#echo#error(printf(
3535
\ 'Failed to wait for "%s" to start. It took more than %d milliseconds and timed out.',
36-
\ a:plugin,
36+
\ a:name,
3737
\ l:options.timeout,
3838
\))
3939
endif
4040
return -1
4141
endif
4242
endfunction
4343

44-
function! denops#plugin#wait_async(plugin, callback) abort
45-
if has_key(s:loaded_plugins, a:plugin)
46-
if s:loaded_plugins[a:plugin] isnot# 0
44+
function! denops#plugin#wait_async(name, callback) abort
45+
if has_key(s:loaded_plugins, a:name)
46+
if s:loaded_plugins[a:name] isnot# 0
4747
return
4848
endif
4949
call a:callback()
5050
return
5151
endif
52-
let l:callbacks = get(s:load_callbacks, a:plugin, [])
52+
let l:callbacks = get(s:load_callbacks, a:name, [])
5353
call add(l:callbacks, a:callback)
5454
let s:load_callbacks[a:plugin] = l:callbacks
5555
endfunction
@@ -58,47 +58,45 @@ endfunction
5858
" Some plugins (e.g. dein.vim) use this function with options thus we cannot
5959
" change the interface of this function.
6060
" That's why we introduce 'load' function that replaces this function.
61-
function! denops#plugin#register(plugin, ...) abort
61+
function! denops#plugin#register(name, ...) abort
6262
call denops#_internal#echo#deprecate(
6363
\ 'denops#plugin#register() is deprecated. Use denops#plugin#load() instead.',
6464
\)
6565
if a:0 is# 0 || type(a:1) is# v:t_dict
66-
let l:script = s:find_plugin(a:plugin)
66+
let l:script = denops#_internal#plugin#find(a:name).script
6767
else
6868
let l:script = a:1
6969
endif
70-
return denops#plugin#load(a:plugin, l:script)
70+
return denops#plugin#load(a:name, l:script)
7171
endfunction
7272

73-
function! denops#plugin#load(plugin, script) abort
73+
function! denops#plugin#load(name, script) abort
7474
let l:script = denops#_internal#path#norm(a:script)
75-
let l:args = [a:plugin, l:script]
75+
let l:args = [a:name, l:script]
7676
call denops#_internal#echo#debug(printf('load plugin: %s', l:args))
7777
call denops#_internal#server#chan#notify('invoke', ['load', l:args])
7878
endfunction
7979

80-
function! denops#plugin#reload(plugin) abort
81-
let l:args = [a:plugin]
80+
function! denops#plugin#reload(name) abort
81+
let l:args = [a:name]
8282
call denops#_internal#echo#debug(printf('reload plugin: %s', l:args))
8383
call denops#_internal#server#chan#notify('invoke', ['reload', l:args])
8484
endfunction
8585

8686
function! denops#plugin#discover() abort
87-
let l:plugins = {}
88-
call s:gather_plugins(l:plugins)
87+
let l:plugins = denops#_internal#plugin#collect()
8988
call denops#_internal#echo#debug(printf('%d plugins are discovered', len(l:plugins)))
90-
for [l:plugin, l:script] in items(l:plugins)
91-
call denops#plugin#load(l:plugin, l:script)
89+
for l:plugin in l:plugins
90+
call denops#plugin#load(l:plugin.name, l:plugin.script)
9291
endfor
9392
endfunction
9493

9594
function! denops#plugin#check_type(...) abort
96-
if !a:0
97-
let l:plugins = {}
98-
call s:gather_plugins(l:plugins)
99-
endif
95+
let l:plugins = a:0
96+
\ ? [denops#_internal#plugin#find(a:1)]
97+
\ : denops#_internal#plugin#collect()
10098
let l:args = [g:denops#deno, 'check']
101-
let l:args += a:0 ? [s:find_plugin(a:1)] : values(l:plugins)
99+
let l:args = extend(l:args, map(l:plugins, { _, v -> v.script }))
102100
let l:job = denops#_internal#job#start(l:args, {
103101
\ 'env': {
104102
\ 'NO_COLOR': 1,
@@ -112,35 +110,6 @@ function! denops#plugin#check_type(...) abort
112110
\ })
113111
endfunction
114112

115-
function! s:gather_plugins(plugins) abort
116-
for l:script in globpath(&runtimepath, denops#_internal#path#join(['denops', '*', 'main.ts']), 1, 1, 1)
117-
let l:plugin = fnamemodify(l:script, ':h:t')
118-
if l:plugin[:0] ==# '@' || has_key(a:plugins, l:plugin)
119-
continue
120-
endif
121-
call extend(a:plugins, { l:plugin : l:script })
122-
endfor
123-
endfunction
124-
125-
function! s:options(base, default) abort
126-
let l:options = extend(a:default, a:base)
127-
if l:options.mode !~# '^\(reload\|skip\|error\)$'
128-
throw printf('Unknown mode "%s" is specified', l:options.mode)
129-
endif
130-
return l:options
131-
endfunction
132-
133-
function! s:find_plugin(plugin) abort
134-
for l:script in globpath(&runtimepath, denops#_internal#path#join(['denops', a:plugin, 'main.ts']), 1, 1, 1)
135-
let l:plugin = fnamemodify(l:script, ':h:t')
136-
if l:plugin[:0] ==# '@' || !filereadable(l:script)
137-
continue
138-
endif
139-
return l:script
140-
endfor
141-
throw printf('No denops plugin for "%s" exists', a:plugin)
142-
endfunction
143-
144113
function! s:relay_autocmd(name) abort
145114
let l:plugin = matchstr(expand('<amatch>'), '^[^:]\+:\zs.*')
146115
execute printf('doautocmd <nomodeline> User %s:%s', a:name, l:plugin)

doc/denops.txt

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,26 @@ VARIABLE *denops-variable*
158158
FUNCTION *denops-function*
159159

160160
*denops#notify()*
161-
denops#notify({plugin}, {method}, {params})
162-
Calls API {method} of {plugin} with {params} and returns immediately
163-
without waiting for a result.
161+
denops#notify({name}, {method}, {params})
162+
Calls API {method} of {name} plugin with {params} and returns
163+
immediately without waiting for a result.
164164
Use |denops#request()| instead if you need a result.
165165
Note: It does not redraw in the Vim environment. Manually execute
166166
|:redraw| if needed.
167167

168168
*denops#request()*
169-
denops#request({plugin}, {method}, {params})
170-
Calls API {method} of {plugin} with {params} and waits for a result,
171-
then returns it.
169+
denops#request({name}, {method}, {params})
170+
Calls API {method} of {name} plugin with {params} and waits for a
171+
result, then returns it.
172172
Use |denops#notify()| instead if you don't need a result.
173173
Use |denops#request_async()| instead if you need a result
174174
asynchronously.
175175

176176
*denops#request_async()*
177-
denops#request_async({plugin}, {method}, {params}, {success}, {failure})
178-
Calls API {method} of {plugin} with {params} and returns immediately.
179-
Once the call succeeds, the {success} callback is called with a
180-
result.
177+
denops#request_async({name}, {method}, {params}, {success}, {failure})
178+
Calls API {method} of {name} plugin with {params} and returns
179+
immediately. Once the call succeeds, the {success} callback is called
180+
with a result.
181181
Otherwise, the {failure} callback is called with an error.
182182
Use |denops#notify()| instead if you don't need a result.
183183
Use |denops#request()| instead if you need a result synchronously.
@@ -200,6 +200,16 @@ denops#request_async({plugin}, {method}, {params}, {success}, {failure})
200200
\ { e -> s:failure(e) },
201201
\)
202202
<
203+
*denops#cache#update()*
204+
denops#cache#update([{options}])
205+
Update Deno module cache of denops itself and denops plugins in
206+
|runtimepath|.
207+
208+
The following {options} is available
209+
210+
"reload" |v:true| to force update ("--reload" flag of "deno
211+
cache") command.
212+
203213
*denops#server#start()*
204214
denops#server#start()
205215
Starts a denops server process and connects to the channel. It does
@@ -282,14 +292,14 @@ denops#server#wait_async({callback})
282292
callbacks registered are called in order of registration.
283293

284294
*denops#plugin#is_loaded()*
285-
denops#plugin#is_loaded({plugin})
286-
Returns 1 if a {plugin} plugin is already loaded. Otherwise, returns
295+
denops#plugin#is_loaded({name})
296+
Returns 1 if a {name} plugin is already loaded. Otherwise, returns
287297
0.
288298

289299
*denops#plugin#wait()*
290-
denops#plugin#wait({plugin}[, {options}])
291-
Waits synchronously until a {plugin} plugin is loaded. It returns
292-
immediately when the {plugin} plugin is already loaded.
300+
denops#plugin#wait({name}[, {options}])
301+
Waits synchronously until a {name} plugin is loaded. It returns
302+
immediately when the {name} plugin is already loaded.
293303
It returns -1 if it times out, -2 if the server is not yet ready or
294304
interrupted, or -3 if the plugin initialization failed.
295305
Developers need to consider this return value to decide whether to
@@ -305,15 +315,15 @@ denops#plugin#wait({plugin}[, {options}])
305315
Default: 0
306316

307317
*denops#plugin#wait_async()*
308-
denops#plugin#wait_async({plugin}, {callback})
309-
Waits asynchronously until a {plugin} plugin is loaded and invokes a
310-
{callback}. It invokes the {callback} immediately when the {plugin}
318+
denops#plugin#wait_async({name}, {callback})
319+
Waits asynchronously until a {name} plugin is loaded and invokes a
320+
{callback}. It invokes the {callback} immediately when the {name}
311321
plugin is already loaded. If this function is called multiple times
312-
for the same {plugin} plugin, callbacks registered for the plugin are
322+
for the same {name} plugin, callbacks registered for the plugin are
313323
called in order of registration.
314324

315325
*denops#plugin#register()*
316-
denops#plugin#register({plugin}[, {script}[, {options}]])
326+
denops#plugin#register({name}[, {script}[, {options}]])
317327
DEPRECATED: Use |denops#plugin#load()| instead.
318328

319329
Developers who would like to support previous denops versions should
@@ -344,7 +354,7 @@ denops#plugin#discover()
344354
by |denops#server#start()|.
345355

346356
*denops#plugin#load()*
347-
denops#plugin#load({plugin}, {script})
357+
denops#plugin#load({name}, {script})
348358
Loads a denops plugin. Use this function to load denops plugins that
349359
are not discovered by |denops#plugin#discover()|.
350360
It invokes |User| |DenopsPluginPre|:{plugin} just before denops
@@ -353,12 +363,12 @@ denops#plugin#load({plugin}, {script})
353363
function of the plugin.
354364

355365
*denops#plugin#reload()*
356-
denops#plugin#reload({plugin})
366+
denops#plugin#reload({name})
357367
Reloads a denops plugin.
358368

359369
*denops#plugin#check_type()*
360-
denops#plugin#check_type([{plugin}])
361-
Runs Deno's type check feature for {plugin} or all registered plugins.
370+
denops#plugin#check_type([{name}])
371+
Runs Deno's type check feature for {name} or all registered plugins.
362372
The result of the check will be displayed in |message-history|.
363373

364374
*denops#callback#register()*

0 commit comments

Comments
 (0)