Skip to content

Commit

Permalink
Merge pull request #41 from molsonkiko/match_space_separated
Browse files Browse the repository at this point in the history
bump .NET to v4.8, reduce directory searches
  • Loading branch information
young-developer authored Jun 1, 2023
2 parents 6a19290 + 67606f3 commit d501926
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 121 deletions.
86 changes: 57 additions & 29 deletions NppNavigateTo/Forms/FrmNavigateTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,66 @@ partial class FrmNavigateTo : Form

public List<string> SelectedFiles { get; set; }

public string[] filesInCurrentDirectory { get; set; }

public bool shouldReloadFilesInDirectory { get; set; }

public long lastDirectoryReloadTimeTicks { get; set; }

public bool isShuttingDown { get; set; }

public bool IsFuzzyResult { get; set; }

public FrmNavigateTo(IScintillaGateway editor, INotepadPPGateway notepad)
{
this.editor = editor;
this.notepad = notepad;
this.SelectedFiles = new List<string>();
filesInCurrentDirectory = null;
shouldReloadFilesInDirectory = true;
IsFuzzyResult = false;
InitializeComponent();
ReloadFileList();
this.notepad.ReloadMenuItems();
}

void SearchInCurrentDirectory(string s, string searchPattern1)
private static bool MatchesAllWordsInFilter(string s, string[] words)
{
return words.All(word =>
s.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) >= 0
);
}

void SearchInCurrentDirectory(string[] words)
{
string currentFilePath = notepad.GetCurrentFileDirectory();
if (!String.IsNullOrWhiteSpace(currentFilePath))
{
bool searchInSubDirs = FrmSettings.Settings.GetBoolSetting(Settings.searchInSubDirs);
foreach (var filePath in Directory.GetFiles(currentFilePath, searchPattern1,
searchInSubDirs ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.AsParallel().Where(e =>
e.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) >= 0 ||
e.Contains(s))
.ToList())
if (string.IsNullOrWhiteSpace(currentFilePath))
return;
bool searchInSubDirs = FrmSettings.Settings.GetBoolSetting(Settings.searchInSubDirs);
long nextTimeToRefresh = lastDirectoryReloadTimeTicks +
10_000_000 * FrmSettings.Settings.GetIntSetting(Settings.secondsBetweenDirectoryScans); // 10 million ticks/sec
if (shouldReloadFilesInDirectory ||
filesInCurrentDirectory == null ||
DateTime.UtcNow.Ticks >= nextTimeToRefresh)
{
// only load the files in current directory as needed
// the correct files to load will depend on the active buffer
filesInCurrentDirectory = Directory.GetFiles(
currentFilePath,
"*.*",
searchInSubDirs ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly
);
shouldReloadFilesInDirectory = false;
lastDirectoryReloadTimeTicks = DateTime.UtcNow.Ticks;
}
foreach (var filePath in filesInCurrentDirectory
.AsParallel().Where(fname => MatchesAllWordsInFilter(fname, words))
.ToList())
{
if (!FilteredFileList.Exists(file => file.FilePath.Equals(filePath)))
{
if (!FilteredFileList.Exists(file => file.FilePath.Equals(filePath)))
{
FilteredFileList.Add(new FileModel(Path.GetFileName(filePath), filePath, -1, -1,
searchInSubDirs ? FOLDER_SUB : FOLDER_TOP, 0));
}
FilteredFileList.Add(new FileModel(Path.GetFileName(filePath), filePath, -1, -1,
searchInSubDirs ? FOLDER_SUB : FOLDER_TOP, 0));
}
}
}
Expand Down Expand Up @@ -117,16 +146,17 @@ public void FilterDataGrid(string filter)
}
else
{
var words = filter.Split(' ').Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
searchInTabs(filter);

if (FrmSettings.Settings.GetBoolSetting(Settings.searchInCurrentFolder))
{
SearchInCurrentDirectory(filter, searchPattern);
SearchInCurrentDirectory(words);
}

if (FrmSettings.Settings.GetBoolSetting(Settings.searchMenuCommands))
{
FilterMenuCommands(filter);
FilterMenuCommands(words);
}

FilteredFileList.ForEach(file =>
Expand Down Expand Up @@ -181,13 +211,11 @@ private void searchInTabs(string filter)
if (FrmSettings.Settings.GetBoolSetting(Settings.preferFilenameResults))
{
FilteredFileList = FileList.AsParallel()
.Where(e => words.All(word =>
e.FileName.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) >= 0))
.Where(e => MatchesAllWordsInFilter(e.FileName, words))
.ToList();

FileList.AsParallel()
.Where(e => words.All(word =>
e.FilePath.IndexOf(filter, StringComparison.CurrentCultureIgnoreCase) >= 0))
.Where(e => MatchesAllWordsInFilter(e.FilePath, words))
.ToList().ForEach(filePath =>
{
if (!FilteredFileList.Exists(file => file.FilePath.Equals(filePath.FilePath)))
Expand All @@ -200,10 +228,10 @@ private void searchInTabs(string filter)
else
{
FilteredFileList = FileList.AsParallel()
.Where(e => words.All(word =>
e.FilePath.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) >= 0 ||
e.FileName.IndexOf(word, StringComparison.CurrentCultureIgnoreCase) >= 0))
.ToList();
.Where(e =>
MatchesAllWordsInFilter(e.FilePath, words) ||
MatchesAllWordsInFilter(e.FileName, words)
).ToList();
}
}

Expand Down Expand Up @@ -236,11 +264,11 @@ private void searchInTabs(string filter)
}
}

private void FilterMenuCommands(string filter)
private void FilterMenuCommands(string[] filterWords)
{
foreach (var nppMenuCmd in notepad.MainMenuItems.AsParallel().Where(e => e.Value.IndexOf(filter,
StringComparison.CurrentCultureIgnoreCase) >= 0 ||
e.Value.Contains(filter)).ToList())
foreach (var nppMenuCmd in notepad.MainMenuItems.AsParallel().Where(e =>
MatchesAllWordsInFilter(e.Value, filterWords)
).ToList())
{
FilteredFileList.Add(new FileModel(
nppMenuCmd.Key.ToString(),
Expand Down
6 changes: 6 additions & 0 deletions NppNavigateTo/Forms/FrmSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private void frmSettings_Shown(object sender, EventArgs e)
numericUpDownMinGridWidth.Value = Settings.GetIntSetting(Settings.gridMinWidth);
checkBoxSearchInFolder.Checked = Settings.GetBoolSetting(Settings.searchInCurrentFolder);
checkBoxSearchInSubDirs.Checked = Settings.GetBoolSetting(Settings.searchInSubDirs);
numUpDownSecsBetweenDirectoryScans.Value = Settings.GetIntSetting(Settings.secondsBetweenDirectoryScans);
checkBoxSearchMenu.Checked = Settings.GetBoolSetting(Settings.searchMenuCommands);
checkBoxCleanSearch.Checked = Settings.GetBoolSetting(Settings.clearOnClose);
checkBoxKeepSelected.Checked = Settings.GetBoolSetting(Settings.selectFirstRowOnFilter);
Expand Down Expand Up @@ -333,5 +334,10 @@ private void buttonRowBackgroud_Click(object sender, EventArgs e)

RefreshDataGridStyles();
}

private void secondsBetweenDirectoryScans_ValueChanged(object sender, EventArgs e)
{
Settings.SetIntSetting(Settings.secondsBetweenDirectoryScans, (int)numUpDownSecsBetweenDirectoryScans.Value);
}
}
}
Loading

0 comments on commit d501926

Please sign in to comment.