@@ -52,7 +52,6 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
5252 << " not"
5353 << " or"
5454 << " pass"
55- << " print"
5655 << " raise"
5756 << " return"
5857 << " try"
@@ -99,8 +98,90 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
9998 << " \\ ["
10099 << " \\ ]" ;
101100
102- setStyles ();
101+ builtins = QStringList () << " abs"
102+ << " divmod"
103+ << " input"
104+ << " open"
105+ << " staticmethod"
106+ << " all"
107+ << " enumerate"
108+ << " int"
109+ << " ord"
110+ << " str"
111+ << " any"
112+ << " eval"
113+ << " isinstance"
114+ << " pow"
115+ << " sum"
116+ << " basestring"
117+ << " execfile"
118+ << " issubclass"
119+ << " print"
120+ << " super"
121+ << " bin"
122+ << " file"
123+ << " iter"
124+ << " property"
125+ << " tuple"
126+ << " bool"
127+ << " filter"
128+ << " len"
129+ << " range"
130+ << " type"
131+ << " bytearray"
132+ << " float"
133+ << " list"
134+ << " raw_input"
135+ << " unichr"
136+ << " callable"
137+ << " format"
138+ << " locals"
139+ << " reduce"
140+ << " unicode"
141+ << " chr"
142+ << " frozenset"
143+ << " long"
144+ << " reload"
145+ << " vars"
146+ << " classmethod"
147+ << " getattr"
148+ << " map"
149+ << " repr"
150+ << " xrange"
151+ << " cmp"
152+ << " globals"
153+ << " max"
154+ << " reversed"
155+ << " zip"
156+ << " compile"
157+ << " hasattr"
158+ << " memoryview"
159+ << " round"
160+ << " __import__"
161+ << " complex"
162+ << " hash"
163+ << " min"
164+ << " set"
165+ << " apply"
166+ << " delattr"
167+ << " help"
168+ << " next"
169+ << " setattr"
170+ << " buffer"
171+ << " dict"
172+ << " hex"
173+ << " object"
174+ << " slice"
175+ << " coerce"
176+ << " dir"
177+ << " id"
178+ << " oct"
179+ << " sorted"
180+ << " intern" ;
103181
182+ setStyles ();
183+ mSearchRegex = tr (" " );
184+ mSearchHighlight = getTextCharFormat (" black" , " bold" , " yellow" );
104185 triSingleQuote.setPattern (" '''" );
105186 triDoubleQuote.setPattern (" \"\"\" " );
106187
@@ -111,6 +192,7 @@ void PythonSyntaxHighlighter::setStyles()
111192{
112193 basicStyles.insert (" keyword" , getTextCharFormat (" orange" , " bold" ));
113194 basicStyles.insert (" operator" , getTextCharFormat (" yellow" , " bold" ));
195+ basicStyles.insert (" builtins" , getTextCharFormat (" lightblue" , " underline" ));
114196 basicStyles.insert (" brace" , getTextCharFormat (" red" , " bold" ));
115197 basicStyles.insert (" string" , getTextCharFormat (" magenta" ));
116198 basicStyles.insert (" stringlong" , getTextCharFormat (" magenta" , " bold" ));
@@ -138,15 +220,20 @@ void PythonSyntaxHighlighter::initializeRules()
138220 rules.append (HighlightingRule (" \\ b__[\\ w_]+__\\ b" , 0 , basicStyles.value (" hackish" )));
139221 rules.append (HighlightingRule (" \\ b_[\\ w_]+\\ b" , 0 , basicStyles.value (" private" )));
140222
223+ foreach (QString currBuiltin, builtins) {
224+ rules.append (HighlightingRule (QString (" \\ b%1\\ b" ).arg (currBuiltin), 0 , basicStyles.value (" builtins" )));
225+ }
226+
227+
141228 rules.append (HighlightingRule (" \\ b_\\ b" , 0 , basicStyles.value (" special" )));
142229 rules.append (HighlightingRule (" \\ bself\\ b" , 0 , basicStyles.value (" special" )));
143230
144- rules.append (HighlightingRule (" [uUrR]?\" [^\"\\\\ ]*(\\\\ .[^\"\\\\ ]*)*\" " , 0 , basicStyles.value (" string" )));
145- rules.append (HighlightingRule (" [uUrR]?'[^'\\\\ ]*(\\\\ .[^'\\\\ ]*)*'" , 0 , basicStyles.value (" string" )));
146-
147231 rules.append (HighlightingRule (" (b|B|br|Br|bR|BR|rb|rB|Rb|RB)\" [^\"\\\\ ]*(\\\\ .[^\"\\\\ ]*)*\" " , 0 , basicStyles.value (" bytes" )));
148232 rules.append (HighlightingRule (" (b|B|br|Br|bR|BR|rb|rB|Rb|RB)'[^'\\\\ ]*(\\\\ .[^'\\\\ ]*)*'" , 0 , basicStyles.value (" bytes" )));
149233
234+ rules.append (HighlightingRule (" [uUrR]?\" [^\"\\\\ ]*(\\\\ .[^\"\\\\ ]*)*\" " , 0 , basicStyles.value (" string" )));
235+ rules.append (HighlightingRule (" [uUrR]?'[^'\\\\ ]*(\\\\ .[^'\\\\ ]*)*'" , 0 , basicStyles.value (" string" )));
236+
150237 rules.append (HighlightingRule (" \\ b[+-]?[0-9]+[lL]?\\ b" , 0 , basicStyles.value (" numbers" )));
151238 rules.append (HighlightingRule (" \\ b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\ b" , 0 , basicStyles.value (" numbers" )));
152239 rules.append (HighlightingRule (" \\ b[+-]?[0-9]+(?:\\ .[0-9]+)?(?:[eE][+-]?[0-9]+)?\\ b" , 0 , basicStyles.value (" numbers" )));
@@ -178,8 +265,26 @@ void PythonSyntaxHighlighter::highlightBlock(const QString& text)
178265
179266 // Do multi-line strings
180267 bool isInMultilne = matchMultiline (text, triSingleQuote, 1 , basicStyles.value (" stringlong" ));
181- if (!isInMultilne)
268+ if (!isInMultilne) {
182269 isInMultilne = matchMultiline (text, triDoubleQuote, 2 , basicStyles.value (" stringlong" ));
270+ }
271+
272+ // Highlight found stuff
273+ if (!mSearchRegex .isNull () && !mSearchRegex .isEmpty ())
274+ {
275+ QRegExp reg (mSearchRegex );
276+ int idx = reg.indexIn (text, 0 );
277+ while (idx >= 0 ) {
278+ int length = reg.cap (0 ).length ();
279+ setFormat (idx, length, mSearchHighlight );
280+ idx = reg.indexIn (text, idx + length);
281+ }
282+ }
283+ }
284+
285+ void PythonSyntaxHighlighter::SetSearchRegEx (const QString& text)
286+ {
287+ mSearchRegex = text;
183288}
184289
185290bool PythonSyntaxHighlighter::matchMultiline (const QString& text, const QRegExp& delimiter, const int inState, const QTextCharFormat& style)
0 commit comments