Skip to content

Commit 916d57b

Browse files
committed
Cleanup implementation of the script
This is a partial commit of a larger piece of work that actually introduced problems. I'm going to submit this piece and then try to figure out what part of the remainder is causing the tests to fail.
1 parent 572b582 commit 916d57b

File tree

1 file changed

+56
-43
lines changed

1 file changed

+56
-43
lines changed

plugin/EasyGrep.vim

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ function! s:GetBufferDirsList()
147147
endif
148148
let dirs[d]=1
149149
endfor
150-
return keys(dirs)
150+
" Note that this returns a unique set of directories
151+
return sort(keys(dirs))
151152
endfunction
152153
" }}}
153154
" GetVisibleBuffers {{{
@@ -670,13 +671,13 @@ endfunction
670671
" }}}
671672
" AddAdditionalLocationsToFileTargetList {{{
672673
function! s:AddAdditionalLocationsToFileTargetList(lst)
673-
if empty(a:lst) || s:IsModeBuffers()
674-
return a:lst
674+
let lst = a:lst
675+
if empty(lst) || s:IsModeBuffers()
676+
return lst
675677
endif
676678

677-
let lst = a:lst
678679
if g:EasyGrepSearchCurrentBufferDir && s:IsBufferDirSearchAllowed()
679-
let lst = s:AddBufferDirsToFileTargetList(lst)
680+
let lst = s:ApplySearchDirectoriesToFileTargetList(lst)
680681
endif
681682

682683
if g:EasyGrepHidden
@@ -718,14 +719,14 @@ function! s:AddAdditionalLocationsToFileTargetList(lst)
718719
return newlst
719720
endfunction
720721
"}}}
721-
" AddBufferDirsToFileTargetList {{{
722-
function! s:AddBufferDirsToFileTargetList(lst)
723-
let lst = a:lst
722+
" ApplySearchDirectoriesToFileTargetList {{{
723+
function! s:ApplySearchDirectoriesToFileTargetList(fileTargets)
724+
let fileTargets = a:fileTargets
724725

725726
" Build a list of the directories in buffers
726-
let dirs = sort(s:GetBufferDirsList())
727+
let dirs = s:GetBufferDirsList()
727728

728-
let newlst = copy(lst)
729+
let newlst = copy(fileTargets)
729730

730731
let root = s:GetGrepRoot()
731732
let currDir = s:GetCwdEscaped()
@@ -747,7 +748,7 @@ function! s:AddBufferDirsToFileTargetList(lst)
747748
endif
748749
if addToList
749750
call add(accepteddirs, dir)
750-
for p in lst
751+
for p in fileTargets
751752
call add(newlst, dir."/".p)
752753
endfor
753754
endif
@@ -785,18 +786,29 @@ function! s:IsRecursivelyReachable(fromthisdir, target)
785786
return 1
786787
endfunction
787788
" }}}
788-
" GetRecursiveMinimalSetList {{{
789-
function! s:GetRecursiveMinimalSetList()
789+
" GetDirectorySearchList {{{
790+
function! s:GetDirectorySearchList()
791+
if !g:EasyGrepSearchCurrentBufferDir
792+
return [ s:GetGrepRoot() ]
793+
endif
790794

795+
let root = s:GetGrepRoot()
791796
let currDir = s:GetCwdEscaped()
792-
let bufferdirs = sort(s:GetBufferDirsList())
793-
let bufferSetList = [ s:GetGrepRoot() ]
797+
let bufferDirs = s:GetBufferDirsList()
794798

795-
for dir in bufferdirs
799+
call add(bufferDirs, root)
800+
let bufferDirsWithRoot = sort(bufferDirs)
801+
802+
let bufferSetList = []
803+
804+
let i = 0
805+
let end = len(bufferDirsWithRoot)
806+
while i < end
807+
let dir = bufferDirsWithRoot[i]
796808
let addToList = 1
797-
if dir == currDir || dir == '.'
809+
if (i > 0) && (dir == bufferDirsWithRoot[i-1])
798810
let addToList = 0
799-
else
811+
elseif s:IsRecursiveSearch()
800812
for d in bufferSetList
801813
if s:IsRecursivelyReachable(d, dir)
802814
let addToList = 0
@@ -807,24 +819,22 @@ function! s:GetRecursiveMinimalSetList()
807819
if addToList
808820
call add(bufferSetList, s:FileEscape(dir))
809821
endif
810-
endfor
822+
let i += 1
823+
endwhile
811824

812-
return bufferSetList
813-
endfunction
814-
" }}}
815-
" GetDirectorySearchList {{{
816-
function! s:GetDirectorySearchList()
817-
if g:EasyGrepSearchCurrentBufferDir
818-
if s:IsRecursiveSearch()
819-
return s:GetRecursiveMinimalSetList()
820-
else
821-
let lst = [s:GetGrepRoot()]
822-
call s:AddBufferDirsToFileTargetList(lst)
823-
return lst
825+
" Place the root as the first item if possible
826+
let i = 0
827+
let end = len(bufferSetList)
828+
while i < end
829+
if bufferSetList[i] == root
830+
call remove(bufferSetList, i)
831+
call insert(bufferSetList, root, 0)
832+
break
824833
endif
825-
else
826-
return [ s:GetGrepRoot() ]
827-
endif
834+
let i += 1
835+
endwhile
836+
837+
return bufferSetList
828838
endfunction
829839
" }}}
830840
" CheckIfCurrentFileIsSearched {{{
@@ -1041,27 +1051,30 @@ function! <sid>EchoGrepCommand()
10411051
return 0
10421052
endif
10431053

1044-
let placeholder = "<pattern>"
1045-
let dirAnnotation = "Grep Root: "
1046-
let grepCommand = s:GetGrepCommandLine(placeholder, "", 0, "", 1)
1054+
let recursiveTag = s:IsRecursiveSearch() ? " (Recursive)" : ""
1055+
call s:Echo("Search Mode: ".s:GetModeName(g:EasyGrepMode).recursiveTag)
1056+
10471057
if g:EasyGrepSearchCurrentBufferDir && s:IsBufferDirSearchAllowed()
10481058
let dirs = s:GetDirectorySearchList()
1059+
let dirAnnotation = "Search Directory: "
10491060
for d in dirs
10501061
call s:Echo(dirAnnotation.d)
10511062
let dirAnnotation = "Additional Directory: "
10521063
endfor
1053-
let bufferDirs = s:GetBufferDirsList()
1054-
if (len(bufferDirs) > len(dirs))
1055-
call s:Echo("Note: Additional directories covered by recursive search")
1056-
endif
10571064
else
1065+
let dirAnnotation = "Search Directory: "
10581066
call s:Echo(dirAnnotation.s:GetGrepRoot())
10591067
endif
1068+
1069+
let placeholder = "<pattern>"
1070+
let grepCommand = s:GetGrepCommandLine(placeholder, "", 0, "", 1)
10601071
call s:Echo("VIM command: ".grepCommand)
1072+
10611073
if s:GetGrepCommandName() == "grep"
10621074
let shellCommand = substitute(grepCommand, "grep", &grepprg, "")
10631075
call s:Echo("Shell command: ".shellCommand)
10641076
endif
1077+
10651078
if s:GetGrepCommandChoice(0) != s:GetGrepCommandChoice(1)
10661079
call s:Echo("Note: "."Overriding '".substitute(s:GetGrepCommandName(0), "grep", &grepprg, "")."' with '".s:GetGrepCommandName(1)."' due to 'Buffers' mode")
10671080
endif
@@ -2669,8 +2682,8 @@ function! s:ConfigureGrepCommandParameters()
26692682
\ 'opt_bool_bufferdirsearchallowed': '1',
26702683
\ 'opt_str_suppresserrormessages': '',
26712684
\ 'opt_bool_directoryneedsbackslash': '0',
2672-
\ 'opt_bool_isinherentlyrecursive': '0',
2673-
\ 'opt_bool_isselffiltering': '0',
2685+
\ 'opt_bool_isinherentlyrecursive': '1',
2686+
\ 'opt_bool_isselffiltering': '1',
26742687
\ 'opt_bool_replacewildcardwithcwd': '0',
26752688
\ })
26762689

0 commit comments

Comments
 (0)