Skip to content

Commit b6706ae

Browse files
committed
Support tilde expansion when completing files
- 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.
1 parent c7857e0 commit b6706ae

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

plugin/grepper.vim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,14 @@ endfunction
235235
" grepper#complete_files() {{{2
236236
function! grepper#complete_files(lead, _line, _pos)
237237
let [head, path] = s:extract_path(a:lead)
238+
" tilde expansion to $HOME
239+
if path ==# '~'
240+
return [head . $HOME]
241+
" handle paths in $HOME (~/foo)
242+
elseif path[0:1] ==# '~/'
243+
return map(split(globpath($HOME, path[2:].'*'), '\n'), 'head . "~" . v:val['.len($HOME).':] . (isdirectory(v:val) ? s:slash : "")')
238244
" handle relative paths
239-
if empty(path) || (path =~ '\s$') || (path =~ '^\s*\w\+')
245+
elseif empty(path) || (path =~ '\s$') || (path =~ '^\s*\w\+')
240246
return map(
241247
\ map(split(globpath('.'.s:slash, path.'*'), '\n'), "substitute(v:val, '^\\s*.'.s:slash, '', '')"),
242248
\ 'head . v:val . (isdirectory(v:val) ? s:slash : "")'

test/feature/completion.vader

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ Execute (command: flags, -tool options):
3737
Assert len(grepper#complete('', 'Grepper -tool ', v:null)) > 1
3838
AssertEqual count(grepper#complete('', 'Grepper -tool ', v:null), 'grep '), 1
3939

40+
Execute (prompt: path, tilde expansion):
41+
AssertEqual grepper#complete_files('x ~', v:null, v:null), ['x '.$HOME]
42+
43+
Execute (prompt: path, relative to $HOME):
44+
let home = $HOME
45+
let $HOME = getcwd()
46+
AssertEqual grepper#complete_files('x ~/f', v:null, v:null), ['x ~/foo/']
47+
let $HOME = home
48+
4049
Execute (prompt: relative path, empty string):
4150
AssertEqual grepper#complete_files('x ', v:null, v:null), ['x foo/']
4251

0 commit comments

Comments
 (0)