From 755408abe0a5ee5da13d17466d24743768515188 Mon Sep 17 00:00:00 2001 From: Etienne BAUDOUX Date: Sun, 13 Feb 2022 14:54:34 -0800 Subject: [PATCH] Fix #242 - RegEx matches in multiline text don't highlight property --- .../Tools/Text/RegEx/RegExToolViewModel.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/dev/impl/DevToys/ViewModels/Tools/Text/RegEx/RegExToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/Text/RegEx/RegExToolViewModel.cs index de5d12bbbe..1acad443bb 100644 --- a/src/dev/impl/DevToys/ViewModels/Tools/Text/RegEx/RegExToolViewModel.cs +++ b/src/dev/impl/DevToys/ViewModels/Tools/Text/RegEx/RegExToolViewModel.cs @@ -241,14 +241,15 @@ await ThreadHelper.RunOnUIThreadAsync(() => string? pattern = data.pattern.Trim('/'); var regex = new Regex(data.pattern, GetOptions()); - MatchCollection matches = regex.Matches(data.text.Replace("\r\n", "\r")); + MatchCollection matches = regex.Matches(data.text); foreach (Match match in matches) { + int lineCount = CountLines(data.text, match.Index); spans.Add( new HighlightSpan() { - StartIndex = match.Index, + StartIndex = match.Index - lineCount, Length = match.Length, BackgroundColor = highlighterBackgroundColor, ForegroundColor = highlighterForegroundColor @@ -311,5 +312,22 @@ private RegexOptions GetOptions() return options; } + + private int CountLines(string input, int maxLength) + { + int lines = 0; + int i = 0; + while (i > -1 && i < maxLength) + { + i = input.IndexOf("\r\n", startIndex: i); + if (i > -1 && i < maxLength) + { + lines++; + i++; + } + } + + return lines; + } } }