Skip to content

Commit

Permalink
Merge pull request #57 from molsonkiko/ctrl_back_to_delete_word
Browse files Browse the repository at this point in the history
add ctrl+backspace to delete word, performance optimizations
  • Loading branch information
young-developer authored Aug 23, 2023
2 parents 7355c3b + 12469a9 commit e46e31a
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 81 deletions.
4 changes: 2 additions & 2 deletions NppNavigateTo/FormStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using NppPluginNET;

namespace NavigateTo.Plugin.Namespace
{
Expand All @@ -25,8 +26,7 @@ public class FormStyle
public static void ApplyStyle(Control ctrl, bool use_npp_style, bool isDark = false)
{
if (ctrl == null || ctrl.IsDisposed) return;
int[] version = Main.notepad.GetNppVersion();
if (version[0] < 8)
if (!MiscUtils.nppVersionAtLeast8)
use_npp_style = false; // trying to follow editor style looks weird for Notepad++ 7.3.3
if (ctrl is Form form)
{
Expand Down
51 changes: 47 additions & 4 deletions NppNavigateTo/Forms/FrmNavigateTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ partial class FrmNavigateTo : Form
/// </summary>
public int minDirTreeSizeToWarn { get; set; } = 5000;

private bool tooManyResultsToHighlight { get; set; } = false;

private bool lastCharCannotChangeResults { get; set; } = false;

public FrmNavigateTo(IScintillaGateway editor, INotepadPPGateway notepad)
{
this.editor = editor;
Expand Down Expand Up @@ -180,6 +184,7 @@ public void FilterDataGrid(string filter)
dataGridFileList.Rows.Clear();
if (emptyFilter)
{
tooManyResultsToHighlight = FileList.Count >= FrmSettings.Settings.GetIntSetting(Settings.maxResultsHighlightingEnabled);
FileList.ForEach(file =>
{
DataGridViewRow newRow = new DataGridViewRow();
Expand All @@ -198,6 +203,7 @@ public void FilterDataGrid(string filter)
//backgroundWorker.CancelAsync(); // cancel the previous filtering
//backgroundWorker.RunWorkerAsync(); // filter the file list (run BackgroundWorker_DoWork)
FilterEverything(filter);
tooManyResultsToHighlight = FilteredFileList.Count >= FrmSettings.Settings.GetIntSetting(Settings.maxResultsHighlightingEnabled);

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

if (FrmSettings.Settings.GetBoolSetting(Settings.searchInCurrentFolder))
{
FilterCurrentDirectory(nonFuzzyFilterFunc/*, e*/);
FilterCurrentDirectory();
}

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

void FilterCurrentDirectory(Func<string, bool> filterFunc)
void FilterCurrentDirectory()
{
string currentDirectory = notepad.GetCurrentFileDirectory();
if (string.IsNullOrWhiteSpace(currentDirectory))
return;
Func<string, bool> filterFunc = Glob.CacheGlobFuncResultsForTopDirectory(currentDirectory, glob.globFunctions);
bool userWantsSearchSubdirs = FrmSettings.Settings.GetBoolSetting(Settings.searchInSubDirs);
DirectorySearchLevel searchLevel = userWantsSearchSubdirs
? DirectorySearchLevel.RecurseSubdirs
Expand Down Expand Up @@ -511,8 +518,34 @@ private void dataGridView1_MouseClick(object sender, MouseEventArgs e)

private void SearchComboBoxKeyDown(object sender, KeyEventArgs e)
{
lastCharCannotChangeResults = false;
switch (e.KeyCode)
{
case Keys.Back:
if (!e.Control)
return;
// Ctrl+Backspace: delete the word (sequence of non-whitespace, non-punctuation chars)
// ending at the current cursor position.
var text = searchComboBox.Text;
int currentWordEnd = searchComboBox.SelectionStart;
int currentWordStart = currentWordEnd - 1;
for (; currentWordStart >= 0; currentWordStart--)
{
char c = text[currentWordStart];
if (Char.IsPunctuation(c) || Char.IsWhiteSpace(c))
break;
}
if (currentWordStart < currentWordEnd - 1)
currentWordStart++;
string textWithoutCurrentWord = currentWordStart > 0 ? text.Substring(0, currentWordStart) : "";
textWithoutCurrentWord += currentWordEnd < text.Length ? text.Substring(currentWordEnd) : "";
searchComboBox.Text = textWithoutCurrentWord;
searchComboBox.SelectionStart = currentWordStart;
searchComboBox.SelectionLength = 0;
e.SuppressKeyPress = true;
e.Handled = true;
break;

case Keys.Down:
e.Handled = NavigateGridDown(e.Shift);
break;
Expand Down Expand Up @@ -541,6 +574,12 @@ private void SearchComboBoxKeyDown(object sender, KeyEventArgs e)
e.Handled = true;
e.SuppressKeyPress = true;
break;

case Keys.Space:
// adding space can't change the results
// unless the space is part of a character class in a glob (e.g. "[ ]" matches literal whitespace)
lastCharCannotChangeResults = searchComboBox.Text.IndexOf('[') == -1;
break;
}
}

Expand Down Expand Up @@ -617,6 +656,8 @@ private bool NavigateGridDown(bool isShiftPressed)

private void SearchComboBoxTextChanged(object sender, EventArgs e)
{
if (lastCharCannotChangeResults)
return;
int textLength = searchComboBox.Text.Length;
bool emptyText = string.IsNullOrWhiteSpace(searchComboBox.Text);
int minLength = FrmSettings.Settings.GetIntSetting(Settings.minTypeCharLimit);
Expand Down Expand Up @@ -716,8 +757,10 @@ private void dataGridFileList_Resize(object sender, EventArgs e)

private void dataGridFileList_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.Value == null) return;
if (searchComboBox.Text.Length == 0) return;
if (e.Value == null
|| searchComboBox.Text.Length == 0
|| tooManyResultsToHighlight)
return;

if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
Expand Down
8 changes: 8 additions & 0 deletions NppNavigateTo/Forms/FrmSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ private void frmSettings_Shown(object sender, EventArgs e)
comboBoxSortOrder.SelectedIndex = Settings.GetIntSetting(Settings.sortOrderAfterFilterBy);
checkBoxFuzzySearch.Checked = Settings.GetBoolSetting(Settings.fuzzySearch);
numericUpDownFuzzyness.Value = Settings.GetIntSetting(Settings.fuzzynessTolerance);
maxResultsHighlightingEnabledUpDown.Value = Settings.GetIntSetting(Settings.maxResultsHighlightingEnabled);

DataGridViewRow newRow = new DataGridViewRow();
newRow.CreateCells(dataGridFileListPreview);
Expand Down Expand Up @@ -164,6 +165,7 @@ private void RefreshDataGridStyles()
dataGridViewCellStyle1.BackColor = Settings.GetColorSetting(Settings.rowBackgroundColor);
dataGridViewCellStyle1.SelectionBackColor = Settings.GetColorSetting(Settings.gridSelectedRowBackground);
dataGridViewCellStyle1.SelectionForeColor = Settings.GetColorSetting(Settings.gridSelectedRowForeground);
// TODO: Add ability to change font
dataGridFileListPreview.DefaultCellStyle = dataGridViewCellStyle1;
dataGridFileListPreview.BackgroundColor = Settings.GetColorSetting(Settings.gridBackgroundColor);
dataGridFileListPreview.BackColor = Settings.GetColorSetting(Settings.rowBackgroundColor);
Expand Down Expand Up @@ -263,6 +265,7 @@ private void buttonResetStyle_Click(object sender, EventArgs e)
Settings.SetColorSetting(Settings.gridBackgroundColor, System.Drawing.SystemColors.AppWorkspace);
Settings.SetColorSetting(Settings.highlightColorBackground, Color.Orange);
Settings.SetColorSetting(Settings.rowBackgroundColor, Color.White);
//Settings.SetIntSetting(Settings.fontSize, 8); // Only once we figure out how to properly change font size
RefreshDataGridStyles();
}

Expand Down Expand Up @@ -350,5 +353,10 @@ private void secondsBetweenDirectoryScans_ValueChanged(object sender, EventArgs
{
Settings.SetIntSetting(Settings.secondsBetweenDirectoryScans, (int)numUpDownSecsBetweenDirectoryScans.Value);
}

private void maxResultsHighlightingEnabled_ValueChanged(object sender, EventArgs e)
{
Settings.SetIntSetting(Settings.maxResultsHighlightingEnabled, (int)maxResultsHighlightingEnabledUpDown.Value);
}
}
}
60 changes: 45 additions & 15 deletions NppNavigateTo/Forms/FrmSettings.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e46e31a

Please sign in to comment.