1616package org .springframework .shell .core .tui .component ;
1717
1818import java .io .IOException ;
19+ import java .nio .file .FileSystems ;
1920import java .nio .file .FileVisitResult ;
2021import java .nio .file .Files ;
2122import java .nio .file .Path ;
2223import java .nio .file .Paths ;
24+ import java .nio .file .PathMatcher ;
25+ import java .nio .file .SimpleFileVisitor ;
2326import java .nio .file .attribute .BasicFileAttributes ;
2427import java .util .ArrayList ;
2528import java .util .Collections ;
3437import java .util .stream .Collectors ;
3538import java .util .stream .Stream ;
3639
37- import org .apache .commons .io .file .AccumulatorPathVisitor ;
38- import org .apache .commons .io .file .Counters ;
39- import org .apache .commons .io .filefilter .IOFileFilter ;
40- import org .apache .commons .io .filefilter .NotFileFilter ;
41- import org .apache .commons .io .filefilter .WildcardFileFilter ;
4240import org .jline .keymap .BindingReader ;
4341import org .jline .keymap .KeyMap ;
4442import org .jline .terminal .Terminal ;
7371 *
7472 * @author Janne Valkealahti
7573 * @author Piotr Olaszewski
74+ * @author Andrey Litvitski
7675 */
7776public class PathSearch extends AbstractTextComponent <Path , PathSearchContext > {
7877
@@ -614,16 +613,15 @@ public PathScannerResult apply(String input, PathSearchContext context) {
614613 Path path = Path .of (p );
615614 log .debug (String .format ("Walking input %s for path %s" , input , path ));
616615 Files .walkFileTree (path , visitor );
617- log .debug (String .format ("walked files %s dirs %s" , visitor .getPathCounters ().getFileCounter ().get (),
618- visitor .getPathCounters ().getDirectoryCounter ().get ()));
616+ log .debug (String .format ("walked files %s dirs %s" , visitor .getFileCount (), visitor .getDirCount ()));
619617 }
620618 catch (Exception e ) {
621619 log .debug ("PathSearchPathVisitor caused exception" , e );
622620 }
623621
624622 // match and score candidates
625623 Set <ScoredPath > treeSet = new HashSet <ScoredPath >();
626- Stream .concat (visitor .getFileList ().stream (), visitor .getDirList ().stream ()).forEach (p -> {
624+ Stream .concat (visitor .getFiles ().stream (), visitor .getDirs ().stream ()).forEach (p -> {
627625 SearchMatchResult result ;
628626 if (StringUtils .hasText (match )) {
629627 SearchMatch searchMatch = SearchMatch .builder ()
@@ -643,37 +641,81 @@ public PathScannerResult apply(String input, PathSearchContext context) {
643641 return treeSet .stream ()
644642 .sorted ()
645643 .limit (context .getPathSearchConfig ().getMaxPathsSearch ())
646- .collect (Collectors .collectingAndThen (Collectors .toList (),
647- list -> PathScannerResult .of (list , visitor .getPathCounters ().getDirectoryCounter ().get (),
648- visitor .getPathCounters ().getFileCounter ().get (), StringUtils .hasText (match ))));
644+ .collect (Collectors .collectingAndThen (Collectors .toList (), list -> PathScannerResult .of (list ,
645+ visitor .getDirCount (), visitor .getFileCount (), StringUtils .hasText (match ))));
649646 }
650647
651648 }
652649
653650 /**
654- * Extension to AccumulatorPathVisitor which allows to break out from scanning when
655- * enough results are found.
651+ * Extension to SimpleFileVisitor which allows to break out from scanning when enough
652+ * results are found.
656653 */
657- private static class PathSearchPathVisitor extends AccumulatorPathVisitor {
654+ private static class PathSearchPathVisitor extends SimpleFileVisitor < Path > {
658655
659656 private final int limitFiles ;
660657
661- private final static IOFileFilter DNFILTER = new NotFileFilter (new WildcardFileFilter (".*" ));
658+ private long fileCount ;
659+
660+ private long dirCount ;
661+
662+ private final List <Path > files = new ArrayList <>();
663+
664+ private final List <Path > dirs = new ArrayList <>();
665+
666+ private final PathMatcher hiddenMatcher = FileSystems .getDefault ().getPathMatcher ("glob:.*" );
662667
663668 PathSearchPathVisitor (int limitFiles ) {
664- super (Counters .longPathCounters (), DNFILTER , DNFILTER );
665669 this .limitFiles = limitFiles ;
666670 }
667671
672+ private boolean accept (Path path ) {
673+ Path name = path .getFileName ();
674+ if (name == null ) {
675+ return false ;
676+ }
677+ return !this .hiddenMatcher .matches (name );
678+ }
679+
680+ @ Override
681+ public FileVisitResult preVisitDirectory (Path dir , BasicFileAttributes attrs ) throws IOException {
682+ if (!accept (dir )) {
683+ return FileVisitResult .SKIP_SUBTREE ;
684+ }
685+ this .dirs .add (dir );
686+ this .dirCount ++;
687+ return FileVisitResult .CONTINUE ;
688+ }
689+
668690 @ Override
669691 public FileVisitResult visitFile (Path file , BasicFileAttributes attributes ) throws IOException {
670692 FileVisitResult result = super .visitFile (file , attributes );
671- if (getPathCounters ().getFileCounter ().get () >= this .limitFiles ) {
672- return FileVisitResult .TERMINATE ;
693+ if (accept (file )) {
694+ this .files .add (file );
695+ this .fileCount ++;
696+ if (this .fileCount >= this .limitFiles ) {
697+ return FileVisitResult .TERMINATE ;
698+ }
673699 }
674700 return result ;
675701 }
676702
703+ public long getDirCount () {
704+ return this .dirCount ;
705+ }
706+
707+ public List <Path > getFiles () {
708+ return this .files ;
709+ }
710+
711+ public List <Path > getDirs () {
712+ return this .dirs ;
713+ }
714+
715+ public long getFileCount () {
716+ return this .fileCount ;
717+ }
718+
677719 }
678720
679721}
0 commit comments