From ad42e1b78d190efebf158f153db0ea65b25d334b Mon Sep 17 00:00:00 2001 From: Mark Woods Date: Wed, 20 Apr 2022 10:15:53 +0100 Subject: [PATCH] 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 #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. --- plugin/grepper.vim | 8 +++++++- test/feature/completion.vader | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/plugin/grepper.vim b/plugin/grepper.vim index 7931d64..1fc891a 100644 --- a/plugin/grepper.vim +++ b/plugin/grepper.vim @@ -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 : "")' diff --git a/test/feature/completion.vader b/test/feature/completion.vader index 583ced4..b8baefc 100644 --- a/test/feature/completion.vader +++ b/test/feature/completion.vader @@ -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('x ~', v:null, v:null), ['x '.$HOME] + +Execute (prompt: path, relative to $HOME): + let home = $HOME + let $HOME = getcwd() + AssertEqual grepper#complete('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/']