11using System ;
2- using System . Collections ;
32using System . Collections . Generic ;
43using System . ComponentModel ;
54using System . Drawing ;
6- using System . Globalization ;
75using System . IO ;
86using System . Linq ;
9- using System . Net ;
10- using System . Reflection ;
11- using System . Reflection . Emit ;
12- using System . Runtime . CompilerServices ;
13- using System . Text ;
147using System . Text . RegularExpressions ;
15- using System . Threading ;
16- using System . Windows . Controls ;
17- using System . Windows . Documents ;
8+ using System . Threading . Tasks ;
189using System . Windows . Forms ;
19- using System . Windows . Input ;
20- using System . Windows . Shapes ;
21- using Kbg . NppPluginNET ;
10+ using System . Windows . Threading ;
2211using Kbg . NppPluginNET . PluginInfrastructure ;
2312using NppPluginNET ;
24- using static System . String ;
25- using static System . Windows . Forms . VisualStyles . VisualStyleElement ;
2613using Control = System . Windows . Forms . Control ;
2714using KeyEventArgs = System . Windows . Forms . KeyEventArgs ;
2815using MouseEventArgs = System . Windows . Forms . MouseEventArgs ;
@@ -56,6 +43,7 @@ partial class FrmNavigateTo : Form
5643 public List < string > SelectedFiles { get ; set ; }
5744
5845 public string currentDirectory { get ; set ; }
46+ public SolidBrush hlBrush { get ; set ; }
5947
6048 public string [ ] filesInCurrentDirectory { get ; set ; }
6149
@@ -69,11 +57,8 @@ partial class FrmNavigateTo : Form
6957
7058 private Glob glob { get ; set ; }
7159
72- const int lastKeyPressTimerInterval = 250 ;
60+ private DispatcherTimer lastKeyPressTimer ;
7361
74- private System . Timers . Timer lastKeyPressTimer ;
75-
76- private long lastKeypressTicks ;
7762
7863 /// <summary>
7964 /// function used to filter filenames when not using fuzzy matchin
@@ -103,16 +88,17 @@ public FrmNavigateTo(IScintillaGateway editor, INotepadPPGateway notepad)
10388 shouldReloadFiles = true ;
10489 IsFuzzyResult = false ;
10590 lastReloadTimes = new Dictionary < string , long > ( ) ;
91+ hlBrush =
92+ new SolidBrush (
93+ Color . FromArgb ( FrmSettings . Settings . GetIntSetting ( Settings . highlightColorBackground ) ) ) ;
10694 searchLevelOverrides = new Dictionary < string , DirectorySearchLevel > ( ) ;
10795 //backgroundWorker = new BackgroundWorker();
10896 //backgroundWorker.WorkerSupportsCancellation = true;
10997 //backgroundWorker.DoWork += BackgroundWorker_DoWork;
11098 glob = new Glob ( ) ;
111- lastKeyPressTimer = new System . Timers . Timer ( ) ;
112- lastKeyPressTimer . AutoReset = false ;
113- lastKeyPressTimer . Interval = lastKeyPressTimerInterval ;
114- lastKeyPressTimer . Elapsed += LastKeyPressTimer_Elapsed ;
115- lastKeypressTicks = 0 ;
99+ lastKeyPressTimer = new DispatcherTimer ( ) ;
100+ lastKeyPressTimer . Interval = TimeSpan . FromMilliseconds ( FrmSettings . Settings . GetIntSetting ( Settings . searchDelayMs ) ) ;
101+ lastKeyPressTimer . Tick += OnSearchTimerTick ;
116102 nonFuzzyFilterFunc = null ;
117103 InitializeComponent ( ) ;
118104 ReloadFileList ( ) ;
@@ -189,14 +175,17 @@ public void FilterDataGrid(string filter)
189175
190176 foreach ( DataGridViewRow selectedRow in dataGridFileList . SelectedRows )
191177 {
192- SelectedFiles . Add ( selectedRow . Cells [ 1 ] . Value . ToString ( ) ) ;
178+ if ( selectedRow . Cells [ 1 ] . Value != null )
179+ {
180+ SelectedFiles . Add ( selectedRow . Cells [ 1 ] . Value . ToString ( ) ) ;
181+ }
193182 }
194183
195184 dataGridFileList . Rows . Clear ( ) ;
196185 if ( emptyFilter )
197186 {
198187 tooManyResultsToHighlight = FileList . Count >= FrmSettings . Settings . GetIntSetting ( Settings . maxResultsHighlightingEnabled ) ;
199- FileList . ForEach ( file =>
188+ dataGridFileList . Rows . AddRange ( FileList . ConvertAll < DataGridViewRow > ( file =>
200189 {
201190 DataGridViewRow newRow = new DataGridViewRow ( ) ;
202191
@@ -205,9 +194,8 @@ public void FilterDataGrid(string filter)
205194 newRow . Cells [ 0 ] . Value = file . FileName ;
206195 newRow . Cells [ 1 ] . Value = file . FilePath ;
207196 newRow . Cells [ 2 ] . Value = file . Source ;
208-
209- dataGridFileList . Rows . Add ( newRow ) ;
210- } ) ;
197+ return newRow ;
198+ } ) . ToArray ( ) ) ;
211199 }
212200 else
213201 {
@@ -216,7 +204,7 @@ public void FilterDataGrid(string filter)
216204 FilterEverything ( filter ) ;
217205 tooManyResultsToHighlight = FilteredFileList . Count >= FrmSettings . Settings . GetIntSetting ( Settings . maxResultsHighlightingEnabled ) ;
218206
219- FilteredFileList . ForEach ( file =>
207+ dataGridFileList . Rows . AddRange ( FilteredFileList . ConvertAll < DataGridViewRow > ( file =>
220208 {
221209 DataGridViewRow newRow = new DataGridViewRow ( ) ;
222210
@@ -225,9 +213,8 @@ public void FilterDataGrid(string filter)
225213 newRow . Cells [ 0 ] . Value = file . FileName ;
226214 newRow . Cells [ 1 ] . Value = file . FilePath ;
227215 newRow . Cells [ 2 ] . Value = file . Source ;
228-
229- dataGridFileList . Rows . Add ( newRow ) ;
230- } ) ;
216+ return newRow ;
217+ } ) . ToArray ( ) ) ;
231218 }
232219
233220 //auto sort
@@ -303,7 +290,6 @@ private void searchInTabs(string filter, Func<string, bool> nonFuzzyFilterFunc/*
303290 . Where ( fm => nonFuzzyFilterFunc ( fm . FileName ) || nonFuzzyFilterFunc ( fm . FilePath ) )
304291 . ToList ( ) ;
305292 }
306-
307293 //run fuzzy search if there are no results from normal search and it is enabled
308294 bool fuzzy = FrmSettings . Settings . GetBoolSetting ( Settings . fuzzySearch ) ;
309295 if ( FilteredFileList != null && FilteredFileList . Count == 0 && fuzzy )
@@ -475,6 +461,8 @@ void frmNavigateAll_VisibleChanged(object sender, EventArgs e)
475461 {
476462 if ( Visible )
477463 {
464+ hlBrush = new SolidBrush (
465+ Color . FromArgb ( FrmSettings . Settings . GetIntSetting ( Settings . highlightColorBackground ) ) ) ;
478466 SetColumnsWidth ( ) ;
479467 RefreshDataGridStyles ( ) ;
480468 }
@@ -487,6 +475,7 @@ void frmNavigateAll_VisibleChanged(object sender, EventArgs e)
487475
488476 Win32 . SendMessage ( PluginBase . nppData . _nppHandle , ( uint ) NppMsg . NPPM_SETMENUITEMCHECK ,
489477 PluginBase . _funcItems . Items [ Main . idFormNavigateAll ] . _cmdID , 0 ) ;
478+ hlBrush . Dispose ( ) ;
490479 }
491480 }
492481
@@ -660,33 +649,43 @@ private bool NavigateGridDown(bool isShiftPressed)
660649
661650 private void SearchComboBoxTextChanged ( object sender , EventArgs e )
662651 {
652+ lastKeyPressTimer . Stop ( ) ;
663653 lastKeyPressTimer . Start ( ) ;
664- lastKeypressTicks = System . DateTime . UtcNow . Ticks ;
665654 }
666655
667- private void LastKeyPressTimer_Elapsed ( object sender , EventArgs e )
656+ private void OnSearchTimerTick ( object sender , EventArgs e )
668657 {
669- Invoke ( new Action ( ( ) =>
670- {
671- // allow keypresses more recent than the interval, because we can't rely on precise timing of this event firing
672- if ( System . DateTime . UtcNow . Ticks <= lastKeypressTicks + System . TimeSpan . TicksPerMillisecond * lastKeyPressTimerInterval / 10 )
673- return ;
674- int textLength = searchComboBox . Text . Length ;
675- bool emptyText = string . IsNullOrWhiteSpace ( searchComboBox . Text ) ;
676- int minLength = FrmSettings . Settings . GetIntSetting ( Settings . minTypeCharLimit ) ;
658+ lastKeyPressTimer . Stop ( ) ;
659+ Search ( searchComboBox . Text ) ;
660+ }
677661
678- if ( emptyText )
679- ReloadFileList ( ) ;
680- if ( textLength > minLength || emptyText )
662+ private void Search ( string text )
663+ {
664+ Task . Run ( ( ) =>
665+ {
666+ lastKeyPressTimer . Dispatcher . Invoke ( ( ) =>
681667 {
682- FilterDataGrid ( searchComboBox . Text ) ;
683- }
668+ if ( searchComboBox . Text == text )
669+ {
670+ int textLength = searchComboBox . Text . Length ;
671+ bool emptyText = string . IsNullOrWhiteSpace ( searchComboBox . Text ) ;
672+ int minLength = FrmSettings . Settings . GetIntSetting ( Settings . minTypeCharLimit ) ;
684673
685- if ( FrmSettings . Settings . GetBoolSetting ( Settings . selectFirstRowOnFilter ) )
686- {
687- SelectFirstRow ( ) ;
688- }
689- } ) ) ;
674+ if ( emptyText )
675+ ReloadFileList ( ) ;
676+ if ( textLength > minLength || emptyText )
677+ {
678+ FilterDataGrid ( searchComboBox . Text ) ;
679+ }
680+
681+ if ( FrmSettings . Settings . GetBoolSetting ( Settings . selectFirstRowOnFilter ) )
682+ {
683+ SelectFirstRow ( ) ;
684+ }
685+ }
686+ } ) ;
687+ } ) ;
688+
690689 }
691690
692691 private void SelectFirstRow ( )
@@ -773,7 +772,9 @@ private void dataGridFileList_CellPainting(object sender, DataGridViewCellPainti
773772 {
774773 if ( e . Value == null
775774 || searchComboBox . Text . Length == 0
776- || tooManyResultsToHighlight )
775+ || tooManyResultsToHighlight
776+ || IsFuzzyResult && ( e . ColumnIndex != 0 )
777+ )
777778 return ;
778779
779780 if ( e . RowIndex > - 1 && e . ColumnIndex > - 1 )
@@ -791,6 +792,8 @@ private void dataGridFileList_CellPainting(object sender, DataGridViewCellPainti
791792 }
792793 List < Rectangle > rectangleList = new List < Rectangle > ( ) ;
793794
795+
796+
794797 if ( ! String . IsNullOrWhiteSpace ( search ) )
795798 {
796799 foreach ( var word in filterList )
@@ -855,16 +858,11 @@ private void dataGridFileList_CellPainting(object sender, DataGridViewCellPainti
855858 }
856859 }
857860
858- SolidBrush hlBrush =
859- new SolidBrush (
860- Color . FromArgb ( FrmSettings . Settings . GetIntSetting ( Settings . highlightColorBackground ) ) ) ;
861-
862861 foreach ( var recto in rectangleList )
863862 {
864863 e . Graphics . FillRectangle ( hlBrush , recto ) ;
865864 }
866865
867- hlBrush . Dispose ( ) ;
868866 e . PaintContent ( e . CellBounds ) ;
869867 }
870868 }
@@ -980,7 +978,7 @@ private void toolStripMenuItemClose_Click(object sender, EventArgs e)
980978 {
981979 foreach ( DataGridViewRow selectedRow in dataGridFileList . SelectedRows )
982980 {
983- if ( selectedRow . Cells [ 2 ] . Value . Equals ( TABS ) )
981+ if ( selectedRow . Cells [ 1 ] . Value != null && TABS . Equals ( selectedRow . Cells [ 2 ] . Value ) )
984982 {
985983 notepad . SwitchToFile ( selectedRow . Cells [ 1 ] . Value . ToString ( ) , true ) ;
986984 Win32 . SendMessage ( PluginBase . nppData . _nppHandle , ( uint ) NppMsg . NPPM_MENUCOMMAND , 0 ,
0 commit comments