@@ -42,43 +42,76 @@ partial class FrmNavigateTo : Form
4242
4343 public List < string > SelectedFiles { get ; set ; }
4444
45+ public string [ ] filesInCurrentDirectory { get ; set ; }
46+
47+ public bool shouldReloadFilesInDirectory { get ; set ; }
48+
49+ public long lastDirectoryReloadTimeTicks { get ; set ; }
50+
51+ public bool isShuttingDown { get ; set ; }
52+
4553 public bool IsFuzzyResult { get ; set ; }
4654
4755 public FrmNavigateTo ( IScintillaGateway editor , INotepadPPGateway notepad )
4856 {
4957 this . editor = editor ;
5058 this . notepad = notepad ;
5159 this . SelectedFiles = new List < string > ( ) ;
60+ filesInCurrentDirectory = null ;
61+ shouldReloadFilesInDirectory = true ;
5262 IsFuzzyResult = false ;
5363 InitializeComponent ( ) ;
5464 ReloadFileList ( ) ;
5565 this . notepad . ReloadMenuItems ( ) ;
5666 }
5767
58- void SearchInCurrentDirectory ( string s , string searchPattern1 )
68+ private static bool MatchesAllWordsInFilter ( string s , string [ ] words )
69+ {
70+ return words . All ( word =>
71+ s . IndexOf ( word , StringComparison . CurrentCultureIgnoreCase ) >= 0
72+ ) ;
73+ }
74+
75+ void SearchInCurrentDirectory ( string [ ] words )
5976 {
6077 string currentFilePath = notepad . GetCurrentFileDirectory ( ) ;
61- if ( ! String . IsNullOrWhiteSpace ( currentFilePath ) )
62- {
63- bool searchInSubDirs = FrmSettings . Settings . GetBoolSetting ( Settings . searchInSubDirs ) ;
64- foreach ( var filePath in Directory . GetFiles ( currentFilePath , searchPattern1 ,
65- searchInSubDirs ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly )
66- . AsParallel ( ) . Where ( e =>
67- e . IndexOf ( s , StringComparison . CurrentCultureIgnoreCase ) >= 0 ||
68- e . Contains ( s ) )
69- . ToList ( ) )
78+ if ( string . IsNullOrWhiteSpace ( currentFilePath ) )
79+ return ;
80+ bool searchInSubDirs = FrmSettings . Settings . GetBoolSetting ( Settings . searchInSubDirs ) ;
81+ long nextTimeToRefresh = lastDirectoryReloadTimeTicks +
82+ 10_000_000 * FrmSettings . Settings . GetIntSetting ( Settings . secondsBetweenDirectoryScans ) ; // 10 million ticks/sec
83+ if ( shouldReloadFilesInDirectory ||
84+ filesInCurrentDirectory == null ||
85+ DateTime . UtcNow . Ticks >= nextTimeToRefresh )
86+ {
87+ // only load the files in current directory as needed
88+ // the correct files to load will depend on the active buffer
89+ filesInCurrentDirectory = Directory . GetFiles (
90+ currentFilePath ,
91+ "*.*" ,
92+ searchInSubDirs ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly
93+ ) ;
94+ shouldReloadFilesInDirectory = false ;
95+ lastDirectoryReloadTimeTicks = DateTime . UtcNow . Ticks ;
96+ }
97+ foreach ( var filePath in filesInCurrentDirectory
98+ . AsParallel ( ) . Where ( fname => MatchesAllWordsInFilter ( fname , words ) )
99+ . ToList ( ) )
100+ {
101+ if ( ! FilteredFileList . Exists ( file => file . FilePath . Equals ( filePath ) ) )
70102 {
71- if ( ! FilteredFileList . Exists ( file => file . FilePath . Equals ( filePath ) ) )
72- {
73- FilteredFileList . Add ( new FileModel ( Path . GetFileName ( filePath ) , filePath , - 1 , - 1 ,
74- searchInSubDirs ? FOLDER_SUB : FOLDER_TOP , 0 ) ) ;
75- }
103+ FilteredFileList . Add ( new FileModel ( Path . GetFileName ( filePath ) , filePath , - 1 , - 1 ,
104+ searchInSubDirs ? FOLDER_SUB : FOLDER_TOP , 0 ) ) ;
76105 }
77106 }
78107 }
79108
80109 public void FilterDataGrid ( string filter )
81110 {
111+ // a bunch of NPPN_BUFFERACTIVATED events fire when Notepad++ is shutting down
112+ // which will lead to this being called repeatedly
113+ if ( isShuttingDown )
114+ return ;
82115 if ( ! string . IsNullOrEmpty ( searchComboBox . Text ) ) filter = searchComboBox . Text ;
83116
84117 if ( ! string . IsNullOrWhiteSpace ( filter ) )
@@ -117,16 +150,17 @@ public void FilterDataGrid(string filter)
117150 }
118151 else
119152 {
153+ var words = filter . Split ( ' ' ) . Where ( x => ! string . IsNullOrWhiteSpace ( x ) ) . ToArray ( ) ;
120154 searchInTabs ( filter ) ;
121155
122156 if ( FrmSettings . Settings . GetBoolSetting ( Settings . searchInCurrentFolder ) )
123157 {
124- SearchInCurrentDirectory ( filter , searchPattern ) ;
158+ SearchInCurrentDirectory ( words ) ;
125159 }
126160
127161 if ( FrmSettings . Settings . GetBoolSetting ( Settings . searchMenuCommands ) )
128162 {
129- FilterMenuCommands ( filter ) ;
163+ FilterMenuCommands ( words ) ;
130164 }
131165
132166 FilteredFileList . ForEach ( file =>
@@ -181,13 +215,11 @@ private void searchInTabs(string filter)
181215 if ( FrmSettings . Settings . GetBoolSetting ( Settings . preferFilenameResults ) )
182216 {
183217 FilteredFileList = FileList . AsParallel ( )
184- . Where ( e => words . All ( word =>
185- e . FileName . IndexOf ( word , StringComparison . CurrentCultureIgnoreCase ) >= 0 ) )
218+ . Where ( e => MatchesAllWordsInFilter ( e . FileName , words ) )
186219 . ToList ( ) ;
187220
188221 FileList . AsParallel ( )
189- . Where ( e => words . All ( word =>
190- e . FilePath . IndexOf ( filter , StringComparison . CurrentCultureIgnoreCase ) >= 0 ) )
222+ . Where ( e => MatchesAllWordsInFilter ( e . FilePath , words ) )
191223 . ToList ( ) . ForEach ( filePath =>
192224 {
193225 if ( ! FilteredFileList . Exists ( file => file . FilePath . Equals ( filePath . FilePath ) ) )
@@ -200,10 +232,10 @@ private void searchInTabs(string filter)
200232 else
201233 {
202234 FilteredFileList = FileList . AsParallel ( )
203- . Where ( e => words . All ( word =>
204- e . FilePath . IndexOf ( word , StringComparison . CurrentCultureIgnoreCase ) >= 0 ||
205- e . FileName . IndexOf ( word , StringComparison . CurrentCultureIgnoreCase ) >= 0 ) )
206- . ToList ( ) ;
235+ . Where ( e =>
236+ MatchesAllWordsInFilter ( e . FilePath , words ) ||
237+ MatchesAllWordsInFilter ( e . FileName , words )
238+ ) . ToList ( ) ;
207239 }
208240 }
209241
@@ -236,11 +268,11 @@ private void searchInTabs(string filter)
236268 }
237269 }
238270
239- private void FilterMenuCommands ( string filter )
271+ private void FilterMenuCommands ( string [ ] filterWords )
240272 {
241- foreach ( var nppMenuCmd in notepad . MainMenuItems . AsParallel ( ) . Where ( e => e . Value . IndexOf ( filter ,
242- StringComparison . CurrentCultureIgnoreCase ) >= 0 ||
243- e . Value . Contains ( filter ) ) . ToList ( ) )
273+ foreach ( var nppMenuCmd in notepad . MainMenuItems . AsParallel ( ) . Where ( e =>
274+ MatchesAllWordsInFilter ( e . Value , filterWords )
275+ ) . ToList ( ) )
244276 {
245277 FilteredFileList . Add ( new FileModel (
246278 nppMenuCmd . Key . ToString ( ) ,
@@ -251,6 +283,8 @@ private void FilterMenuCommands(string filter)
251283
252284 public void ReloadFileList ( )
253285 {
286+ if ( isShuttingDown )
287+ return ;
254288 if ( FileList == null ) FileList = new List < FileModel > ( ) ;
255289 FileList . Clear ( ) ;
256290 int firstViewCount =
0 commit comments