Skip to main content

Python syntax highlighting for QTextEdit C++/Qt

We were in need for some basic Python syntax highlighting in a QTextEdit box, and didn't immediately want to go fully pro with a Scintilla wrapper. (Update: We did that, too, in the end, according to this scintilla/qt post).

So we looked for a QSyntaxHighlighter subclass for Python formatting, but only found a PyQt example  which seems to be from Torsten Marek ( shlomme.diotavelli.net ) originally.

I asked Frankie to create a C++  Qt4 QSyntaxHighlighter class based on this, so here it is:

/*
$Id: PythonSyntaxHighlighter.cpp 167 2013-11-03 17:01:22Z oliver $
This is a C++ port of the following PyQt example
http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
C++ port by Frankie Simon (www.kickdrive.de, www.fuh-edv.de)

The following free software license applies for this file ("X11 license"): 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial 
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "PythonSyntaxHighlighter.h"

PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument *parent)
 : QSyntaxHighlighter(parent)
{
 keywords = QStringList() << "and" << "assert" << "break" << "class" << "continue" << "def" <<
        "del" << "elif" << "else" << "except" << "exec" << "finally" <<
        "for" << "from" << "global" << "if" << "import" << "in" <<
        "is" << "lambda" << "not" << "or" << "pass" << "print" <<
        "raise" << "return" << "try" << "while" << "yield" <<
        "None" << "True" << "False";
 
 operators = QStringList() << "=" <<
        // Comparison
        "==" << "!=" << "<" << "<=" << ">" << ">=" <<
        // Arithmetic
        "\\+" << "-" << "\\*" << "/" << "//" << "%" << "\\*\\*" <<
        // In-place
        "\\+=" << "-=" << "\\*=" << "/=" << "%=" <<
        // Bitwise
        "\\^" << "\\|" << "&" << "~" << ">>" << "<<";

 braces = QStringList() << "{" << "}" << "\\(" << "\\)" << "\\[" << "]";

 basicStyles.insert("keyword", getTextCharFormat("blue"));
 basicStyles.insert("operator", getTextCharFormat("red"));
 basicStyles.insert("brace", getTextCharFormat("darkGray"));
    basicStyles.insert("defclass", getTextCharFormat("black", "bold"));
 basicStyles.insert("brace", getTextCharFormat("darkGray"));
 basicStyles.insert("string", getTextCharFormat("magenta"));
 basicStyles.insert("string2", getTextCharFormat("darkMagenta"));
 basicStyles.insert("comment", getTextCharFormat("darkGreen", "italic"));
 basicStyles.insert("self", getTextCharFormat("black", "italic"));
 basicStyles.insert("numbers", getTextCharFormat("brown"));

 triSingleQuote.setPattern("'''");
 triDoubleQuote.setPattern("\"\"\"");

 initializeRules();
}

void PythonSyntaxHighlighter::initializeRules()
{
 foreach (QString currKeyword, keywords)
 {
  rules.append(HighlightingRule(QString("\\b%1\\b").arg(currKeyword), 0, basicStyles.value("keyword")));
 }
 foreach (QString currOperator, operators)
 {
  rules.append(HighlightingRule(QString("%1").arg(currOperator), 0, basicStyles.value("operator")));
 }
 foreach (QString currBrace, braces)
 {
  rules.append(HighlightingRule(QString("%1").arg(currBrace), 0, basicStyles.value("brace")));
 }
 // 'self'
    rules.append(HighlightingRule("\\bself\\b", 0, basicStyles.value("self")));

    // Double-quoted string, possibly containing escape sequences
 // FF: originally in python : r'"[^"\\]*(\\.[^"\\]*)*"'
    rules.append(HighlightingRule("\"[^\"\\\\]*(\\\\.[^\"\\\\]*)*\"", 0, basicStyles.value("string")));
    // Single-quoted string, possibly containing escape sequences
 // FF: originally in python : r"'[^'\\]*(\\.[^'\\]*)*'"
 rules.append(HighlightingRule("'[^'\\\\]*(\\\\.[^'\\\\]*)*'", 0, basicStyles.value("string")));

    // 'def' followed by an identifier
 // FF: originally: r'\bdef\b\s*(\w+)'
    rules.append(HighlightingRule("\\bdef\\b\\s*(\\w+)", 1, basicStyles.value("defclass")));
    //  'class' followed by an identifier
 // FF: originally: r'\bclass\b\s*(\w+)'
 rules.append(HighlightingRule("\\bclass\\b\\s*(\\w+)", 1, basicStyles.value("defclass")));

    // From '#' until a newline
 // FF: originally: r'#[^\\n]*'
    rules.append(HighlightingRule("#[^\\n]*", 0, basicStyles.value("comment")));

    // Numeric literals
    rules.append(HighlightingRule("\\b[+-]?[0-9]+[lL]?\\b", 0, basicStyles.value("numbers"))); // r'\b[+-]?[0-9]+[lL]?\b'
    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'
    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'
}

void PythonSyntaxHighlighter::highlightBlock(const QString &text)
{ 
 foreach (HighlightingRule currRule, rules)
 {
  int idx = currRule.pattern.indexIn(text, 0);
  while (idx >= 0)
  {
   // Get index of Nth match
   idx = currRule.pattern.pos(currRule.nth);
   int length = currRule.pattern.cap(currRule.nth).length();
   setFormat(idx, length, currRule.format);
   idx = currRule.pattern.indexIn(text, idx + length);
  }
 }
    
 setCurrentBlockState(0);

    // Do multi-line strings
    bool isInMultilne = matchMultiline(text, triSingleQuote, 1, basicStyles.value("string2"));
    if (!isInMultilne)
  isInMultilne = matchMultiline(text, triDoubleQuote, 2, basicStyles.value("string2"));
}

bool PythonSyntaxHighlighter::matchMultiline(const QString &text, const QRegExp &delimiter, const int inState, const QTextCharFormat &style)
{
    int start = -1;
 int add = -1;
 int end = -1;
 int length = 0;

 // If inside triple-single quotes, start at 0
 if (previousBlockState() == inState) {
        start = 0;
        add = 0;
 }
 // Otherwise, look for the delimiter on this line
 else { 
        start = delimiter.indexIn(text);
        // Move past this match
        add = delimiter.matchedLength();
 }

    // As long as there's a delimiter match on this line...
    while (start >= 0) {
        // Look for the ending delimiter
        end = delimiter.indexIn(text, start + add);
        // Ending delimiter on this line?
        if (end >= add) {
            length = end - start + add + delimiter.matchedLength();
            setCurrentBlockState(0);
  }
        // No; multi-line string
        else {
            setCurrentBlockState(inState);
            length = text.length() - start + add;
  }
        // Apply formatting and look for next
        setFormat(start, length, style);        
        start = delimiter.indexIn(text, start + length);
 }
    // Return True if still inside a multi-line string, False otherwise
    if (currentBlockState() == inState)
        return true;
    else
        return false;
}

const QTextCharFormat PythonSyntaxHighlighter::getTextCharFormat(const QString &colorName, const QString &style)
{
 QTextCharFormat charFormat;
 QColor color(colorName);
 charFormat.setForeground(color);
 if (style.contains("bold", Qt::CaseInsensitive))
  charFormat.setFontWeight(QFont::Bold);
 if (style.contains("italic", Qt::CaseInsensitive))
  charFormat.setFontItalic(true);
 return charFormat;
}




$Id: KickPythonSyntaxHighlighter.h 167 2013-11-03 17:01:22Z oliver $
This is a C++ port of the following PyQt example
http://diotavelli.net/PyQtWiki/Python%20syntax%20highlighting
C++ port by Frankie Simon (docklight.de, www.fuh-edv.de)

The following free software license applies for this file ("X11 license"): 

Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial 
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef KICKPYTHONSYNTAXHIGHLIGHTER_H
#define KICKPYTHONSYNTAXHIGHLIGHTER_H




#include <QSyntaxHighlighter>

//! Container to describe a highlighting rule. Based on a regular expression, a relevant match # and the format.
class HighlightingRule
{
public: 
 HighlightingRule(const QString &patternStr, int n, const QTextCharFormat &matchingFormat)
 {
  originalRuleStr = patternStr;
  pattern = QRegExp(patternStr);
  nth = n;
  format = matchingFormat;
 } 
 QString originalRuleStr;
 QRegExp pattern;
 int nth;
 QTextCharFormat format;
};

//! Implementation of highlighting for Python code.
class KickPythonSyntaxHighlighter : public QSyntaxHighlighter
{
 Q_OBJECT
public:
 KickPythonSyntaxHighlighter(QTextDocument *parent = 0);
protected:
     void highlightBlock(const QString &text);
private:
 QStringList keywords;
 QStringList operators;
 QStringList braces;

 QHash<QString, QTextCharFormat> basicStyles;
 
 void initializeRules();
 
 //! Highlighst multi-line strings, returns true if after processing we are still within the multi-line section.
 bool matchMultiline(const QString &text, const QRegExp &delimiter, const int inState, const QTextCharFormat &style);
 const QTextCharFormat getTextCharFormat(const QString &colorName, const QString &style = QString());

 QList<HighlightingRule> rules;
 QRegExp triSingleQuote;
 QRegExp triDoubleQuote;
};

#endif


Using a QSyntaxHighlighter for QTextEdit is a one-liner, e.g.
PythonSyntaxHighlighter *pythonHighlighter = new PythonSyntaxHighlighter(ui.plainTextEditScript->document());




Comments

  1. Is the header for this published anywhere? I'd like to try it out, but it's a little annoying trying to infer some of the types.

    ReplyDelete
    Replies
    1. in fact, forgot to add the header. Corrected. Sorry for the guesswork. I somehow forgot about this, because as mentioned in the end we went for a Scintilla based solution.

      Delete
    2. Much obliged. I know there are more generic syntax highlighting options out there, but for a current project, this was basically the perfect little thing to drop in without any fuss. (Okay, I fussed over the colors for a minute to match my app's theme. But that was the only fuss.)

      Delete

Post a Comment

Popular posts from this blog

Windows DLL for Microchip PIC32 firmware update / UBL bootloader

Update 2017: Microchip now has a Qt 5 example in their MLA folder "apps\usb\device\bootloaders\utilities". It uses the popular HIDAPI by Alan Ott / Signal 11.   It makes the below approach obsolete , and I really recommend to download and check out the Microchip MLA package instead. ============================================== While working on a Microchip PIC32 based USB HID Composite Device for our Kickdrive environment, we realized the need for updating the PIC application firmware from our own UI, instead of using an external tool.  Firmware updates for any Embedded device or accessory are an essential part of product support and should be done in the least disrupting way. So even launching an external Windows command line tool did not look like a good solution to us. What I found was Microchip's own sample code  from  Application Note AN1388 , but the PIC32UBL.exe code was MFC / Visual Studio 2003 and not designed to be anything else th...

On CRCs and CRC Reverse Engineering

Last week I ran into this interesting CRC Reverse Engineering challenge at Stackoverflow . I immediately felt this is something I should be able to figure out using our Docklight Scripting tool , and indeed I managed to find the correct CRC within two hours. (Update 2016 - the Docklight CRC Finder also found the answer for this second CRC puzzle .) Now here are my personal sentiments about CRC: The fact that the most frequently cited reference is called  A Painless Guide to CRC Error Detection Algorithms  only proves - it's painful. It really is. So if you are with CRC aches, too, here is my personal list of painkillers on the subject: Sven Reifegerte's great Online CRC Calculator  and his related C sample implementations . A somewhat comforting article about  CRC Implementation Code in C . This article was originally called "Easier said than done (Michael Barr) - A guide to CRC calculation", and I much respect the honesty.  The Boost CRC ...