Improve flexibility of public API argument order

This allows tomfoolery like FugitiveConfig(dir)->FugitiveConfigGet(key).
I'm not sure I want to officially endorse this usage, but if nothing
else it makes interactive debugging a bit more fluid.
This commit is contained in:
Tim Pope
2021-08-07 15:31:22 -04:00
parent f58ac20359
commit b8ba07f7d8
2 changed files with 50 additions and 16 deletions

View File

@@ -420,11 +420,16 @@ function! fugitive#PrepareDirEnvGitArgv(...) abort
throw 'fugitive: Git 1.8.5 or higher required'
endif
let git = s:GitCmd()
if a:0 && type(a:1) ==# type([])
let cmd = a:000[1:-1] + a:1
else
let cmd = copy(a:000)
endif
let list_args = []
let cmd = []
for arg in a:000
if type(arg) ==# type([])
call extend(list_args, arg)
else
call add(cmd, arg)
endif
endfor
call extend(cmd, list_args)
let env = {}
let i = 0
while i < len(cmd)
@@ -675,7 +680,7 @@ function! fugitive#Config(...) abort
let name = ''
let default = get(a:, 3, '')
if a:0 >= 2 && type(a:2) == type({}) && has_key(a:2, 'GetAll')
return fugitive#ConfigGetAll(a:1, a:2)
return get(fugitive#ConfigGetAll(a:1, a:2), 0, default)
elseif a:0 >= 2
let dir = s:Dir(a:2)
let name = a:1
@@ -718,20 +723,32 @@ function! fugitive#Config(...) abort
endfunction
function! fugitive#ConfigGetAll(name, ...) abort
let config = fugitive#Config(a:0 ? a:1 : s:Dir())
let name = substitute(a:name, '^[^.]\+\|[^.]\+$', '\L&', 'g')
if type(a:name) !=# type('') && a:0
let config = fugitive#Config(a:0)
let name = a:1
else
let config = fugitive#Config(a:0 ? a:1 : s:Dir())
let name = a:name
endif
let name = substitute(name, '^[^.]\+\|[^.]\+$', '\L&', 'g')
return copy(get(config, name, []))
endfunction
function! fugitive#ConfigGetRegexp(pattern, ...) abort
let config = fugitive#Config(a:0 ? a:1 : s:Dir())
let filtered = map(filter(copy(config), 'v:key =~# "\\." && v:key =~# a:pattern'), 'copy(v:val)')
if a:pattern !~# '\\\@<!\%(\\\\\)*\\z[se]'
if type(a:pattern) !=# type('') && a:0
let config = fugitive#Config(a:0)
let pattern = a:1
else
let config = fugitive#Config(a:0 ? a:1 : s:Dir())
let pattern = a:pattern
endif
let filtered = map(filter(copy(config), 'v:key =~# "\\." && v:key =~# pattern'), 'copy(v:val)')
if pattern !~# '\\\@<!\%(\\\\\)*\\z[se]'
return filtered
endif
let transformed = {}
for [k, v] in items(filtered)
let k = matchstr(k, a:pattern)
let k = matchstr(k, pattern)
if len(k)
let transformed[k] = v
endif

View File

@@ -72,11 +72,19 @@ endfunction
" An optional second argument provides the Git dir, or the buffer number of a
" buffer with a Git dir. The default is the current buffer.
function! FugitiveFind(...) abort
return fugitive#Find(a:0 ? a:1 : bufnr(''), FugitiveGitDir(a:0 > 1 ? a:2 : -1))
if a:0 && type(a:1) ==# type({})
return call('fugitive#Find', a:000[1:-1] + [FugitiveGitDir(a:1)])
else
return fugitive#Find(a:0 ? a:1 : bufnr(''), FugitiveGitDir(a:0 > 1 ? a:2 : -1))
endif
endfunction
function! FugitivePath(...) abort
if a:0 > 1
if a:0 > 2 && type(a:1) ==# type({})
return fugitive#Path(a:2, a:3, FugitiveGitDir(a:1))
elseif a:0 && type(a:1) ==# type({})
return FugitiveReal(a:0 > 1 ? a:2 : @%)
elseif a:0 > 1
return fugitive#Path(a:1, a:2, FugitiveGitDir(a:0 > 2 ? a:3 : -1))
else
return FugitiveReal(a:0 ? a:1 : @%)
@@ -182,11 +190,20 @@ endfunction
" An optional second argument provides the Git dir, or the buffer number of a
" buffer with a Git dir. The default is the current buffer.
function! FugitiveHead(...) abort
let dir = FugitiveGitDir(a:0 > 1 ? a:2 : -1)
if a:0 && type(a:1) ==# type({})
let dir = FugitiveGitDir(a:1)
let arg = get(a:, 2, 0)
elseif a:0 > 1
let dir = FugitiveGitDir(a:2)
let arg = a:1
else
let dir = FugitiveGitDir()
let args = get(a:, 1, 0)
endif
if empty(dir)
return ''
endif
return fugitive#Head(a:0 ? a:1 : 0, dir)
return fugitive#Head(arg, dir)
endfunction
function! FugitiveStatusline(...) abort