Skip to content

Commit fe902c8

Browse files
committed
💪 Refactor plugin collect/find mech.
1 parent 495ded7 commit fe902c8

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

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/plugin.vim

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function! denops#plugin#register(name, ...) abort
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:name)
66+
let l:script = denops#_internal#plugin#find(a:name).script
6767
else
6868
let l:script = a:1
6969
endif
@@ -84,21 +84,19 @@ function! denops#plugin#reload(name) abort
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:name, l:script] in items(l:plugins)
91-
call denops#plugin#load(l:name, 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,27 +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:find_plugin(name) abort
126-
for l:script in globpath(&runtimepath, denops#_internal#path#join(['denops', a:name, 'main.ts']), 1, 1, 1)
127-
let l:name = fnamemodify(l:script, ':h:t')
128-
if l:name[:0] ==# '@' || !filereadable(l:script)
129-
continue
130-
endif
131-
return l:script
132-
endfor
133-
throw printf('No denops plugin for "%s" exists', a:name)
134-
endfunction
135-
136113
function! s:relay_autocmd(name) abort
137114
let l:plugin = matchstr(expand('<amatch>'), '^[^:]\+:\zs.*')
138115
execute printf('doautocmd <nomodeline> User %s:%s', a:name, l:plugin)

0 commit comments

Comments
 (0)