Skip to content

Instantly share code, notes, and snippets.

@tyru
Last active July 2, 2019 17:02
Show Gist options
  • Save tyru/6d2999418ac5f5b7d5cdb960032d6009 to your computer and use it in GitHub Desktop.
Save tyru/6d2999418ac5f5b7d5cdb960032d6009 to your computer and use it in GitHub Desktop.
require() function for deeply nested very loooooooooong named autoload function
let s:L = vital#of('vital').import('Data.List')
function! s:require(ns, ...) abort
let ns = strtrans(a:ns)
let fnlist = map(
\ split(execute('function /' . ns), '\n'),
\ {_,l -> matchstr(l, '^function ' . ns . '#\zs\w\+\ze(')}
\)
call filter(fnlist, '!empty(v:val)')
if a:0
let nofuncs = filter(copy(a:1), {_,fn -> index(fnlist, fn) is -1})
if !empty(nofuncs)
" TODO: show multiple func name at once?
throw printf('Exxx: Unknown function in %s: %s', ns, nofuncs[0])
endif
return map(fnlist, {_,fn -> function(ns . '#' . fn)})
endif
let d = {}
for fn in fnlist
let d[fn] = function(ns . '#' . fn)
endfor
return d
endfunction
let v:errors = []
" vital#of('vital').import('Data.List').pop([1,2,3])
call assert_equal(function('vital#of'), s:require('vital').of)
call assert_equal(3, s:require('vital').of('vital').import('Data.List').pop([1,2,3]))
call assert_equal([function('vital#of')], s:require('vital', ['of']))
call assert_equal(3, s:require('vital', ['of'])[0]('vital').import('Data.List').pop([1,2,3]))
try
call s:require('vital', ['xxx'])
call assert_true(0, 'Exxx was not thrown')
catch
call assert_exception('Exxx:')
endtry
echohl ErrorMsg
for s:_ in v:errors
echo s:_
endfor
echohl None
@tyru
Copy link
Author

tyru commented Jul 2, 2019

あ、すみません。
冒頭で vital import してますがその後使ってないです (issue にあげるために依存切った)。

簡易化いいですね。
というか今気づきましたが自分のバージョンだとスクリプトがロードされてない場合に対処できてないですね。
あざます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment