Skip to content

Commit d4e9ff0

Browse files
committed
vitalizer: External module is now available!
1 parent 95ad5fe commit d4e9ff0

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

autoload/vitalizer.vim

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,29 @@ function! s:search_dependence(modules)
100100
return sort(keys(all))
101101
endfunction
102102

103+
function! s:is_camel_case(str)
104+
return !empty(matchstr(a:str, '^\%([0-9A-Z]\l*\)\+$'))
105+
endfunction
106+
107+
function! s:is_module_name(str)
108+
return s:L.and(map(split(a:str, '\.'), 's:is_camel_case(v:val)'))
109+
endfunction
110+
103111
function! s:module2file(name)
104112
let target = a:name ==# '' ? '' : '/' . substitute(a:name, '\W\+', '/', 'g')
105113
return printf('autoload/vital/__latest__%s.vim', target)
106114
endfunction
107115

108-
function! s:camelize(str)
109-
return substitute(a:str, '\%(^\|_\)\(\l\)', '\u\1', 'g')
110-
endfunction
111-
112116
function! s:file2module(file)
113-
let tail = matchstr(a:file, 'autoload/vital/_\w\+/\zs.*\ze\.vim$')
114-
return join(map(split(tail, '[\\/]\+'), 's:camelize(v:val)'), '.')
117+
let filename = s:FP.unify_separator(a:file)
118+
let tail = matchstr(filename, 'autoload/vital/_\w\+/\zs.*\ze\.vim$')
119+
return join(split(tail, '[\\/]\+'), '.')
115120
endfunction
116121

117-
function! s:all_modules()
118-
let pat = '^.*\zs\<autoload/vital/.*'
119-
return filter(map(split(glob(
120-
\ g:vitalizer#vital_dir . '/autoload/vital/**/*.vim', 1), "\n"),
121-
\ 'matchstr(s:FP.unify_separator(v:val), pat)'), 'v:val!=""')
122+
function! s:available_module_names()
123+
return sort(s:L.uniq(filter(map(split(globpath(&runtimepath,
124+
\ 'autoload/vital/__latest__/**/*.vim', 1), "\n"),
125+
\ 's:file2module(v:val)'), 's:is_module_name(v:val)')))
122126
endfunction
123127

124128
function! s:get_changes()
@@ -240,8 +244,8 @@ function! vitalizer#vitalize(name, to, modules, hash)
240244
" Check if all of specified modules exist.
241245
let missing = copy(a:modules)
242246
call map(missing, 'substitute(v:val, "^[+-]", "", "")')
243-
let all_modules = s:all_modules()
244-
call filter(missing, 'index(all_modules, s:module2file(v:val)) is -1')
247+
let all_modules = s:available_module_names()
248+
call filter(missing, 'index(all_modules, v:val) is -1')
245249
if !empty(missing)
246250
throw "vitalizer: Some modules don't exist: " . join(missing, ', ')
247251
endif
@@ -282,15 +286,25 @@ function! vitalizer#vitalize(name, to, modules, hash)
282286
endif
283287
endif
284288

289+
" List and check the installing files.
290+
let install_files = []
291+
for f in files + s:REQUIRED_FILES
292+
let after = substitute(f, '__latest__', '_' . a:name, '')
293+
let paths = globpath(g:vitalizer#vital_dir . ',' . &runtimepath, f, 1)
294+
let from = get(split(paths, "\n"), 0)
295+
if !filereadable(from)
296+
throw 'vitalizer: Can not read the installing file: ' . file
297+
endif
298+
call add(install_files, [from, s:FP.join(a:to, after)])
299+
endfor
300+
285301
" Remove previous vital.
286302
call s:uninstall(a:to)
287303

288304
" Install vital.
289305
let short_hash = hash[: s:HASH_SIZE]
290-
for f in files + s:REQUIRED_FILES
291-
let after = substitute(f, '__latest__', '_' . a:name, '')
292-
call s:copy(s:FP.join(g:vitalizer#vital_dir, f),
293-
\ s:FP.join(a:to, after))
306+
for [from, to] in install_files
307+
call s:copy(from, to)
294308
endfor
295309
let content = [a:name, short_hash, ''] + installing_modules
296310
call writefile(content, vital_file_name)
@@ -315,7 +329,7 @@ function! vitalizer#complete(arglead, cmdline, cursorpos)
315329
return filter(options, 'stridx(v:val, a:arglead)!=-1')
316330
elseif len(args) > 2 || (len(args) == 2 && a:cmdline =~# '\s$')
317331
let prefix = a:arglead =~# '^[+-]' ? a:arglead[0] : ''
318-
return filter(map(s:all_modules(), 'prefix . s:file2module(v:val)'),
332+
return filter(map(s:available_module_names(), 'prefix . v:val'),
319333
\ 'stridx(v:val, a:arglead)!=-1')
320334
else
321335
return map(filter(split(glob(a:arglead . "*", 1), "\n"),

0 commit comments

Comments
 (0)