From 410e921979424a339504aa71bbcc0ef6e2a9b396 Mon Sep 17 00:00:00 2001 From: Falko Schmidt Date: Tue, 27 Mar 2018 16:17:21 +0200 Subject: [PATCH 1/3] Use guessProjectRoot from rking/ag.vim. --- plugin/side-search.vim | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/plugin/side-search.vim b/plugin/side-search.vim index 563ae49..2d953ad 100644 --- a/plugin/side-search.vim +++ b/plugin/side-search.vim @@ -151,6 +151,24 @@ function! SideSearchWinnr() return s:my_buffer_winnr() endfunction +function! s:guessProjectRoot() + let l:splitsearchdir = split(getcwd(), "/") + + while len(l:splitsearchdir) > 2 + let l:searchdir = '/'.join(l:splitsearchdir, '/').'/' + for l:marker in ['.rootdir', '.git', '.hg', '.svn', 'bzr', '_darcs', 'build.xml'] + " found it! Return the dir + if filereadable(l:searchdir.l:marker) || isdirectory(l:searchdir.l:marker) + return l:searchdir + endif + endfor + let l:splitsearchdir = l:splitsearchdir[0:-2] " Splice the list to get rid of the tail directory + endwhile + + " Nothing found, fallback to current working dir + return getcwd() +endfunction + " The public facing function. " Accept 1 or 2 arguments which basically get passed directly " to the `ag` command. @@ -173,8 +191,10 @@ function! SideSearch(args) abort call s:append_guide() + " determine root directory + let l:cwd = s:guessProjectRoot() " execute showing summary of stuff read (without silent) - let b:cmd = g:side_search_prg . ' ' . a:args + let b:cmd = g:side_search_prg . ' ' . a:args . ' ' . l:cwd " Thanks: https://github.com/rking/ag.vim/blob/master/autoload/ag.vim#L154 let query = matchstr(a:args, "\\v(-)\@ Date: Tue, 27 Mar 2018 16:49:38 +0200 Subject: [PATCH 2/3] Use relative directory. --- plugin/side-search.vim | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/plugin/side-search.vim b/plugin/side-search.vim index 2d953ad..1f96a32 100644 --- a/plugin/side-search.vim +++ b/plugin/side-search.vim @@ -153,16 +153,19 @@ endfunction function! s:guessProjectRoot() let l:splitsearchdir = split(getcwd(), "/") + let l:searchdir = '' while len(l:splitsearchdir) > 2 - let l:searchdir = '/'.join(l:splitsearchdir, '/').'/' for l:marker in ['.rootdir', '.git', '.hg', '.svn', 'bzr', '_darcs', 'build.xml'] + let l:dir = l:searchdir.l:marker " found it! Return the dir - if filereadable(l:searchdir.l:marker) || isdirectory(l:searchdir.l:marker) + if filereadable(l:dir) || isdirectory(l:dir) return l:searchdir endif endfor - let l:splitsearchdir = l:splitsearchdir[0:-2] " Splice the list to get rid of the tail directory + " Splice the list to get rid of the tail directory + let l:splitsearchdir = l:splitsearchdir[0:-2] + let l:searchdir = '../'.l:searchdir endwhile " Nothing found, fallback to current working dir From 589a5f44db2fcf5a050a01905d92b8027b72c52a Mon Sep 17 00:00:00 2001 From: Falko Schmidt Date: Tue, 27 Mar 2018 17:10:11 +0200 Subject: [PATCH 3/3] Refactor function. --- plugin/side-search.vim | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/plugin/side-search.vim b/plugin/side-search.vim index 1f96a32..6b4f7a7 100644 --- a/plugin/side-search.vim +++ b/plugin/side-search.vim @@ -152,24 +152,21 @@ function! SideSearchWinnr() endfunction function! s:guessProjectRoot() - let l:splitsearchdir = split(getcwd(), "/") + let l:cwd = getcwd() + let l:maxdistance = len(split(l:cwd, '/')) - 2 let l:searchdir = '' - while len(l:splitsearchdir) > 2 + while len(split(l:searchdir, '/')) < l:maxdistance for l:marker in ['.rootdir', '.git', '.hg', '.svn', 'bzr', '_darcs', 'build.xml'] let l:dir = l:searchdir.l:marker - " found it! Return the dir if filereadable(l:dir) || isdirectory(l:dir) return l:searchdir endif endfor - " Splice the list to get rid of the tail directory - let l:splitsearchdir = l:splitsearchdir[0:-2] let l:searchdir = '../'.l:searchdir endwhile - " Nothing found, fallback to current working dir - return getcwd() + return l:cwd endfunction " The public facing function.