add support for regex groups

If a filter regex contains groups, then we only highlight the group matches.
This commit is contained in:
2023-01-29 09:26:04 +01:00
parent d47949fdca
commit ee514dc305

View File

@@ -64,19 +64,21 @@ class HighlightRegex(Highlight):
match_iter = re.finditer(self.regex, line.line())
for match in match_iter:
# print("%s" % match)
start_char = match.start(0)
end_char = match.end(0)
start_column = line.char_to_column(start_char)
end_column = line.char_to_column(end_char)
result.append(HighlightedRange(
start_column,
end_column - start_column,
highlight_full_line=True,
brush=self._brush_hit,
brush_full_line=self._brush_line
))
# if the regex has groups then skip the first, it represents the whole match,
# but we only want to highlight the groups
first_group = 1 if len(match.groups()) > 0 else 0
for i in range(first_group, len(match.groups()) + 1):
start_column = line.char_to_column(match.start(i))
end_column = line.char_to_column(match.end(i))
# print(f"highlight: {start_column}:{end_column} - {match.group(i)}")
result.append(HighlightedRange(
start_column,
end_column - start_column,
highlight_full_line=True,
brush=self._brush_hit,
brush_full_line=self._brush_line
))
return result