-
Notifications
You must be signed in to change notification settings - Fork 1.4k
ColoredConsoleTarget - WholeWords should only highlight the word #5964
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
Conversation
WalkthroughAdds a private GenerateMatch helper to ConsoleWordHighlightingRule to compute trimmed match spans when WholeWords is enabled, replaces direct match constructions with it, tightens FindNextWordForHighlighting boundary checks, and adds a unit test covering delimiter-only Text with WholeWords. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Caller as Highlighting Logic
participant Rule as ConsoleWordHighlightingRule
participant Gen as GenerateMatch
Caller->>Rule: YieldWordsForHighlighting(needle, text)
Rule->>Rule: Find firstIndex / nextIndex via FindNextWordForHighlighting
alt match found
Rule->>Gen: GenerateMatch(needle, index)
Gen-->>Rule: (start, length) — trimmed if WholeWords
Rule-->>Caller: yield (start, length)
end
opt additional matches
Rule->>Gen: GenerateMatch(needle, nextIndex)
Gen-->>Rule: (start, length)
Rule-->>Caller: yield (start, length)
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
19b8ce3 to
0c35d8a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
src/NLog/Targets/ConsoleWordHighlightingRule.cs (3)
252-282: GenerateMatch: correct trimming; consider EOL symmetry and delimiter-only intentThe trimming logic is sound and produces the desired “word-only” highlight from a delimited needle.
Two follow-ups to consider:
- Symmetry with end-of-line: The current acceptance logic in FindNextWordForHighlighting accounts for start-of-line but not end-of-line when only one side has a delimiter. If you also want to support patterns like "Warn|" at end-of-message (WholeWords), see my suggestion on Lines 305-309.
- Delimiter-only needles: When the needle consists entirely of non-letter/digit characters, this returns the original span (highlights only delimiters). Verify that this is intentional with “WholeWords” semantics. If not, you might choose to skip these matches.
Performance (optional): GenerateMatch rescans the same needle each time it’s called. When there are many occurrences, precomputing trim offsets per needle can avoid repeated scans.
Do you want delimiter-only needles to be highlighted under WholeWords, or should they be skipped entirely? I can provide a targeted change and unit test if you want to change this behavior.
286-286: Consistent use of GenerateMatch; consider precomputing trim once per needleLGTM on switching all yields to GenerateMatch to ensure consistent trimming when WholeWords is enabled.
Optional improvement: precompute the left-trim count and core length once per needle and reuse them across calls to avoid rescanning in GenerateMatch for every match occurrence. If you’d like, I can propose a small helper (e.g., ComputeWordSpan) and wire it here.
Also applies to: 288-288, 295-295
305-309: WholeWords boundary: add end-of-line symmetry for one-sided delimitersThe added condition allows delimited needles like “|Warn|” (or “at ” at index 0) to pass WholeWords checks. To handle the symmetric case at the end of the haystack (e.g., “|Warn” at end-of-message), consider also treating end-of-line as a valid non-word boundary.
Suggested change:
- if (StringHelpers.IsWholeWord(haystack, needle, index) || ((index == 0 || !char.IsLetterOrDigit(needle[0])) && !char.IsLetterOrDigit(needle[needle.Length - 1]))) + var startBoundaryOk = (index == 0) || !char.IsLetterOrDigit(needle[0]); + var endBoundaryOk = (index + needle.Length == haystack.Length) || !char.IsLetterOrDigit(needle[needle.Length - 1]); + if (StringHelpers.IsWholeWord(haystack, needle, index) || (startBoundaryOk && endBoundaryOk)) return index;This keeps the current behavior for “|Warn|” and also supports start-only or end-only delimiter cases at the message boundaries.
If only “both-sides delimited” needles (like “|Warn|”) are intended, and one-sided cases should be ignored, please disregard this suggestion. Otherwise, I can add unit tests for start-only and end-only delimiter scenarios.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/NLog/Targets/ConsoleWordHighlightingRule.cs(2 hunks)tests/NLog.UnitTests/Targets/ColoredConsoleTargetTests.cs(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- tests/NLog.UnitTests/Targets/ColoredConsoleTargetTests.cs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: task-list-completed
- GitHub Check: task-list-completed
🔇 Additional comments (1)
src/NLog/Targets/ConsoleWordHighlightingRule.cs (1)
245-247: Single-match path now trims delimiters correctly via GenerateMatchGood replacement. Using GenerateMatch here ensures WholeWords highlights only the core word instead of the full needle.
|
|
Updated wiki: https://github.com/NLog/NLog/wiki/ColoredConsole-target |



Thus one can specify
text="|Warn|" wholeWords="true"and it will only highlightWarn-word when inside|