Skip to content

Commit fc10270

Browse files
authored
Fix #242 - RegEx matches in multiline text don't highlight property (#381)
1 parent 4cfcbba commit fc10270

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/dev/impl/DevToys/ViewModels/Tools/Text/RegEx/RegExToolViewModel.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,15 @@ await ThreadHelper.RunOnUIThreadAsync(() =>
241241
string? pattern = data.pattern.Trim('/');
242242

243243
var regex = new Regex(data.pattern, GetOptions());
244-
MatchCollection matches = regex.Matches(data.text.Replace("\r\n", "\r"));
244+
MatchCollection matches = regex.Matches(data.text);
245245

246246
foreach (Match match in matches)
247247
{
248+
int lineCount = CountLines(data.text, match.Index);
248249
spans.Add(
249250
new HighlightSpan()
250251
{
251-
StartIndex = match.Index,
252+
StartIndex = match.Index - lineCount,
252253
Length = match.Length,
253254
BackgroundColor = highlighterBackgroundColor,
254255
ForegroundColor = highlighterForegroundColor
@@ -311,5 +312,22 @@ private RegexOptions GetOptions()
311312

312313
return options;
313314
}
315+
316+
private int CountLines(string input, int maxLength)
317+
{
318+
int lines = 0;
319+
int i = 0;
320+
while (i > -1 && i < maxLength)
321+
{
322+
i = input.IndexOf("\r\n", startIndex: i);
323+
if (i > -1 && i < maxLength)
324+
{
325+
lines++;
326+
i++;
327+
}
328+
}
329+
330+
return lines;
331+
}
314332
}
315333
}

0 commit comments

Comments
 (0)