Skip to content

Commit

Permalink
Added event which triggers when the search wraps the end/start of the…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
Carsten Franke committed Nov 29, 2024
1 parent a1c9299 commit 51e377f
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions ICSharpCode.AvalonEdit/Search/SearchPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class SearchPanel : Control
/// </summary>
public static int DelayBeforeSearch { get; set; } = 250;

/// <summary>
/// Event that occurs if the search wrapped the end or beginning of the file
/// </summary>
public static event EventHandler SearchWrapped;

TextArea textArea;
SearchInputHandler handler;
TextDocument currentDocument;
Expand Down Expand Up @@ -97,7 +102,7 @@ public bool MatchCase
/// <summary>
/// Gets/sets whether the search pattern should only match whole words.
/// </summary>
public bool WholeWords
public bool WholeWords
{
get { return (bool)GetValue(WholeWordsProperty); }
set { SetValue(WholeWordsProperty, value); }
Expand Down Expand Up @@ -196,7 +201,7 @@ private static void MarkerCornerRadiusChangedCallback(DependencyObject d, Depend
/// <summary>
/// Gets/sets the localization for the SearchPanel.
/// </summary>
public Localization Localization
public Localization Localization
{
get { return (Localization)GetValue(LocalizationProperty); }
set { SetValue(LocalizationProperty, value); }
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -400,7 +407,7 @@ void DoSearch(bool changeSelection)
return;

lastChangeSelection = changeSelection;
if (typingTimer == null)
if (typingTimer == null)
{
typingTimer = new DispatcherTimer
{
Expand All @@ -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;
}
Expand All @@ -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();
}
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 51e377f

Please sign in to comment.