1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-02 20:30:05 +08:00

feat(api): improve SpaceVim#api#register function

This commit is contained in:
wsdjeg 2022-03-29 22:08:13 +08:00
parent b53538deda
commit 8e7ffefc11
2 changed files with 45 additions and 6 deletions

View File

@ -32,9 +32,8 @@ let s:apis = {}
""
"@public
"Import API base the given {name}, and return the API object. for all
"available APIs please check |spacevim-api|
" Import API base the given {name}, and return the API object. for all
" available APIs please check |spacevim-api|
function! SpaceVim#api#import(name) abort
if has_key(s:apis, a:name)
return deepcopy(s:apis[a:name])
@ -48,11 +47,33 @@ function! SpaceVim#api#import(name) abort
return p
endfunction
""
" Register new {api} named {name}, 1 will be returned if succeed. for all
" available APIs please check |spacevim-api|
"
" Example:
" >
" let s:hello = {}
" function! s:hello.say(who)
" echo 'hello! ' . a:who
" endfunction
" call SpaceVim#api#register('hello', s:hello)
"
" " after registering api, you can use this api
" " in other vim scripts like this:
" let s:H = SpaceVim#api#import('hello')
" call s:H.say('wsdjeg')
" <
function! SpaceVim#api#register(name, api) abort
if !empty(SpaceVim#api#import(a:name))
echoerr '[SpaceVim api] Api : ' . a:name . ' already existed!'
" the name must be string
if type(a:name) ==# type('') && strlen(a:name) >= 1
if !empty(SpaceVim#api#import(a:name))
call SpaceVim#logger#warn('API:' . a:name . ' already existed!')
else
let s:apis[a:name] = deepcopy(a:api)
endif
else
let s:apis[a:name] = deepcopy(a:api)
call SpaceVim#logger#warn('failed to register api, the name must be a no empty string!')
endif
endfunction

View File

@ -1472,6 +1472,24 @@ SpaceVim#api#import({name}) *SpaceVim#api#import()*
Import API base the given {name}, and return the API object. for all
available APIs please check |spacevim-api|
SpaceVim#api#register({name}, {api}) *SpaceVim#api#register()*
Register new {api} named {name}, 1 will be returned if succeed. for all
available APIs please check |spacevim-api|
Example:
>
let s:hello = {}
function! s:hello.say(who)
echo 'hello! ' . a:who
endfunction
call SpaceVim#api#register('hello', s:hello)
" after registering api, you can use this api
" in other vim scripts like this:
let s:H = SpaceVim#api#import('hello')
call s:H.say('wsdjeg')
<
SpaceVim#custom#SPC({m}, {keys}, {cmd}, {desc}, {is_cmd})
*SpaceVim#custom#SPC()*
The first parameter sets the type of shortcut key, which can be `nnoremap`