Refactor s:add function

This commit is contained in:
Andrea Cedraro 2014-07-27 14:31:28 +02:00
parent b651558c63
commit da7d6bd58b

113
plug.vim
View File

@ -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)
let s:extended[name] = g:plugs[name]
return
endif
if plugin[0] =~ '[/$~]' || plugin =~? '^[a-z]:' if !a:force && has_key(g:plugs, name)
let spec = extend(opts, { 'dir': s:dirpath(expand(plugin)) }) let s:extended[name] = g:plugs[name]
else return
if plugin =~ ':' endif
let uri = plugin
else if !a:force
if plugin !~ '/' let s:extended[name] = spec
let plugin = 'vim-scripts/'. plugin endif
let g:plugs[name] = spec
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 endif
let uri = 'https://git:@github.com/' . plugin . '.git' 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 endif
let dir = s:dirpath( fnamemodify(join([g:plug_home, name], '/'), ':p') ) let dir = s:dirpath( fnamemodify(join([g:plug_home, name], '/'), ':p') )
let spec = extend(opts, { 'dir': dir, 'uri': uri }) let properties = { 'dir': dir, 'uri': uri }
endif endif
return [name, properties]
endfunction
let g:plugs[name] = spec function! s:trim_ending_slash(str)
if !a:force return substitute(a:str, '[/\\]*$', '', '')
let s:extended[name] = spec endfunction
endif
let g:plugs_order += [name] 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(...)