-
Notifications
You must be signed in to change notification settings - Fork 184
Extract highlight match/trigger logic into Core HighlightEvaluator #643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68c5dab
Extract highlight match/trigger logic into Core HighlightEvaluator
0fac8b8
chore: update plugin hashes [skip ci]
github-actions[bot] fb9cf54
update
4a18ae0
Merge branch 'highlight-evaluator-extraction' of https://github.com/L…
0f916a7
chore: update plugin hashes [skip ci]
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/LogExpert.Core/Classes/Highlight/HighlightEvaluator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| using ColumnizerLib; | ||
|
|
||
| namespace LogExpert.Core.Classes.Highlight; | ||
|
|
||
| /// <summary> | ||
| /// Pure evaluation of <see cref="HighlightEntry"/> rules against a log line: which entries match and | ||
| /// which trigger actions their matches imply. This is the single home of highlight-match semantics, | ||
| /// shared by the tail trigger path (<c>LogWindow.CheckFilterAndHighlight</c>) and the bulk | ||
| /// <see cref="Bookmark.HighlightBookmarkScanner"/>. | ||
| /// <para> | ||
| /// It reports the <em>decision</em> only. Side-effecting triggers (Audio Alert, Set Bookmark, | ||
| /// Stop Tail) are fired by the caller, so the "Audio Alert fires only on the tail path" invariant | ||
| /// stays at the call site and cannot leak onto bulk/paint paths. | ||
| /// </para> | ||
| /// </summary> | ||
| public static class HighlightEvaluator | ||
| { | ||
| /// <summary> | ||
| /// Returns whether a single <see cref="HighlightEntry"/> matches the given line. | ||
| /// </summary> | ||
| public static bool IsMatch (HighlightEntry entry, ITextValueMemory line) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(entry); | ||
| ArgumentNullException.ThrowIfNull(line); | ||
|
|
||
| if (entry.IsRegex) | ||
| { | ||
| return entry.Regex.IsMatch(line.Text.ToString()); | ||
| } | ||
|
|
||
| var comparison = entry.IsCaseSensitive | ||
| ? StringComparison.Ordinal | ||
| : StringComparison.OrdinalIgnoreCase; | ||
|
|
||
| return line.Text.Span.Contains(entry.SearchText.AsSpan(), comparison); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns every entry in <paramref name="entries"/> that matches the line, preserving order. | ||
| /// A null line matches nothing. | ||
| /// </summary> | ||
| public static IList<HighlightEntry> FindMatchingEntries (IEnumerable<HighlightEntry> entries, ITextValueMemory line) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(entries); | ||
|
|
||
| List<HighlightEntry> result = []; | ||
| if (line == null) | ||
| { | ||
| return result; | ||
| } | ||
|
|
||
| foreach (var entry in entries.Where(e => IsMatch(e, line))) | ||
| { | ||
| result.Add(entry); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Classifies the non-plugin trigger actions implied by a set of matching entries: whether to | ||
| /// suppress the dirty LED, stop tailing, set a bookmark, and the concatenated bookmark comment. | ||
| /// Plugin and Audio Alert triggers are intentionally not handled here — they are fired by the | ||
| /// caller. | ||
| /// </summary> | ||
| public static HighlightActions GetTriggerActions (IEnumerable<HighlightEntry> matchingEntries) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(matchingEntries); | ||
|
|
||
| var suppressLed = false; | ||
| var stopTail = false; | ||
| var setBookmark = false; | ||
| var bookmarkComment = string.Empty; | ||
|
|
||
| foreach (var entry in matchingEntries) | ||
| { | ||
| if (entry.IsLedSwitch) | ||
| { | ||
| suppressLed = true; | ||
| } | ||
|
|
||
| if (entry.IsSetBookmark) | ||
| { | ||
| setBookmark = true; | ||
| if (!string.IsNullOrEmpty(entry.BookmarkComment)) | ||
| { | ||
| bookmarkComment += entry.BookmarkComment + "\r\n"; | ||
| } | ||
| } | ||
|
|
||
| if (entry.IsStopTail) | ||
| { | ||
| stopTail = true; | ||
| } | ||
| } | ||
|
|
||
| bookmarkComment = bookmarkComment.TrimEnd(['\r', '\n']); | ||
|
|
||
| return new HighlightActions(suppressLed, stopTail, setBookmark, bookmarkComment); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The non-plugin trigger decision for a line: which LED/tail/bookmark side effects its matching | ||
| /// entries imply. Carries no side effects of its own — the caller acts on it. | ||
| /// </summary> | ||
| public readonly record struct HighlightActions (bool SuppressLed, bool StopTail, bool SetBookmark, string BookmarkComment); | ||
138 changes: 138 additions & 0 deletions
138
src/LogExpert.Tests/Highlight/HighlightEvaluatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| using ColumnizerLib; | ||
|
|
||
| using LogExpert.Core.Classes.Highlight; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| namespace LogExpert.Tests.Highlight; | ||
|
|
||
| [TestFixture] | ||
| public class HighlightEvaluatorTests | ||
| { | ||
| #region Helper | ||
|
|
||
| private sealed class TestLine (string text) : ITextValueMemory | ||
| { | ||
| public ReadOnlyMemory<char> Text => text.AsMemory(); | ||
| } | ||
|
|
||
| private static ITextValueMemory Line (string text) => new TestLine(text); | ||
|
|
||
| #endregion | ||
|
|
||
| [Test] | ||
| public void IsMatch_PlainSubstring_MatchesCaseInsensitiveByDefault () | ||
| { | ||
| var entry = new HighlightEntry { SearchText = "error" }; | ||
|
|
||
| Assert.That(HighlightEvaluator.IsMatch(entry, Line("Something ERROR happened")), Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public void IsMatch_CaseSensitive_RespectsCase () | ||
| { | ||
| var entry = new HighlightEntry { SearchText = "error", IsCaseSensitive = true }; | ||
|
|
||
| Assert.That(HighlightEvaluator.IsMatch(entry, Line("ERROR happened")), Is.False); | ||
| Assert.That(HighlightEvaluator.IsMatch(entry, Line("an error here")), Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public void IsMatch_Regex_Matches () | ||
| { | ||
| var entry = new HighlightEntry { SearchText = "ERR\\w+", IsRegex = true }; | ||
|
|
||
| Assert.That(HighlightEvaluator.IsMatch(entry, Line("ERROR happened")), Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public void IsMatch_NoSubstring_ReturnsFalse () | ||
| { | ||
| var entry = new HighlightEntry { SearchText = "FATAL" }; | ||
|
|
||
| Assert.That(HighlightEvaluator.IsMatch(entry, Line("just an info line")), Is.False); | ||
| } | ||
|
|
||
| [Test] | ||
| public void FindMatchingEntries_ReturnsOnlyMatches_InOrder () | ||
| { | ||
| var error = new HighlightEntry { SearchText = "error" }; | ||
| var warn = new HighlightEntry { SearchText = "warn" }; | ||
| var fatal = new HighlightEntry { SearchText = "fatal" }; | ||
| var entries = new[] { error, warn, fatal }; | ||
|
|
||
| var result = HighlightEvaluator.FindMatchingEntries(entries, Line("warn: an error occurred")); | ||
|
|
||
| Assert.That(result, Is.EqualTo(new[] { error, warn })); | ||
| } | ||
|
|
||
| [Test] | ||
| public void FindMatchingEntries_NullLine_ReturnsEmpty () | ||
| { | ||
| var entries = new[] { new HighlightEntry { SearchText = "error" } }; | ||
|
|
||
| var result = HighlightEvaluator.FindMatchingEntries(entries, null); | ||
|
|
||
| Assert.That(result, Is.Empty); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetTriggerActions_EmptyList_AllFalse () | ||
| { | ||
| var actions = HighlightEvaluator.GetTriggerActions([]); | ||
|
|
||
| Assert.That(actions.SuppressLed, Is.False); | ||
| Assert.That(actions.StopTail, Is.False); | ||
| Assert.That(actions.SetBookmark, Is.False); | ||
| Assert.That(actions.BookmarkComment, Is.Empty); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetTriggerActions_AggregatesFlagsAcrossEntries () | ||
| { | ||
| var matching = new[] | ||
| { | ||
| new HighlightEntry { IsLedSwitch = true }, | ||
| new HighlightEntry { IsStopTail = true }, | ||
| new HighlightEntry { IsSetBookmark = true } | ||
| }; | ||
|
|
||
| var actions = HighlightEvaluator.GetTriggerActions(matching); | ||
|
|
||
| Assert.That(actions.SuppressLed, Is.True); | ||
| Assert.That(actions.StopTail, Is.True); | ||
| Assert.That(actions.SetBookmark, Is.True); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetTriggerActions_JoinsBookmarkComments_WithCrlf_TrimmingTrailing () | ||
| { | ||
| var matching = new[] | ||
| { | ||
| new HighlightEntry { IsSetBookmark = true, BookmarkComment = "first" }, | ||
| new HighlightEntry { IsSetBookmark = true, BookmarkComment = "second" } | ||
| }; | ||
|
|
||
| var actions = HighlightEvaluator.GetTriggerActions(matching); | ||
|
|
||
| Assert.That(actions.BookmarkComment, Is.EqualTo("first\r\nsecond")); | ||
| } | ||
|
|
||
| [Test] | ||
| public void IsMatch_NullEntry_Throws () | ||
| { | ||
| _ = Assert.Throws<ArgumentNullException>(() => HighlightEvaluator.IsMatch(null, Line("x"))); | ||
| } | ||
|
|
||
| [Test] | ||
| public void FindMatchingEntries_NullEntries_Throws () | ||
| { | ||
| _ = Assert.Throws<ArgumentNullException>(() => HighlightEvaluator.FindMatchingEntries(null, Line("x"))); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GetTriggerActions_NullList_Throws () | ||
| { | ||
| _ = Assert.Throws<ArgumentNullException>(() => HighlightEvaluator.GetTriggerActions(null)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.