1- /*
2- $Id: PythonSyntaxHighlighter.cpp 167 2013-11-03 17:01:22Z oliver $
3- This is a C++ port of the following PyQt example
4- http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
5- C++ port by Frankie Simon (www.kickdrive.de, www.fuh-edv.de)
6-
7- The following free software license applies for this file ("X11 license"):
8-
9- Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10- and associated documentation files (the "Software"), to deal in the Software without restriction,
11- including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12- and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13- subject to the following conditions:
14-
15- The above copyright notice and this permission notice shall be included in all copies or substantial
16- portions of the Software.
17-
18- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
19- LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20- IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22- USE OR OTHER DEALINGS IN THE SOFTWARE.
1+ /* $Id: PythonSyntaxHighlighter.cpp 167 2013-11-03 17:01:22Z oliver $
2+ *
3+ * This is a C++ port of the following PyQt example
4+ * http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
5+ * C++ port by Frankie Simon (www.kickdrive.de, www.fuh-edv.de)
6+ *
7+ * The following free software license applies for this file ("X11 license"):
8+ *
9+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13+ * subject to the following conditions:
14+ *
15+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16+ * portions of the Software.
17+ *
18+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
19+ * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+ * IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
23+ *
24+ * -----------Modified By Bhathiya Perera-------------
2325*/
2426
2527#include " CodeEditor/PythonSyntaxHighlighter.h"
@@ -60,30 +62,26 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
6062 << " True"
6163 << " False" ;
6264
63- operators = QStringList () << " =" <<
64- // Comparison
65- " =="
65+ operators = QStringList () << " ="
66+ << " =="
6667 << " !="
6768 << " <"
6869 << " <="
6970 << " >"
70- << " >=" <<
71- // Arithmetic
72- " \\ +"
71+ << " >="
72+ << " \\ +"
7373 << " -"
7474 << " \\ *"
7575 << " /"
7676 << " //"
7777 << " %"
78- << " \\ *\\ *" <<
79- // In-place
80- " \\ +="
78+ << " \\ *\\ *"
79+ << " \\ +="
8180 << " -="
8281 << " \\ *="
8382 << " /="
84- << " %=" <<
85- // Bitwise
86- " \\ ^"
83+ << " %="
84+ << " \\ ^"
8785 << " \\ |"
8886 << " &"
8987 << " ~"
@@ -97,11 +95,6 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
9795 << " \\ ["
9896 << " ]" ;
9997
100- QFont* font = new QFont ();
101- font->setFamily (" Courier New" );
102- font->setFixedPitch (true );
103- font->setPointSize (10 );
104-
10598 setStyles ();
10699
107100 triSingleQuote.setPattern (" '''" );
@@ -112,16 +105,16 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument* parent)
112105
113106void PythonSyntaxHighlighter::setStyles ()
114107{
115- basicStyles.insert (" keyword" , getTextCharFormat (" blue " ));
108+ basicStyles.insert (" keyword" , getTextCharFormat (" orange " , " bold " ));
116109 basicStyles.insert (" operator" , getTextCharFormat (" red" ));
117- basicStyles.insert (" brace" , getTextCharFormat (" darkGray" ));
118- basicStyles.insert (" defclass" , getTextCharFormat (" black" , " bold" ));
119- basicStyles.insert (" brace" , getTextCharFormat (" darkGray" ));
110+ basicStyles.insert (" brace" , getTextCharFormat (" red" , " bold" ));
111+ basicStyles.insert (" defclass" , getTextCharFormat (" white" , " bold" ));
120112 basicStyles.insert (" string" , getTextCharFormat (" magenta" ));
121113 basicStyles.insert (" string2" , getTextCharFormat (" darkMagenta" ));
122- basicStyles.insert (" comment" , getTextCharFormat (" darkGreen" , " italic" ));
123- basicStyles.insert (" self" , getTextCharFormat (" black" , " italic" ));
124- basicStyles.insert (" numbers" , getTextCharFormat (" brown" ));
114+ basicStyles.insert (" comment" , getTextCharFormat (" darkGreen" , " bold" ));
115+ basicStyles.insert (" self" , getTextCharFormat (" white" , " bold" ));
116+ basicStyles.insert (" numbers" , getTextCharFormat (" cyan" ));
117+ basicStyles.insert (" bugs" , getTextCharFormat (" yellow" , " bold" , " red" ));
125118}
126119
127120void PythonSyntaxHighlighter::initializeRules ()
@@ -139,27 +132,25 @@ void PythonSyntaxHighlighter::initializeRules()
139132 rules.append (HighlightingRule (" \\ bself\\ b" , 0 , basicStyles.value (" self" )));
140133
141134 // Double-quoted string, possibly containing escape sequences
142- // FF: originally in python : r'"[^"\\]*(\\.[^"\\]*)*"'
143135 rules.append (HighlightingRule (" \" [^\"\\\\ ]*(\\\\ .[^\"\\\\ ]*)*\" " , 0 , basicStyles.value (" string" )));
144136 // Single-quoted string, possibly containing escape sequences
145- // FF: originally in python : r"'[^'\\]*(\\.[^'\\]*)*'"
146137 rules.append (HighlightingRule (" '[^'\\\\ ]*(\\\\ .[^'\\\\ ]*)*'" , 0 , basicStyles.value (" string" )));
147138
148139 // 'def' followed by an identifier
149- // FF: originally: r'\bdef\b\s*(\w+)'
150140 rules.append (HighlightingRule (" \\ bdef\\ b\\ s*(\\ w+)" , 1 , basicStyles.value (" defclass" )));
151141 // 'class' followed by an identifier
152- // FF: originally: r'\bclass\b\s*(\w+)'
153142 rules.append (HighlightingRule (" \\ bclass\\ b\\ s*(\\ w+)" , 1 , basicStyles.value (" defclass" )));
154143
155144 // From '#' until a newline
156- // FF: originally: r'#[^\\n]*'
157145 rules.append (HighlightingRule (" #[^\\ n]*" , 0 , basicStyles.value (" comment" )));
158146
159147 // Numeric literals
160- rules.append (HighlightingRule (" \\ b[+-]?[0-9]+[lL]?\\ b" , 0 , basicStyles.value (" numbers" ))); // r'\b[+-]?[0-9]+[lL]?\b'
161- rules.append (HighlightingRule (" \\ b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\ b" , 0 , basicStyles.value (" numbers" ))); // r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b'
162- rules.append (HighlightingRule (" \\ b[+-]?[0-9]+(?:\\ .[0-9]+)?(?:[eE][+-]?[0-9]+)?\\ b" , 0 , basicStyles.value (" numbers" ))); // r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b'
148+ rules.append (HighlightingRule (" \\ b[+-]?[0-9]+[lL]?\\ b" , 0 , basicStyles.value (" numbers" )));
149+ rules.append (HighlightingRule (" \\ b[+-]?0[xX][0-9A-Fa-f]+[lL]?\\ b" , 0 , basicStyles.value (" numbers" )));
150+ rules.append (HighlightingRule (" \\ b[+-]?[0-9]+(?:\\ .[0-9]+)?(?:[eE][+-]?[0-9]+)?\\ b" , 0 , basicStyles.value (" numbers" )));
151+
152+ // tab and space mixed
153+ rules.append (HighlightingRule (" [^\\ n]*(?:\\ t | \\ t)[^\\ n]*" , 0 , basicStyles.value (" bugs" )));
163154}
164155
165156void PythonSyntaxHighlighter::highlightBlock (const QString& text)
@@ -227,11 +218,17 @@ bool PythonSyntaxHighlighter::matchMultiline(const QString& text, const QRegExp&
227218 return false ;
228219}
229220
230- const QTextCharFormat PythonSyntaxHighlighter::getTextCharFormat (const QString& colorName, const QString& style)
221+ const QTextCharFormat PythonSyntaxHighlighter::getTextCharFormat (
222+ const QString& colorName, const QString& style, const QString& backColorName)
231223{
232224 QTextCharFormat charFormat;
233225 QColor color (colorName);
234226 charFormat.setForeground (color);
227+
228+ if (!backColorName.isEmpty ()) {
229+ QColor backColor (backColorName);
230+ charFormat.setBackground (backColor);
231+ }
235232 if (style.contains (" bold" , Qt::CaseInsensitive))
236233 charFormat.setFontWeight (QFont::Bold);
237234 if (style.contains (" italic" , Qt::CaseInsensitive))
0 commit comments