3
3
"""
4
4
5
5
6
- from Qt import QtCore , QtGui , QtWidgets
6
+ from PySide6 import QtGui
7
+ from PySide6 .QtCore import QRegularExpression
7
8
8
9
9
10
def format (color , style = '' ):
@@ -70,8 +71,9 @@ def __init__(self, parent=None):
70
71
super (PythonHighlighter , self ).__init__ (parent )
71
72
72
73
# 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' ])
75
77
76
78
rules = []
77
79
@@ -108,7 +110,7 @@ def __init__(self, parent=None):
108
110
]
109
111
110
112
# Build a QRegExp for each pattern
111
- self .rules = [(QtCore . QRegExp (pat ), index , fmt )
113
+ self .rules = [(QRegularExpression (pat ), index , fmt )
112
114
for (pat , index , fmt ) in rules ]
113
115
114
116
def highlightBlock (self , text ):
@@ -118,32 +120,41 @@ def highlightBlock(self, text):
118
120
self .tripleQuoutesWithinStrings = []
119
121
# Do other syntax formatting
120
122
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
147
158
148
159
self .setCurrentBlockState (0 )
149
160
@@ -155,7 +166,7 @@ def highlightBlock(self, text):
155
166
def match_multiline (self , text , delimiter , in_state , style ):
156
167
"""
157
168
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
159
170
``in_state`` should be a unique integer to represent the corresponding
160
171
state changes when inside those strings. Returns True if we're still
161
172
inside a multi-line string when this function is finished.
@@ -166,20 +177,23 @@ def match_multiline(self, text, delimiter, in_state, style):
166
177
add = 0
167
178
# Otherwise, look for the delimiter on this line
168
179
else :
169
- start = delimiter .indexIn (text )
180
+ match = delimiter .match (text )
181
+ start = match .capturedStart ()
182
+
170
183
# skipping triple quotes within strings
171
184
if start in self .tripleQuoutesWithinStrings :
172
185
return False
173
186
# Move past this match
174
- add = delimiter . matchedLength ()
187
+ add = match . capturedLength ()
175
188
176
189
# As long as there's a delimiter match on this line...
177
190
while start >= 0 :
191
+ match = delimiter .match (text , start + add )
192
+ end = match .capturedStart ()
193
+
178
194
# Look for the ending delimiter
179
- end = delimiter .indexIn (text , start + add )
180
- # Ending delimiter on this line?
181
195
if end >= add :
182
- length = end - start + add + delimiter . matchedLength ()
196
+ length = end - start + add + match . capturedLength ()
183
197
self .setCurrentBlockState (0 )
184
198
# No; multi-line string
185
199
else :
@@ -188,10 +202,8 @@ def match_multiline(self, text, delimiter, in_state, style):
188
202
# Apply formatting
189
203
self .setFormat (start , length , style )
190
204
# 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
192
207
193
208
# 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