From 51e377fc6c888fb91453202228cf0bfd8323d2fe Mon Sep 17 00:00:00 2001 From: Carsten Franke Date: Fri, 29 Nov 2024 08:50:09 +0100 Subject: [PATCH] Added event which triggers when the search wraps the end/start of the file --- ICSharpCode.AvalonEdit/Search/SearchPanel.cs | 34 +++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/ICSharpCode.AvalonEdit/Search/SearchPanel.cs b/ICSharpCode.AvalonEdit/Search/SearchPanel.cs index 51bce88e..1051126b 100644 --- a/ICSharpCode.AvalonEdit/Search/SearchPanel.cs +++ b/ICSharpCode.AvalonEdit/Search/SearchPanel.cs @@ -42,6 +42,11 @@ public class SearchPanel : Control /// public static int DelayBeforeSearch { get; set; } = 250; + /// + /// Event that occurs if the search wrapped the end or beginning of the file + /// + public static event EventHandler SearchWrapped; + TextArea textArea; SearchInputHandler handler; TextDocument currentDocument; @@ -97,7 +102,7 @@ public bool MatchCase /// /// Gets/sets whether the search pattern should only match whole words. /// - public bool WholeWords + public bool WholeWords { get { return (bool)GetValue(WholeWordsProperty); } set { SetValue(WholeWordsProperty, value); } @@ -196,7 +201,7 @@ private static void MarkerCornerRadiusChangedCallback(DependencyObject d, Depend /// /// Gets/sets the localization for the SearchPanel. /// - public Localization Localization + public Localization Localization { get { return (Localization)GetValue(LocalizationProperty); } set { SetValue(LocalizationProperty, value); } @@ -370,9 +375,10 @@ public void FindNext() SearchResult result = renderer.CurrentResults.FindFirstSegmentWithStartAfter(textArea.Caret.Offset + 1); if (result == null) result = renderer.CurrentResults.FirstSegment; - if (result != null) + if (result != null) { - SelectResult(result); + var s = Selection.Create(textArea, result.StartOffset, result.EndOffset); + SelectResult(result, textArea.Caret.Line >= s.StartPosition.Line); } } @@ -388,7 +394,8 @@ public void FindPrevious() result = renderer.CurrentResults.LastSegment; if (result != null) { - SelectResult(result); + var s = Selection.Create(textArea, result.StartOffset, result.EndOffset); + SelectResult(result, textArea.Caret.Line <= s.StartPosition.Line); } } @@ -400,7 +407,7 @@ void DoSearch(bool changeSelection) return; lastChangeSelection = changeSelection; - if (typingTimer == null) + if (typingTimer == null) { typingTimer = new DispatcherTimer { @@ -416,7 +423,7 @@ void DoSearch(bool changeSelection) private void handleTypingTimerTimeout(object sender, EventArgs e) { var timer = sender as DispatcherTimer; // WPF - if (timer == null) + if (timer == null) { return; } @@ -427,7 +434,7 @@ private void handleTypingTimerTimeout(object sender, EventArgs e) if (!string.IsNullOrEmpty(SearchPattern)) { int offset = textArea.Caret.Offset; - if (changeSelection) + if (changeSelection) { textArea.ClearSelection(); } @@ -436,7 +443,7 @@ private void handleTypingTimerTimeout(object sender, EventArgs e) { if (changeSelection && result.StartOffset >= offset) { - SelectResult(result); + SelectResult(result, false); changeSelection = false; } renderer.CurrentResults.Add(result); @@ -446,7 +453,7 @@ private void handleTypingTimerTimeout(object sender, EventArgs e) messageView.IsOpen = true; messageView.Content = Localization.NoMatchesFoundText; messageView.PlacementTarget = searchTextBox; - } + } else messageView.IsOpen = false; } @@ -455,13 +462,16 @@ private void handleTypingTimerTimeout(object sender, EventArgs e) timer.Stop(); } - void SelectResult(SearchResult result) + void SelectResult(SearchResult result, bool searchWrapped) { textArea.Caret.Offset = result.StartOffset; textArea.Selection = Selection.Create(textArea, result.StartOffset, result.EndOffset); textArea.Caret.BringCaretToView(); // show caret even if the editor does not have the Keyboard Focus textArea.Caret.Show(); + if (searchWrapped) { + SearchWrapped?.Invoke(this, EventArgs.Empty); + } } void SearchLayerKeyDown(object sender, KeyEventArgs e) @@ -474,7 +484,7 @@ void SearchLayerKeyDown(object sender, KeyEventArgs e) FindPrevious(); else FindNext(); - if (searchTextBox != null) + if (searchTextBox != null) { var error = Validation.GetErrors(searchTextBox).FirstOrDefault(); if (error != null)