Refactor s:add function
This commit is contained in:
parent
b651558c63
commit
da7d6bd58b
107
plug.vim
107
plug.vim
|
@ -289,55 +289,90 @@ function! s:lod_map(map, name, prefix)
|
||||||
call feedkeys(a:prefix . substitute(a:map, '^<Plug>', "\<Plug>", '') . extra)
|
call feedkeys(a:prefix . substitute(a:map, '^<Plug>', "\<Plug>", '') . extra)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:add(force, ...)
|
function! s:add(force, repo, ...)
|
||||||
let opts = { 'branch': 'master', 'frozen': 0 }
|
if a:0 > 1
|
||||||
if a:0 == 1
|
|
||||||
let plugin = a:1
|
|
||||||
elseif a:0 == 2
|
|
||||||
let plugin = a:1
|
|
||||||
if type(a:2) == s:TYPE.string
|
|
||||||
let opts.branch = a:2
|
|
||||||
elseif type(a:2) == s:TYPE.dict
|
|
||||||
call extend(opts, a:2)
|
|
||||||
if has_key(opts, 'tag')
|
|
||||||
let opts.branch = remove(opts, 'tag')
|
|
||||||
endif
|
|
||||||
else
|
|
||||||
echoerr "Invalid argument type (expected: string or dictionary)"
|
|
||||||
return
|
|
||||||
endif
|
|
||||||
else
|
|
||||||
echoerr "Invalid number of arguments (1..2)"
|
echoerr "Invalid number of arguments (1..2)"
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let plugin = substitute(plugin, '[/\\]*$', '', '')
|
try
|
||||||
let name = substitute(split(plugin, '/')[-1], '\.git$', '', '')
|
let [name, spec] = s:build_plug_spec(a:repo, a:000)
|
||||||
|
|
||||||
if !a:force && has_key(g:plugs, name)
|
if !a:force && has_key(g:plugs, name)
|
||||||
let s:extended[name] = g:plugs[name]
|
let s:extended[name] = g:plugs[name]
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if plugin[0] =~ '[/$~]' || plugin =~? '^[a-z]:'
|
|
||||||
let spec = extend(opts, { 'dir': s:dirpath(expand(plugin)) })
|
|
||||||
else
|
|
||||||
if plugin =~ ':'
|
|
||||||
let uri = plugin
|
|
||||||
else
|
|
||||||
if plugin !~ '/'
|
|
||||||
let plugin = 'vim-scripts/'. plugin
|
|
||||||
endif
|
|
||||||
let uri = 'https://git:@github.com/' . plugin . '.git'
|
|
||||||
endif
|
|
||||||
let dir = s:dirpath( fnamemodify(join([g:plug_home, name], '/'), ':p') )
|
|
||||||
let spec = extend(opts, { 'dir': dir, 'uri': uri })
|
|
||||||
endif
|
|
||||||
|
|
||||||
let g:plugs[name] = spec
|
|
||||||
if !a:force
|
if !a:force
|
||||||
let s:extended[name] = spec
|
let s:extended[name] = spec
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
let g:plugs[name] = spec
|
||||||
let g:plugs_order += [name]
|
let g:plugs_order += [name]
|
||||||
|
catch
|
||||||
|
echoerr v:exception
|
||||||
|
return
|
||||||
|
endtry
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:build_plug_spec(repo, opts)
|
||||||
|
try
|
||||||
|
let [name, properties] = s:infer_properties(a:repo)
|
||||||
|
let spec = extend(s:parse_options(a:opts), properties)
|
||||||
|
return [name, spec]
|
||||||
|
catch
|
||||||
|
throw v:exception
|
||||||
|
endtry
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:parse_options(args)
|
||||||
|
let opts = { 'branch': 'master', 'frozen': 0 }
|
||||||
|
if !empty(a:args)
|
||||||
|
let arg = a:args[0]
|
||||||
|
if type(arg) == s:TYPE.string
|
||||||
|
let opts.branch = arg
|
||||||
|
elseif type(arg) == s:TYPE.dict
|
||||||
|
call extend(opts, arg)
|
||||||
|
if has_key(opts, 'tag')
|
||||||
|
let opts.branch = remove(opts, 'tag')
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
throw "Invalid argument type (expected: string or dictionary)"
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
return opts
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:infer_properties(repo)
|
||||||
|
let repo = s:trim_ending_slash(a:repo)
|
||||||
|
let name = s:extract_name(repo)
|
||||||
|
if s:is_local_plug(repo)
|
||||||
|
let properties = { 'dir': s:dirpath(expand(repo)) }
|
||||||
|
else
|
||||||
|
if repo =~ ':'
|
||||||
|
let uri = repo
|
||||||
|
else
|
||||||
|
if repo !~ '/'
|
||||||
|
let repo = 'vim-scripts/'. repo
|
||||||
|
endif
|
||||||
|
let uri = 'https://git:@github.com/' . repo . '.git'
|
||||||
|
endif
|
||||||
|
let dir = s:dirpath( fnamemodify(join([g:plug_home, name], '/'), ':p') )
|
||||||
|
let properties = { 'dir': dir, 'uri': uri }
|
||||||
|
endif
|
||||||
|
return [name, properties]
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:trim_ending_slash(str)
|
||||||
|
return substitute(a:str, '[/\\]*$', '', '')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:extract_name(repo)
|
||||||
|
return substitute(split(a:repo, '/')[-1], '\.git$', '', '')
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! s:is_local_plug(repo)
|
||||||
|
return a:repo[0] =~ '[/$~]' || a:repo =~? '^[a-z]:'
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:install(...)
|
function! s:install(...)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user