Skip to content

Commit 2eba599

Browse files
committed
Fix search with tools that are inherently recursive.
- Rename lst to fileTargetList - Add the drive prefix on win32 - Make a grep-specific WAR more general
1 parent 9642876 commit 2eba599

File tree

1 file changed

+39
-48
lines changed

1 file changed

+39
-48
lines changed

plugin/EasyGrep.vim

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ function! s:GetBufferDirsList()
142142
if empty(d)
143143
let d = currDir
144144
elseif has("win32") && d[0] == "/"
145-
" Remove the trailing slash
146-
let d = fnamemodify(d, ":s-/$--")
145+
" Add the drive prefix but remove the trailing slash
146+
let d = fnamemodify(d, ":p:s-/$--")
147147
endif
148148
let dirs[d]=1
149149
endfor
@@ -637,12 +637,12 @@ endfunction
637637
" GetFileTargetList {{{
638638
function! s:GetFileTargetList(addAdditionalLocations)
639639
let addAdditionalLocations = a:addAdditionalLocations
640-
let lst = []
640+
let fileTargetList = []
641641
if s:IsModeBuffers()
642-
let lst = s:EscapeList(s:GetBufferNamesList(), " ")
642+
let fileTargetList = s:EscapeList(s:GetBufferNamesList(), " ")
643643
let addAdditionalLocations = 0
644644
elseif s:IsModeTracked()
645-
let lst = s:GetFileTargetList_Tracked()
645+
let fileTargetList = s:GetFileTargetList_Tracked()
646646
else
647647
let i = 0
648648
let numItems = len(s:Dict)
@@ -655,36 +655,36 @@ function! s:GetFileTargetList(addAdditionalLocations)
655655
endwhile
656656

657657
if !empty(keyList)
658-
let lst = s:CollectEnabledFileTargets(keyList)
658+
let fileTargetList = s:CollectEnabledFileTargets(keyList)
659659
else
660660
call s:InternalFailure("Keylist should not be empty")
661-
let lst = [ "*" ]
661+
let fileTargetList = [ "*" ]
662662
endif
663663
endif
664664

665665
if addAdditionalLocations
666-
let lst = s:AddAdditionalLocationsToFileTargetList(lst)
666+
let fileTargetList = s:AddAdditionalLocationsToFileTargetList(fileTargetList)
667667
endif
668668

669-
return lst
669+
return fileTargetList
670670
endfunction
671671
" }}}
672672
" AddAdditionalLocationsToFileTargetList {{{
673-
function! s:AddAdditionalLocationsToFileTargetList(lst)
674-
let lst = a:lst
675-
if empty(lst) || s:IsModeBuffers()
676-
return lst
673+
function! s:AddAdditionalLocationsToFileTargetList(fileTargetList)
674+
let fileTargetList = a:fileTargetList
675+
if empty(fileTargetList) || s:IsModeBuffers()
676+
return fileTargetList
677677
endif
678678

679679
if g:EasyGrepSearchCurrentBufferDir && s:IsBufferDirSearchAllowed()
680-
let lst = s:ApplySearchDirectoriesToFileTargetList(lst)
680+
let fileTargetList = s:ApplySearchDirectoriesToFileTargetList(fileTargetList)
681681
endif
682682

683683
if g:EasyGrepHidden
684684
let i = 0
685-
let size = len(lst)
685+
let size = len(fileTargetList)
686686
while i < size
687-
let item = lst[i]
687+
let item = fileTargetList[i]
688688
let lastpiece = strridx(item, '/')
689689
if lastpiece == -1 && item[0] == '.'
690690
" skip this item, it's already hidden
@@ -700,21 +700,19 @@ function! s:AddAdditionalLocationsToFileTargetList(lst)
700700
endif
701701
let i += 1
702702
let size += 1
703-
call insert(lst, newItem, i)
703+
call insert(fileTargetList, newItem, i)
704704
endif
705705
let i += 1
706706
endwhile
707707
endif
708708

709709
let newlst = []
710-
for item in lst
710+
for item in fileTargetList
711711
if s:IsRecursiveSearch() && s:IsCommandVimgrep()
712712
" Insert a recursive specifier into the command
713-
let str = substitute(item, '/\([^/]\+\)$', '/**/\1', "")
714-
else
715-
let str = item
713+
let item = substitute(item, '/\([^/]\+\)$', '/**/\1', "")
716714
endif
717-
call add(newlst, str)
715+
call add(newlst, item)
718716
endfor
719717

720718
return newlst
@@ -760,8 +758,9 @@ endfunction
760758
" IsRecursivelyReachable {{{
761759
function! s:IsRecursivelyReachable(fromthisdir, target)
762760
let directoryTarget = fnamemodify(a:target, ":p:h")
761+
let fromthisdir = a:fromthisdir == "." ? s:GetCwdEscaped() : a:fromthisdir
763762

764-
if match(directoryTarget, a:fromthisdir) != 0
763+
if match(directoryTarget, fromthisdir) != 0
765764
return 0
766765
endif
767766

@@ -1047,11 +1046,13 @@ function! <sid>EchoGrepCommand()
10471046
endfor
10481047
else
10491048
let dirAnnotation = "Search Directory: "
1050-
call s:Echo(dirAnnotation.s:GetGrepRoot())
1049+
let d = s:GetGrepRoot()
1050+
let d = (d == ".") ? d." --> ".s:GetCwdEscaped()."" : d
1051+
call s:Echo(dirAnnotation.d)
10511052
endif
10521053

10531054
let placeholder = "<pattern>"
1054-
let grepCommand = s:GetGrepCommandLine(placeholder, "", 0, "", 1)
1055+
let grepCommand = s:GetGrepCommandLine(placeholder, "", 0, "", 1, 0)
10551056
call s:Echo("VIM command: ".grepCommand)
10561057

10571058
if s:GetGrepCommandName() == "grep"
@@ -2550,7 +2551,6 @@ function! s:ConfigureGrepCommandParameters()
25502551
\ 'opt_bool_directoryneedsbackslash': '0',
25512552
\ 'opt_bool_isinherentlyrecursive': '0',
25522553
\ 'opt_bool_isselffiltering': '0',
2553-
\ 'opt_bool_replacewildcardwithcwd': '0',
25542554
\ })
25552555

25562556
call s:RegisterGrepProgram("grep", {
@@ -2573,8 +2573,7 @@ function! s:ConfigureGrepCommandParameters()
25732573
\ 'opt_bool_directoryneedsbackslash': '0',
25742574
\ 'opt_bool_isinherentlyrecursive': '0',
25752575
\ 'opt_bool_isselffiltering': '0',
2576-
\ 'opt_bool_replacewildcardwithcwd': '0',
2577-
\ 'opt_bool_greprecursionwar': '1',
2576+
\ 'opt_str_recursiveinclusionexpression': '"--include=\"" .v:val."\""',
25782577
\ })
25792578

25802579
call s:RegisterGrepProgram("git", {
@@ -2597,7 +2596,6 @@ function! s:ConfigureGrepCommandParameters()
25972596
\ 'opt_bool_directoryneedsbackslash': '0',
25982597
\ 'opt_bool_isinherentlyrecursive': '0',
25992598
\ 'opt_bool_isselffiltering': '0',
2600-
\ 'opt_bool_replacewildcardwithcwd': '0',
26012599
\ })
26022600

26032601
call s:RegisterGrepProgram("ack", {
@@ -2620,7 +2618,6 @@ function! s:ConfigureGrepCommandParameters()
26202618
\ 'opt_bool_directoryneedsbackslash': '0',
26212619
\ 'opt_bool_isinherentlyrecursive': '1',
26222620
\ 'opt_bool_isselffiltering': '1',
2623-
\ 'opt_bool_replacewildcardwithcwd': '1',
26242621
\ })
26252622

26262623
call s:RegisterGrepProgram("ack-grep", s:commandParamsDict["ack"])
@@ -2645,7 +2642,6 @@ function! s:ConfigureGrepCommandParameters()
26452642
\ 'opt_bool_directoryneedsbackslash': '0',
26462643
\ 'opt_bool_isinherentlyrecursive': '1',
26472644
\ 'opt_bool_isselffiltering': '1',
2648-
\ 'opt_bool_replacewildcardwithcwd': '1',
26492645
\ })
26502646

26512647
call s:RegisterGrepProgram("pt", {
@@ -2668,7 +2664,6 @@ function! s:ConfigureGrepCommandParameters()
26682664
\ 'opt_bool_directoryneedsbackslash': '0',
26692665
\ 'opt_bool_isinherentlyrecursive': '1',
26702666
\ 'opt_bool_isselffiltering': '1',
2671-
\ 'opt_bool_replacewildcardwithcwd': '0',
26722667
\ })
26732668

26742669
call s:RegisterGrepProgram("findstr", {
@@ -2691,7 +2686,6 @@ function! s:ConfigureGrepCommandParameters()
26912686
\ 'opt_bool_directoryneedsbackslash': '1',
26922687
\ 'opt_bool_isinherentlyrecursive': '0',
26932688
\ 'opt_bool_isselffiltering': '0',
2694-
\ 'opt_bool_replacewildcardwithcwd': '0',
26952689
\ })
26962690
endfunction
26972691
" }}}
@@ -2710,7 +2704,7 @@ function! s:GetGrepCommandParameters()
27102704
endfunction
27112705
" }}}
27122706
" GetGrepCommandLine {{{
2713-
function! s:GetGrepCommandLine(pattern, add, wholeword, count, escapeArgs)
2707+
function! s:GetGrepCommandLine(pattern, add, wholeword, count, escapeArgs, filterTargetsWithNoFiles)
27142708

27152709
call s:CheckCommandRequirements()
27162710

@@ -2777,7 +2771,7 @@ function! s:GetGrepCommandLine(pattern, add, wholeword, count, escapeArgs)
27772771
let fileTargetList = s:GetFileTargetList(1)
27782772
let filesToExclude = g:EasyGrepFilesToExclude
27792773

2780-
if s:CommandHas("opt_bool_filtertargetswithnofiles")
2774+
if a:filterTargetsWithNoFiles && s:CommandHas("opt_bool_filtertargetswithnofiles")
27812775
call s:FilterTargetsWithNoFiles(fileTargetList)
27822776
endif
27832777

@@ -2790,22 +2784,19 @@ function! s:GetGrepCommandLine(pattern, add, wholeword, count, escapeArgs)
27902784
let opts .= " " . join(map(split(filesToExclude, ','), commandParams["opt_str_mapexclusionsexpression"]), ' ') . " "
27912785
endif
27922786

2793-
if s:CommandHas("opt_bool_replacewildcardwithcwd")
2794-
" 1) Replace a leading star with the current directory
2795-
" 2) Replace all trailing stars with a space
2787+
if s:CommandHas("opt_bool_isinherentlyrecursive")
2788+
" Eliminate a trailing star
2789+
call map(fileTargetList, 'substitute(v:val, "/\\*$", "/", "")')
2790+
"" Replace an individual star with a dot
27962791
call map(fileTargetList, 'substitute(v:val, "^\\*$", s:GetGrepRoot(), "")')
27972792
endif
27982793

27992794
" Set extra inclusions and exclusions
2800-
if s:CommandHas("opt_bool_greprecursionwar")
2801-
" Specific inclusions are only set in recursive mode
2802-
if s:IsRecursiveSearch()
2803-
" The --include paths will contain the file patterns
2804-
let opts .= " " . join(map(fileTargetList, '"--include=\"" .v:val."\""'), ' '). " "
2805-
2806-
" while the files we specify will be directories
2807-
let fileTargetList = s:GetDirectorySearchList()
2808-
endif
2795+
if s:CommandHasLen("opt_str_recursiveinclusionexpression") && s:IsRecursiveSearch()
2796+
" Explicitly specify the file types as arguments according to the configured expression
2797+
let opts .= " " . join(map(fileTargetList, commandParams["opt_str_recursiveinclusionexpression"]), ' '). " "
2798+
" while the files we specify will be directories
2799+
let fileTargetList = s:GetDirectorySearchList()
28092800
endif
28102801

28112802
let filesToGrep = join(fileTargetList, ' ')
@@ -2856,7 +2847,7 @@ function! s:DoGrep(pattern, add, wholeword, count, escapeArgs)
28562847
endif
28572848

28582849
call s:SetGrepVariables(commandName)
2859-
let grepCommand = s:GetGrepCommandLine(a:pattern, a:add, a:wholeword, a:count, a:escapeArgs)
2850+
let grepCommand = s:GetGrepCommandLine(a:pattern, a:add, a:wholeword, a:count, a:escapeArgs, 1)
28602851

28612852
" change directory to the grep root before executing
28622853
call s:ChangeDirectoryToGrepRoot()

0 commit comments

Comments
 (0)