@@ -9,7 +9,7 @@ def format(color, style=''):
99 _format = QtGui .QTextCharFormat ()
1010 _format .setForeground (_color )
1111 if 'bold' in style :
12- _format .setFontWeight (QtGui .QFont .Bold )
12+ _format .setFontWeight (QtGui .QFont .Weight . Bold if hasattr ( QtGui . QFont , 'Weight' ) else QtGui . QFont . Bold )
1313 if 'italic' in style :
1414 _format .setFontItalic (True )
1515 return _format
@@ -48,25 +48,31 @@ class PythonHighlighter (QtGui.QSyntaxHighlighter):
4848 # Comparison
4949 '==' , '!=' , '<' , '<=' , '>' , '>=' ,
5050 # Arithmetic
51- '\+' , '-' , '\*' , '/' , '//' , '\ %' , '\*\*' ,
51+ r '\+' , '-' , r '\*' , '/' , '//' , r' %' , r '\*\*' ,
5252 # In-place
53- '\+=' , '-=' , '\*=' , '/=' , '\ %=' ,
53+ r '\+=' , '-=' , r '\*=' , '/=' , r' %=' ,
5454 # Bitwise
55- '\^' , '\|' , '\&' , '\~' , '>>' , '<<' ,
55+ r '\^' , r '\|' , r '\&' , r '\~' , '>>' , '<<' ,
5656 ]
5757
5858 # Python braces
5959 braces = [
60- '\{' , '\}' , '\(' , '\)' , '\[' , '\]' ,
60+ r '\{' , r '\}' , r '\(' , r '\)' , r '\[' , r '\]' ,
6161 ]
62+
6263 def __init__ (self , document ):
6364 QtGui .QSyntaxHighlighter .__init__ (self , document )
6465
6566 # Multi-line strings (expression, flag, style)
66- # FIXME: The triple-quotes in these two lines will mess up the
67- # syntax highlighting from this point onward
68- self .tri_single = (QtCore .QRegularExpression ("'''" ), 1 , STYLES ['string2' ])
69- self .tri_double = (QtCore .QRegularExpression ('"""' ), 2 , STYLES ['string2' ])
67+ # Use QRegExp if QRegularExpression is not available
68+ if hasattr (QtCore , 'QRegularExpression' ):
69+ self .tri_single = (QtCore .QRegularExpression ("'''" ), 1 , STYLES ['string2' ])
70+ self .tri_double = (QtCore .QRegularExpression ('"""' ), 2 , STYLES ['string2' ])
71+ self .RegexClass = QtCore .QRegularExpression
72+ else :
73+ self .tri_single = (QtCore .QRegExp ("'''" ), 1 , STYLES ['string2' ])
74+ self .tri_double = (QtCore .QRegExp ('"""' ), 2 , STYLES ['string2' ])
75+ self .RegexClass = QtCore .QRegExp
7076
7177 rules = []
7278
@@ -102,8 +108,8 @@ def __init__(self, document):
102108 (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b' , 0 , STYLES ['numbers' ]),
103109 ]
104110
105- # Build a QtCore.QRegularExpression for each pattern
106- self .rules = [(QtCore . QRegularExpression (pat ), index , fmt )
111+ # Build a RegExp for each pattern
112+ self .rules = [(self . RegexClass (pat ), index , fmt )
107113 for (pat , index , fmt ) in rules ]
108114
109115
@@ -112,12 +118,27 @@ def highlightBlock(self, text):
112118 """
113119 # Do other syntax formatting
114120 for expression , nth , format in self .rules :
115- match = expression .match (text , 0 )
116- while match .hasMatch ():
117- index = match .capturedStart ()
118- length = match .capturedLength ()
119- self .setFormat (index , length , format )
120- match = expression .match (text , index + length )
121+ if hasattr (QtCore , 'QRegularExpression' ):
122+ # For QRegularExpression
123+ match = expression .match (text , 0 )
124+ while match .hasMatch ():
125+ index = match .capturedStart ()
126+ length = match .capturedLength ()
127+ if nth > 0 :
128+ index = match .capturedStart (nth )
129+ length = match .capturedLength (nth )
130+ self .setFormat (index , length , format )
131+ match = expression .match (text , index + length )
132+ else :
133+ # For QRegExp
134+ index = expression .indexIn (text )
135+ while index >= 0 :
136+ length = expression .matchedLength ()
137+ if nth > 0 :
138+ index = expression .pos (nth )
139+ length = len (expression .cap (nth ))
140+ self .setFormat (index , length , format )
141+ index = expression .indexIn (text , index + length )
121142
122143 self .setCurrentBlockState (0 )
123144
@@ -129,7 +150,7 @@ def highlightBlock(self, text):
129150
130151 def match_multiline (self , text , delimiter , in_state , style ):
131152 """Do highlighting of multi-line strings. ``delimiter`` should be a
132- ``QtCore. QRegularExpression`` for triple-single-quotes or triple-double-quotes, and
153+ ``QRegExp`` or `` QRegularExpression`` for triple-single-quotes or triple-double-quotes, and
133154 ``in_state`` should be a unique integer to represent the corresponding
134155 state changes when inside those strings. Returns True if we're still
135156 inside a multi-line string when this function is finished.
@@ -140,17 +161,34 @@ def match_multiline(self, text, delimiter, in_state, style):
140161 add = 0
141162 # Otherwise, look for the delimiter on this line
142163 else :
143- start = delimiter .indexIn (text )
144- # Move past this match
145- add = delimiter .matchedLength ()
164+ if hasattr (QtCore , 'QRegularExpression' ):
165+ # For QRegularExpression
166+ match = delimiter .match (text )
167+ start = match .capturedStart () if match .hasMatch () else - 1
168+ add = match .capturedLength () if match .hasMatch () else 0
169+ else :
170+ # For QRegExp
171+ start = delimiter .indexIn (text )
172+ add = delimiter .matchedLength ()
146173
147174 # As long as there's a delimiter match on this line...
148175 while start >= 0 :
149176 # Look for the ending delimiter
150- end = delimiter .indexIn (text , start + add )
177+ if hasattr (QtCore , 'QRegularExpression' ):
178+ # For QRegularExpression
179+ match = delimiter .match (text , start + add )
180+ end = match .capturedStart () if match .hasMatch () else - 1
181+ else :
182+ # For QRegExp
183+ end = delimiter .indexIn (text , start + add )
184+
151185 # Ending delimiter on this line?
152186 if end >= add :
153- length = end - start + add + delimiter .matchedLength ()
187+ # Fix the conditional to properly handle match objects
188+ if hasattr (QtCore , 'QRegularExpression' ) and match .hasMatch ():
189+ length = end - start + add + match .capturedLength ()
190+ else :
191+ length = end - start + add + delimiter .matchedLength ()
154192 self .setCurrentBlockState (0 )
155193 # No; multi-line string
156194 else :
@@ -159,7 +197,13 @@ def match_multiline(self, text, delimiter, in_state, style):
159197 # Apply formatting
160198 self .setFormat (start , length , style )
161199 # Look for the next match
162- start = delimiter .indexIn (text , start + length )
200+ if hasattr (QtCore , 'QRegularExpression' ):
201+ # For QRegularExpression
202+ match = delimiter .match (text , start + length )
203+ start = match .capturedStart () if match .hasMatch () else - 1
204+ else :
205+ # For QRegExp
206+ start = delimiter .indexIn (text , start + length )
163207
164208 # Return True if still inside a multi-line string, False otherwise
165209 if self .currentBlockState () == in_state :
0 commit comments