Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 1 addition & 35 deletions src/LogExpert.Core/Classes/Bookmark/HighlightBookmarkScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static (bool SetBookmark, string BookmarkComment, string SourceHighlight
var bookmarkCommentBuilder = new StringBuilder();
var sourceHighlightText = string.Empty;

foreach (var entry in bookmarkEntries.Where(entry => CheckHighlightEntryMatch(entry, line)))
foreach (var entry in bookmarkEntries.Where(entry => HighlightEvaluator.IsMatch(entry, line)))
{
setBookmark = true;
sourceHighlightText = entry.SearchText;
Expand All @@ -98,40 +98,6 @@ private static (bool SetBookmark, string BookmarkComment, string SourceHighlight
return (setBookmark, bookmarkCommentBuilder.ToString().TrimEnd('\r', '\n'), sourceHighlightText);
}

/// <summary>
/// Matches a highlight entry against a line. Replicates the logic from LogWindow.CheckHighlightEntryMatch so the
/// scanner works identically to the existing tail-mode matching.
/// </summary>
private static bool CheckHighlightEntryMatch (HighlightEntry entry, ITextValueMemory column)
{
if (entry.IsRegex)
{
if (entry.Regex.IsMatch(column.Text.ToString()))
{
return true;
}
}
else
{
if (entry.IsCaseSensitive)
{
if (column.Text.Span.Contains(entry.SearchText.AsSpan(), StringComparison.Ordinal))
{
return true;
}
}
else
{
if (column.Text.Span.Contains(entry.SearchText.AsSpan(), StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}

return false;
}

/// <summary>
/// Resolves the bookmark comment template using ParamParser, matching SetBookmarkFromTrigger behavior.
/// </summary>
Expand Down
111 changes: 111 additions & 0 deletions src/LogExpert.Core/Classes/Highlight/HighlightEvaluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Text;

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 bookmarkCommentBuilder = new StringBuilder();

foreach (var entry in matchingEntries)
{
if (entry.IsLedSwitch)
{
suppressLed = true;
}

if (entry.IsSetBookmark)
{
setBookmark = true;
if (!string.IsNullOrEmpty(entry.BookmarkComment))
{

_ = bookmarkCommentBuilder.Append(entry.BookmarkComment);
_ = bookmarkCommentBuilder.Append("\r\n");
}
}

if (entry.IsStopTail)
{
stopTail = true;
}
}

var bookmarkComment = bookmarkCommentBuilder.ToString().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 src/LogExpert.Tests/Highlight/HighlightEvaluatorTests.cs
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));
}
}
Loading