Skip to content

Commit eeb7b2b

Browse files
committed
bump .NET to v4.8, reduce directory searches
1. Update .NET Framework to 4.8 (this may not be necessary, but I can't compile this project without it) If you want to change this back to 4.0, fine by me. 2. Add a check if Notepad++ is shutting down before updating file list (NPPN_BUFFERACTIVATED events fire when Notepad++ is shutting down, but the user doesn't care about the file list at that point) 3. When files in the current file's directory are queried, only update those files at the following times: * if no files in directory list exists yet * when a new buffer is activated * if the last refresh of the files in directory list was at least N seconds ago (where N is the value of the Settings.secondsBetweenDirectoryScans variable) 4. Because querying the files in the directory is much faster now, change the search algorithm so that it checks if all words in the search box are in the current file's directory and the menu commands (to be consistent with the behavior for searching tabs)
1 parent 6a19290 commit eeb7b2b

File tree

6 files changed

+249
-121
lines changed

6 files changed

+249
-121
lines changed

NppNavigateTo/Forms/FrmNavigateTo.cs

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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 =

NppNavigateTo/Forms/FrmSettings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private void frmSettings_Shown(object sender, EventArgs e)
6262
numericUpDownMinGridWidth.Value = Settings.GetIntSetting(Settings.gridMinWidth);
6363
checkBoxSearchInFolder.Checked = Settings.GetBoolSetting(Settings.searchInCurrentFolder);
6464
checkBoxSearchInSubDirs.Checked = Settings.GetBoolSetting(Settings.searchInSubDirs);
65+
numUpDownSecsBetweenDirectoryScans.Value = Settings.GetIntSetting(Settings.secondsBetweenDirectoryScans);
6566
checkBoxSearchMenu.Checked = Settings.GetBoolSetting(Settings.searchMenuCommands);
6667
checkBoxCleanSearch.Checked = Settings.GetBoolSetting(Settings.clearOnClose);
6768
checkBoxKeepSelected.Checked = Settings.GetBoolSetting(Settings.selectFirstRowOnFilter);
@@ -333,5 +334,10 @@ private void buttonRowBackgroud_Click(object sender, EventArgs e)
333334

334335
RefreshDataGridStyles();
335336
}
337+
338+
private void secondsBetweenDirectoryScans_ValueChanged(object sender, EventArgs e)
339+
{
340+
Settings.SetIntSetting(Settings.secondsBetweenDirectoryScans, (int)numUpDownSecsBetweenDirectoryScans.Value);
341+
}
336342
}
337343
}

0 commit comments

Comments
 (0)