Skip to content

Commit 12469a9

Browse files
committed
add ctrl+backspace to delete word; optimizations
1. Ctrl+Backspace in the base form will now delete the word (sequence of non-whitespace, non-punctuation chars) ending at the cursor location. 2. Added optimizations to reduce the frequency of updating of the file list. 3. Added the ability to disable highlighting of the file list if the number of results is large (for better performance) 4. Made directory search more efficient.
1 parent 7355c3b commit 12469a9

File tree

12 files changed

+468
-81
lines changed

12 files changed

+468
-81
lines changed

NppNavigateTo/FormStyle.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Drawing;
44
using System.Runtime.InteropServices;
55
using System.Windows.Forms;
6+
using NppPluginNET;
67

78
namespace NavigateTo.Plugin.Namespace
89
{
@@ -25,8 +26,7 @@ public class FormStyle
2526
public static void ApplyStyle(Control ctrl, bool use_npp_style, bool isDark = false)
2627
{
2728
if (ctrl == null || ctrl.IsDisposed) return;
28-
int[] version = Main.notepad.GetNppVersion();
29-
if (version[0] < 8)
29+
if (!MiscUtils.nppVersionAtLeast8)
3030
use_npp_style = false; // trying to follow editor style looks weird for Notepad++ 7.3.3
3131
if (ctrl is Form form)
3232
{

NppNavigateTo/Forms/FrmNavigateTo.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ partial class FrmNavigateTo : Form
8383
/// </summary>
8484
public int minDirTreeSizeToWarn { get; set; } = 5000;
8585

86+
private bool tooManyResultsToHighlight { get; set; } = false;
87+
88+
private bool lastCharCannotChangeResults { get; set; } = false;
89+
8690
public FrmNavigateTo(IScintillaGateway editor, INotepadPPGateway notepad)
8791
{
8892
this.editor = editor;
@@ -180,6 +184,7 @@ public void FilterDataGrid(string filter)
180184
dataGridFileList.Rows.Clear();
181185
if (emptyFilter)
182186
{
187+
tooManyResultsToHighlight = FileList.Count >= FrmSettings.Settings.GetIntSetting(Settings.maxResultsHighlightingEnabled);
183188
FileList.ForEach(file =>
184189
{
185190
DataGridViewRow newRow = new DataGridViewRow();
@@ -198,6 +203,7 @@ public void FilterDataGrid(string filter)
198203
//backgroundWorker.CancelAsync(); // cancel the previous filtering
199204
//backgroundWorker.RunWorkerAsync(); // filter the file list (run BackgroundWorker_DoWork)
200205
FilterEverything(filter);
206+
tooManyResultsToHighlight = FilteredFileList.Count >= FrmSettings.Settings.GetIntSetting(Settings.maxResultsHighlightingEnabled);
201207

202208
FilteredFileList.ForEach(file =>
203209
{
@@ -251,7 +257,7 @@ public void FilterEverything(string filter)//BackgroundWorker_DoWork(object send
251257

252258
if (FrmSettings.Settings.GetBoolSetting(Settings.searchInCurrentFolder))
253259
{
254-
FilterCurrentDirectory(nonFuzzyFilterFunc/*, e*/);
260+
FilterCurrentDirectory();
255261
}
256262

257263
if (FrmSettings.Settings.GetBoolSetting(Settings.searchMenuCommands))
@@ -383,11 +389,12 @@ string[] SearchCurrentDirectory(DirectorySearchLevel searchLevel, long nextTimeT
383389
return filesInCurrentDirectory ?? new string[0];
384390
}
385391

386-
void FilterCurrentDirectory(Func<string, bool> filterFunc)
392+
void FilterCurrentDirectory()
387393
{
388394
string currentDirectory = notepad.GetCurrentFileDirectory();
389395
if (string.IsNullOrWhiteSpace(currentDirectory))
390396
return;
397+
Func<string, bool> filterFunc = Glob.CacheGlobFuncResultsForTopDirectory(currentDirectory, glob.globFunctions);
391398
bool userWantsSearchSubdirs = FrmSettings.Settings.GetBoolSetting(Settings.searchInSubDirs);
392399
DirectorySearchLevel searchLevel = userWantsSearchSubdirs
393400
? DirectorySearchLevel.RecurseSubdirs
@@ -511,8 +518,34 @@ private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
511518

512519
private void SearchComboBoxKeyDown(object sender, KeyEventArgs e)
513520
{
521+
lastCharCannotChangeResults = false;
514522
switch (e.KeyCode)
515523
{
524+
case Keys.Back:
525+
if (!e.Control)
526+
return;
527+
// Ctrl+Backspace: delete the word (sequence of non-whitespace, non-punctuation chars)
528+
// ending at the current cursor position.
529+
var text = searchComboBox.Text;
530+
int currentWordEnd = searchComboBox.SelectionStart;
531+
int currentWordStart = currentWordEnd - 1;
532+
for (; currentWordStart >= 0; currentWordStart--)
533+
{
534+
char c = text[currentWordStart];
535+
if (Char.IsPunctuation(c) || Char.IsWhiteSpace(c))
536+
break;
537+
}
538+
if (currentWordStart < currentWordEnd - 1)
539+
currentWordStart++;
540+
string textWithoutCurrentWord = currentWordStart > 0 ? text.Substring(0, currentWordStart) : "";
541+
textWithoutCurrentWord += currentWordEnd < text.Length ? text.Substring(currentWordEnd) : "";
542+
searchComboBox.Text = textWithoutCurrentWord;
543+
searchComboBox.SelectionStart = currentWordStart;
544+
searchComboBox.SelectionLength = 0;
545+
e.SuppressKeyPress = true;
546+
e.Handled = true;
547+
break;
548+
516549
case Keys.Down:
517550
e.Handled = NavigateGridDown(e.Shift);
518551
break;
@@ -541,6 +574,12 @@ private void SearchComboBoxKeyDown(object sender, KeyEventArgs e)
541574
e.Handled = true;
542575
e.SuppressKeyPress = true;
543576
break;
577+
578+
case Keys.Space:
579+
// adding space can't change the results
580+
// unless the space is part of a character class in a glob (e.g. "[ ]" matches literal whitespace)
581+
lastCharCannotChangeResults = searchComboBox.Text.IndexOf('[') == -1;
582+
break;
544583
}
545584
}
546585

@@ -617,6 +656,8 @@ private bool NavigateGridDown(bool isShiftPressed)
617656

618657
private void SearchComboBoxTextChanged(object sender, EventArgs e)
619658
{
659+
if (lastCharCannotChangeResults)
660+
return;
620661
int textLength = searchComboBox.Text.Length;
621662
bool emptyText = string.IsNullOrWhiteSpace(searchComboBox.Text);
622663
int minLength = FrmSettings.Settings.GetIntSetting(Settings.minTypeCharLimit);
@@ -716,8 +757,10 @@ private void dataGridFileList_Resize(object sender, EventArgs e)
716757

717758
private void dataGridFileList_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
718759
{
719-
if (e.Value == null) return;
720-
if (searchComboBox.Text.Length == 0) return;
760+
if (e.Value == null
761+
|| searchComboBox.Text.Length == 0
762+
|| tooManyResultsToHighlight)
763+
return;
721764

722765
if (e.RowIndex > -1 && e.ColumnIndex > -1)
723766
{

NppNavigateTo/Forms/FrmSettings.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ private void frmSettings_Shown(object sender, EventArgs e)
7575
comboBoxSortOrder.SelectedIndex = Settings.GetIntSetting(Settings.sortOrderAfterFilterBy);
7676
checkBoxFuzzySearch.Checked = Settings.GetBoolSetting(Settings.fuzzySearch);
7777
numericUpDownFuzzyness.Value = Settings.GetIntSetting(Settings.fuzzynessTolerance);
78+
maxResultsHighlightingEnabledUpDown.Value = Settings.GetIntSetting(Settings.maxResultsHighlightingEnabled);
7879

7980
DataGridViewRow newRow = new DataGridViewRow();
8081
newRow.CreateCells(dataGridFileListPreview);
@@ -164,6 +165,7 @@ private void RefreshDataGridStyles()
164165
dataGridViewCellStyle1.BackColor = Settings.GetColorSetting(Settings.rowBackgroundColor);
165166
dataGridViewCellStyle1.SelectionBackColor = Settings.GetColorSetting(Settings.gridSelectedRowBackground);
166167
dataGridViewCellStyle1.SelectionForeColor = Settings.GetColorSetting(Settings.gridSelectedRowForeground);
168+
// TODO: Add ability to change font
167169
dataGridFileListPreview.DefaultCellStyle = dataGridViewCellStyle1;
168170
dataGridFileListPreview.BackgroundColor = Settings.GetColorSetting(Settings.gridBackgroundColor);
169171
dataGridFileListPreview.BackColor = Settings.GetColorSetting(Settings.rowBackgroundColor);
@@ -263,6 +265,7 @@ private void buttonResetStyle_Click(object sender, EventArgs e)
263265
Settings.SetColorSetting(Settings.gridBackgroundColor, System.Drawing.SystemColors.AppWorkspace);
264266
Settings.SetColorSetting(Settings.highlightColorBackground, Color.Orange);
265267
Settings.SetColorSetting(Settings.rowBackgroundColor, Color.White);
268+
//Settings.SetIntSetting(Settings.fontSize, 8); // Only once we figure out how to properly change font size
266269
RefreshDataGridStyles();
267270
}
268271

@@ -350,5 +353,10 @@ private void secondsBetweenDirectoryScans_ValueChanged(object sender, EventArgs
350353
{
351354
Settings.SetIntSetting(Settings.secondsBetweenDirectoryScans, (int)numUpDownSecsBetweenDirectoryScans.Value);
352355
}
356+
357+
private void maxResultsHighlightingEnabled_ValueChanged(object sender, EventArgs e)
358+
{
359+
Settings.SetIntSetting(Settings.maxResultsHighlightingEnabled, (int)maxResultsHighlightingEnabledUpDown.Value);
360+
}
353361
}
354362
}

NppNavigateTo/Forms/FrmSettings.designer.cs

Lines changed: 45 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)