41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
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: re.Pattern, 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)
|
|
start = match.start(0)
|
|
end = match.end(0)
|
|
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 |