@@ -88,7 +88,11 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
8888 << " >>"
8989 << " <<" ;
9090
91- braces = QStringList () << " {"
91+ braces = QStringList () << " :"
92+ << " ;"
93+ << " ,"
94+ << " @"
95+ << " {"
9296 << " }"
9397 << " \\ ("
9498 << " \\ )"
@@ -106,15 +110,17 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
106110void PythonSyntaxHighlighter::setStyles ()
107111{
108112 basicStyles.insert (" keyword" , getTextCharFormat (" orange" , " bold" ));
109- basicStyles.insert (" operator" , getTextCharFormat (" red " ));
113+ basicStyles.insert (" operator" , getTextCharFormat (" yellow " , " bold " ));
110114 basicStyles.insert (" brace" , getTextCharFormat (" red" , " bold" ));
111- basicStyles.insert (" defclass" , getTextCharFormat (" white" , " bold" ));
112115 basicStyles.insert (" string" , getTextCharFormat (" magenta" ));
113- basicStyles.insert (" string2 " , getTextCharFormat (" darkMagenta " ));
114- basicStyles.insert (" comment" , getTextCharFormat (" darkGreen " , " bold" ));
115- basicStyles.insert (" self " , getTextCharFormat (" white " , " bold" ));
116+ basicStyles.insert (" stringlong " , getTextCharFormat (" magenta " , " bold " ));
117+ basicStyles.insert (" comment" , getTextCharFormat (" darkgreen " , " bold" ));
118+ basicStyles.insert (" special " , getTextCharFormat (" teal " , " bold" ));
116119 basicStyles.insert (" numbers" , getTextCharFormat (" cyan" ));
117120 basicStyles.insert (" bugs" , getTextCharFormat (" yellow" , " bold" , " red" ));
121+ basicStyles.insert (" hackish" , getTextCharFormat (" royalblue" , " bold" ));
122+ basicStyles.insert (" private" , getTextCharFormat (" white" , " italic" ));
123+ basicStyles.insert (" bytes" , getTextCharFormat (" lightsteelblue" ));
118124}
119125
120126void PythonSyntaxHighlighter::initializeRules ()
@@ -128,50 +134,52 @@ void PythonSyntaxHighlighter::initializeRules()
128134 foreach (QString currBrace, braces) {
129135 rules.append (HighlightingRule (QString (" %1" ).arg (currBrace), 0 , basicStyles.value (" brace" )));
130136 }
131- // 'self'
132- rules.append (HighlightingRule (" \\ bself\\ b" , 0 , basicStyles.value (" self" )));
133137
134- // Double-quoted string, possibly containing escape sequences
135- rules.append (HighlightingRule (" \" [^\"\\\\ ]*(\\\\ .[^\"\\\\ ]*)*\" " , 0 , basicStyles.value (" string" )));
136- // Single-quoted string, possibly containing escape sequences
137- rules.append (HighlightingRule (" '[^'\\\\ ]*(\\\\ .[^'\\\\ ]*)*'" , 0 , basicStyles.value (" string" )));
138+ rules.append (HighlightingRule (" \\ b__[\\ w_]+__\\ b" , 0 , basicStyles.value (" hackish" )));
139+ rules.append (HighlightingRule (" \\ b_[\\ w_]+\\ b" , 0 , basicStyles.value (" private" )));
138140
139- // 'def' followed by an identifier
140- rules.append (HighlightingRule (" \\ bdef\\ b\\ s*(\\ w+)" , 1 , basicStyles.value (" defclass" )));
141- // 'class' followed by an identifier
142- rules.append (HighlightingRule (" \\ bclass\\ b\\ s*(\\ w+)" , 1 , basicStyles.value (" defclass" )));
141+ rules.append (HighlightingRule (" \\ b_\\ b" , 0 , basicStyles.value (" special" )));
142+ rules.append (HighlightingRule (" \\ bself\\ b" , 0 , basicStyles.value (" special" )));
143+
144+ rules.append (HighlightingRule (" [uUrR]?\" [^\"\\\\ ]*(\\\\ .[^\"\\\\ ]*)*\" " , 0 , basicStyles.value (" string" )));
145+ rules.append (HighlightingRule (" [uUrR]?'[^'\\\\ ]*(\\\\ .[^'\\\\ ]*)*'" , 0 , basicStyles.value (" string" )));
146+
147+ rules.append (HighlightingRule (" (b|B|br|Br|bR|BR|rb|rB|Rb|RB)\" [^\"\\\\ ]*(\\\\ .[^\"\\\\ ]*)*\" " , 0 , basicStyles.value (" bytes" )));
148+ rules.append (HighlightingRule (" (b|B|br|Br|bR|BR|rb|rB|Rb|RB)'[^'\\\\ ]*(\\\\ .[^'\\\\ ]*)*'" , 0 , basicStyles.value (" bytes" )));
143149
144- // Numeric literals
145150 rules.append (HighlightingRule (" \\ b[+-]?[0-9]+[lL]?\\ b" , 0 , basicStyles.value (" numbers" )));
146151 rules.append (HighlightingRule (" \\ b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\ b" , 0 , basicStyles.value (" numbers" )));
147152 rules.append (HighlightingRule (" \\ b[+-]?[0-9]+(?:\\ .[0-9]+)?(?:[eE][+-]?[0-9]+)?\\ b" , 0 , basicStyles.value (" numbers" )));
148153
149- // tab and space mixed
150- rules.append (HighlightingRule (" [^\\ n]*(?:\\ t | \\ t)[^\\ n]*" , 0 , basicStyles.value (" bugs" )));
154+ rules.append (HighlightingRule (" \\ t+" , 0 , basicStyles.value (" bugs" )));
155+ rules.append (HighlightingRule (" \\ ?" , 0 , basicStyles.value (" bugs" )));
156+ rules.append (HighlightingRule (" \\ $" , 0 , basicStyles.value (" bugs" )));
151157
152- // From '#' until a newline
153158 rules.append (HighlightingRule (" #[^\\ n]*" , 0 , basicStyles.value (" comment" )));
154159}
155160
156161void PythonSyntaxHighlighter::highlightBlock (const QString& text)
157162{
158- foreach (HighlightingRule currRule, rules) {
159- int idx = currRule.pattern .indexIn (text, 0 );
160- while (idx >= 0 ) {
161- // Get index of Nth match
162- idx = currRule.pattern .pos (currRule.nth );
163- int length = currRule.pattern .cap (currRule.nth ).length ();
164- setFormat (idx, length, currRule.format );
165- idx = currRule.pattern .indexIn (text, idx + length);
163+ int len = text.length ();
164+ for (int i = 0 ; i < len; i++) {
165+ foreach (HighlightingRule currRule, rules) {
166+ int idx = currRule.pattern .indexIn (text, i);
167+ if (idx == i) {
168+ idx = currRule.pattern .pos (currRule.nth );
169+ int length = currRule.pattern .cap (currRule.nth ).length ();
170+ setFormat (idx, length, currRule.format );
171+ i = idx + length - 1 ;
172+ break ;
173+ }
166174 }
167175 }
168176
169177 setCurrentBlockState (0 );
170178
171179 // Do multi-line strings
172- bool isInMultilne = matchMultiline (text, triSingleQuote, 1 , basicStyles.value (" string2 " ));
180+ bool isInMultilne = matchMultiline (text, triSingleQuote, 1 , basicStyles.value (" stringlong " ));
173181 if (!isInMultilne)
174- isInMultilne = matchMultiline (text, triDoubleQuote, 2 , basicStyles.value (" string2 " ));
182+ isInMultilne = matchMultiline (text, triDoubleQuote, 2 , basicStyles.value (" stringlong " ));
175183}
176184
177185bool PythonSyntaxHighlighter::matchMultiline (const QString& text, const QRegExp& delimiter, const int inState, const QTextCharFormat& style)
@@ -233,5 +241,7 @@ const QTextCharFormat PythonSyntaxHighlighter::getTextCharFormat(
233241 charFormat.setFontWeight (QFont::Bold);
234242 if (style.contains (" italic" , Qt::CaseInsensitive))
235243 charFormat.setFontItalic (true );
244+ if (style.contains (" underline" , Qt::CaseInsensitive))
245+ charFormat.setFontUnderline (true );
236246 return charFormat;
237247}
0 commit comments