add regex highlight

This commit is contained in:
2021-10-24 19:47:30 +02:00
parent eab422a6ce
commit 6e831c66be
7 changed files with 116 additions and 50 deletions

42
highlight_regex.py Normal file
View File

@@ -0,0 +1,42 @@
from typing import Optional
from highlight import Highlight
from highlighted_range import HighlightedRange
from line import Line
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
from settings import Settings
from typing import List
import re
class HighlightRegex(Highlight):
def __init__(self, regex: str, brush: QBrush = QBrush(), pen: QPen = Qt.PenStyle.NoPen, brush_full_line=QBrush()):
self.regex = regex
self.brush = brush
self.pen = pen
self.brush_full_line = brush_full_line
def compute_highlight(self, line: Line) -> Optional[List[HighlightedRange]]:
result = []
#print("execute regex: %s in %s" % (self.regex, line.line()))
match_iter = re.finditer(self.regex, line.line())
for match in match_iter:
#print("%s" % match)
group0 = match.group(0)
start = match.start(0)
end = match.end(0)
#print("regex: %s" % (group0))
result.append(HighlightedRange(
start,
end-start,
highlight_full_line=True,
brush=self.brush,
pen=self.pen,
brush_full_line=self.brush_full_line
))
return result