Skip to content

Commit

Permalink
Add support for --input-file completions with spaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
dstein64 committed Feb 16, 2024
1 parent ad414f2 commit 791804e
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions autoload/startuptime.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1431,36 +1431,50 @@ endfunction
function! startuptime#CompleteOptions(arglead, cmdline, cursorpos) abort
let l:args = []
let l:cmdline = a:cmdline[:a:cursorpos - 1]
" WARN: The filename completion does not properly handle filenames with
" spaces. The two issues are:
" 1. The arglead that -complete-func bases its completion on includes no
" spaces.
" 2. Even when backslash escaping spaces in the items returned by this
" function, the spaces are completed without being backslash escaped.
let l:lead = trim(substitute(a:cmdline, a:arglead . '$', '', ''), ' ', 2)
if l:lead =~# '--input-file$'
let l:matches = glob(a:arglead .. '*', v:true, v:true)
call extend(l:args, l:matches)
elseif l:lead =~# '--tries$'
for l:digit in range(10)
let l:candidate = a:arglead .. l:digit
if str2nr(l:candidate) !=# 0
call add(l:args, l:candidate)
endif
endfor
else
call extend(l:args, [
\ '--help',
\ '--hidden',
\ '--input-file',
\ '--other-events', '--no-other-events',
\ '--save',
\ '--sourced', '--no-sourced',
\ '--sort', '--no-sort',
\ '--sourcing-events', '--no-sourcing-events',
\ '--tries',
\ '--',
\ ])
" First try to complete --input-file argument, with handling for paths with
" spaces.
let l:idx = stridx(l:cmdline, '--input-file')
if l:idx !=# -1
let l:offset = len('--input-file')
let l:str = l:cmdline[l:idx + l:offset:]
let l:str = substitute(l:str, '^ *', '', '')
" Don't proceed if there are spaces not preceded by backslash.
if l:str !~# '\\\@<! '
let l:str = substitute(l:str, '\\ ', ' ', 'g')
let l:matches = glob(l:str .. '*', v:true, v:true)
call map(l:matches, {_, x -> substitute(x, ' ', '\\ ', 'g')})
call extend(l:args, l:matches)
endif
endif
" If we couldn't complete an --input-file argument, try the other
" completions.
if empty(l:args)
let l:lead = trim(substitute(a:cmdline, a:arglead . '$', '', ''), ' ', 2)
if l:lead =~# '--tries$'
for l:digit in range(10)
let l:candidate = a:arglead .. l:digit
if str2nr(l:candidate) !=# 0
call add(l:args, l:candidate)
endif
endfor
elseif l:lead =~# '--input-file$'
" The handling above didn't have any completion. An argument is
" necessary for --input-file, so don't give the completions for other
" options.
else
call extend(l:args, [
\ '--help',
\ '--hidden',
\ '--input-file',
\ '--other-events', '--no-other-events',
\ '--save',
\ '--sourced', '--no-sourced',
\ '--sort', '--no-sort',
\ '--sourcing-events', '--no-sourcing-events',
\ '--tries',
\ '--',
\ ])
endif
endif
call sort(l:args)
return join(l:args, "\n")
Expand Down

0 comments on commit 791804e

Please sign in to comment.