Skip to content

Commit

Permalink
Support tilde expansion when completing files
Browse files Browse the repository at this point in the history
- Expand tilde alone to $HOME (same as vim's built in path completion)
- Support completion of paths relative to ~/ (taken from PR mhinz#152)

This is getting quite messy now, but that's ok, we have some tests!
Refactoring can come later, maybe when supporting hidden files/dirs.
  • Loading branch information
mmrwoods committed May 6, 2022
1 parent c7857e0 commit b6706ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion plugin/grepper.vim
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,14 @@ endfunction
" grepper#complete_files() {{{2
function! grepper#complete_files(lead, _line, _pos)
let [head, path] = s:extract_path(a:lead)
" tilde expansion to $HOME
if path ==# '~'
return [head . $HOME]
" handle paths in $HOME (~/foo)
elseif path[0:1] ==# '~/'
return map(split(globpath($HOME, path[2:].'*'), '\n'), 'head . "~" . v:val['.len($HOME).':] . (isdirectory(v:val) ? s:slash : "")')
" handle relative paths
if empty(path) || (path =~ '\s$') || (path =~ '^\s*\w\+')
elseif empty(path) || (path =~ '\s$') || (path =~ '^\s*\w\+')
return map(
\ map(split(globpath('.'.s:slash, path.'*'), '\n'), "substitute(v:val, '^\\s*.'.s:slash, '', '')"),
\ 'head . v:val . (isdirectory(v:val) ? s:slash : "")'
Expand Down
9 changes: 9 additions & 0 deletions test/feature/completion.vader
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ Execute (command: flags, -tool options):
Assert len(grepper#complete('', 'Grepper -tool ', v:null)) > 1
AssertEqual count(grepper#complete('', 'Grepper -tool ', v:null), 'grep '), 1

Execute (prompt: path, tilde expansion):
AssertEqual grepper#complete_files('x ~', v:null, v:null), ['x '.$HOME]

Execute (prompt: path, relative to $HOME):
let home = $HOME
let $HOME = getcwd()
AssertEqual grepper#complete_files('x ~/f', v:null, v:null), ['x ~/foo/']
let $HOME = home

Execute (prompt: relative path, empty string):
AssertEqual grepper#complete_files('x ', v:null, v:null), ['x foo/']

Expand Down

0 comments on commit b6706ae

Please sign in to comment.