Skip to content

Commit

Permalink
Merge pull request #314 from AvaloniaUI/handle-invalid-regex
Browse files Browse the repository at this point in the history
Handle invalid SearchPatternException properly
  • Loading branch information
danipen authored Mar 3, 2023
2 parents 9333d9f + 97bfd5f commit 62895f5
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions src/AvaloniaEdit/Search/SearchPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,19 @@ private void UpdateSearch()
// only reset as long as there are results
// if no results are found, the "no matches found" message should not flicker.
// if results are found by the next run, the message will be hidden inside DoSearch ...
if (_renderer.CurrentResults.Any())
_messageView.IsVisible = false;
_strategy = SearchStrategyFactory.Create(SearchPattern ?? "", !MatchCase, WholeWords, UseRegex ? SearchMode.RegEx : SearchMode.Normal);
OnSearchOptionsChanged(new SearchOptionsChangedEventArgs(SearchPattern, MatchCase, UseRegex, WholeWords));
DoSearch(true);
try
{
if (_renderer.CurrentResults.Any())
_messageView.IsVisible = false;
_strategy = SearchStrategyFactory.Create(SearchPattern ?? "", !MatchCase, WholeWords, UseRegex ? SearchMode.RegEx : SearchMode.Normal);
OnSearchOptionsChanged(new SearchOptionsChangedEventArgs(SearchPattern, MatchCase, UseRegex, WholeWords));
DoSearch(true);
}
catch (SearchPatternException)
{
CleanSearchResults();
UpdateSearchLabel();
}
}

static SearchPanel()
Expand Down Expand Up @@ -278,15 +286,7 @@ private void ValidateSearchText()
if (_searchTextBox == null)
return;

try
{
UpdateSearch();
_validationError = null;
}
catch (SearchPatternException ex)
{
_validationError = ex.Message;
}
UpdateSearch();
}

/// <summary>
Expand Down Expand Up @@ -366,14 +366,13 @@ public void ReplaceAll()

private Panel _messageView;
private TextBlock _messageViewContent;
private string _validationError;

private void DoSearch(bool changeSelection)
{
if (IsClosed)
return;
_renderer.CurrentResults.Clear();
_currentSearchResultIndex = -1;

CleanSearchResults();

if (!string.IsNullOrEmpty(SearchPattern))
{
Expand All @@ -400,6 +399,12 @@ private void DoSearch(bool changeSelection)
_textArea.TextView.InvalidateLayer(KnownLayer.Selection);
}

void CleanSearchResults()
{
_renderer.CurrentResults.Clear();
_currentSearchResultIndex = -1;
}

void UpdateSearchLabel()
{
if (_messageView == null || _messageViewContent == null)
Expand Down Expand Up @@ -462,14 +467,6 @@ private void SearchLayerKeyDown(object sender, KeyEventArgs e)
{
FindNext();
}
if (_searchTextBox != null)
{
if (_validationError != null && _messageView != null && _messageViewContent != null)
{
_messageViewContent.Text = SR.SearchErrorText + " " + _validationError;
_messageView.IsVisible = true;
}
}
break;
case Key.Escape:
e.Handled = true;
Expand Down

0 comments on commit 62895f5

Please sign in to comment.