33"""
44
55
6- from Qt import QtCore , QtGui , QtWidgets
6+ from PySide6 import QtGui
7+ from PySide6 .QtCore import QRegularExpression
78
89
910def format (color , style = '' ):
@@ -70,8 +71,9 @@ def __init__(self, parent=None):
7071 super (PythonHighlighter , self ).__init__ (parent )
7172
7273 # Multi-line strings (expression, flag, style)
73- self .tri_single = (QtCore .QRegExp ("'''" ), 1 , STYLES ['string2' ])
74- self .tri_double = (QtCore .QRegExp ('"""' ), 2 , STYLES ['string2' ])
74+
75+ self .tri_single = (QRegularExpression ("'''" ), 1 , STYLES ['string2' ])
76+ self .tri_double = (QRegularExpression ('"""' ), 2 , STYLES ['string2' ])
7577
7678 rules = []
7779
@@ -108,7 +110,7 @@ def __init__(self, parent=None):
108110 ]
109111
110112 # Build a QRegExp for each pattern
111- self .rules = [(QtCore . QRegExp (pat ), index , fmt )
113+ self .rules = [(QRegularExpression (pat ), index , fmt )
112114 for (pat , index , fmt ) in rules ]
113115
114116 def highlightBlock (self , text ):
@@ -118,32 +120,41 @@ def highlightBlock(self, text):
118120 self .tripleQuoutesWithinStrings = []
119121 # Do other syntax formatting
120122 for expression , nth , format in self .rules :
121- index = expression .indexIn (text , 0 )
122- if index >= 0 :
123- # if there is a string we check
124- # if there are some triple quotes within the string
125- # they will be ignored if they are matched again
126- if expression .pattern () in [r'"[^"\\]*(\\.[^"\\]*)*"' , r"'[^'\\]*(\\.[^'\\]*)*'" ]:
127- innerIndex = self .tri_single [0 ].indexIn (text , index + 1 )
128- if innerIndex == - 1 :
129- innerIndex = self .tri_double [0 ].indexIn (text , index + 1 )
130-
131- if innerIndex != - 1 :
132- tripleQuoteIndexes = range (innerIndex , innerIndex + 3 )
133- self .tripleQuoutesWithinStrings .extend (tripleQuoteIndexes )
134-
135- while index >= 0 :
136- # skipping triple quotes within strings
137- if index in self .tripleQuoutesWithinStrings :
138- index += 1
139- expression .indexIn (text , index )
140- continue
141-
142- # We actually want the index of the nth match
143- index = expression .pos (nth )
144- length = len (expression .cap (nth ))
145- self .setFormat (index , length , format )
146- index = expression .indexIn (text , index + length )
123+ match = expression .match (text , 0 )
124+ while match .hasMatch ():
125+ index = match .capturedStart (nth )
126+ if index >= 0 :
127+ # if there is a string we check
128+ # if there are some triple quotes within the string
129+ # they will be ignored if they are matched again
130+ if expression .pattern () in [r'"[^"\\]*(\\.[^"\\]*)*"' , r"'[^'\\]*(\\.[^'\\]*)*'" ]:
131+ inner_match = self .tri_single [0 ].match (text , index + 1 )
132+ innerIndex = inner_match .capturedStart () if inner_match .hasMatch () else - 1
133+ if innerIndex == - 1 :
134+ inner_match = self .tri_double [0 ].match (text , index + 1 )
135+ innerIndex = inner_match .capturedStart () if inner_match .hasMatch () else - 1
136+
137+ if innerIndex != - 1 :
138+ tripleQuoteIndexes = range (innerIndex , innerIndex + 3 )
139+ self .tripleQuoutesWithinStrings .extend (tripleQuoteIndexes )
140+
141+ while index >= 0 :
142+ # skipping triple quotes within strings
143+ if index in self .tripleQuoutesWithinStrings :
144+ index += 1
145+ match = expression .match (text , index )
146+ if match .hasMatch ():
147+ index = match .capturedStart (nth )
148+ continue
149+
150+ # We actually want the index of the nth match
151+ length = match .capturedLength (nth )
152+ self .setFormat (index , length , format )
153+ match = expression .match (text , index + length )
154+ if match .hasMatch ():
155+ index = match .capturedStart (nth )
156+ else :
157+ index = - 1
147158
148159 self .setCurrentBlockState (0 )
149160
@@ -155,7 +166,7 @@ def highlightBlock(self, text):
155166 def match_multiline (self , text , delimiter , in_state , style ):
156167 """
157168 Do highlighting of multi-line strings. ``delimiter`` should be a
158- ``QRegExp `` for triple-single-quotes or triple-double-quotes, and
169+ ``QRegularExpression `` for triple-single-quotes or triple-double-quotes, and
159170 ``in_state`` should be a unique integer to represent the corresponding
160171 state changes when inside those strings. Returns True if we're still
161172 inside a multi-line string when this function is finished.
@@ -166,20 +177,23 @@ def match_multiline(self, text, delimiter, in_state, style):
166177 add = 0
167178 # Otherwise, look for the delimiter on this line
168179 else :
169- start = delimiter .indexIn (text )
180+ match = delimiter .match (text )
181+ start = match .capturedStart ()
182+
170183 # skipping triple quotes within strings
171184 if start in self .tripleQuoutesWithinStrings :
172185 return False
173186 # Move past this match
174- add = delimiter . matchedLength ()
187+ add = match . capturedLength ()
175188
176189 # As long as there's a delimiter match on this line...
177190 while start >= 0 :
191+ match = delimiter .match (text , start + add )
192+ end = match .capturedStart ()
193+
178194 # Look for the ending delimiter
179- end = delimiter .indexIn (text , start + add )
180- # Ending delimiter on this line?
181195 if end >= add :
182- length = end - start + add + delimiter . matchedLength ()
196+ length = end - start + add + match . capturedLength ()
183197 self .setCurrentBlockState (0 )
184198 # No; multi-line string
185199 else :
@@ -188,10 +202,8 @@ def match_multiline(self, text, delimiter, in_state, style):
188202 # Apply formatting
189203 self .setFormat (start , length , style )
190204 # Look for the next match
191- start = delimiter .indexIn (text , start + length )
205+ match = delimiter .match (text , start + length )
206+ start = match .capturedStart () if match .hasMatch () else - 1
192207
193208 # Return True if still inside a multi-line string, False otherwise
194- if self .currentBlockState () == in_state :
195- return True
196- else :
197- return False
209+ return self .currentBlockState () == in_state
0 commit comments