[QScintilla] Syntax highlighting for custom lexer stopped working after upgrade

Phil Thompson phil at riverbankcomputing.com
Tue Nov 13 13:33:05 GMT 2018


As you suggest I think the problem is the fact that the second argument to startStyling() is no longer used. (This is a change in the underlying Scintilla code.) The argument allowed you to do multiple passes over the text setting a different style in each pass while leaving other styles in place. You now have to do all styling in the same pass which unfortunately makes regexps more difficult to use.

Phil

> On 12 Nov 2018, at 1:42 pm, Jan-Georg Van Der Wath <jgvdwath at altair.com> wrote:
> 
> Hi Phil
>  
> The latest version of QScintilla (2.10.8) has the same behaviour mentioned below.
> See my custom lexer code and 2 screenshots showing the behaviour I get. This code worked in Qt4.8.6 with QScintilla 2.7.1
>  
> class Lexer
>     : public QsciLexerCustom
> {
> public:
>     enum Styles
>     {
>         Default = 0,
>         String = 1,
>         Number = 2
>     };
>     void styleText(int start, int end) override
>     {
>         QString source = editor()->text().toUtf8().mid(start, end - start);
>         // String syntax highligting
>         style(QRegExp("\"[^\"]*(?:\\.[^\"]*)*\""), String, source, start, end, true);
>         // Number syntax highligting
>         style(QRegExp("\\b[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?\\b"), Number, source, start, end);
>     }
>     const char* language() const override
>     {
>         return "lex";
>     }
>     QString description(int style) const override
>     {
>         switch (style)
>         {
>         case Default:
>             return "Default";
>         case String:
>             return "String";
>         case Number:
>             return "Number";
>         }
>         return QString();
>     }
> private:
>  
>     QColor color(int style) const
>     {
>         switch (style)
>         {
>         case Default:
>             return Qt::darkGray;
>         case String:
>             return Qt::darkGreen;
>         case Number:
>             return Qt::darkBlue;
>         }
>         return QsciLexer::color(style);
>     }
>     QFont font(int style) const
>     {
>         QFont styledFont = defaultFont();
>         switch (style)
>         {
>         case String:
>             styledFont.setBold(true);
>             break;
>         case Number:
>             styledFont.setItalic(true);
>             break;
>         }
>         return styledFont;
>     }
>     QColor paper(int style) const
>     {
>         return QsciLexer::paper(style);
>     }
>     void setFont(const QFont &newFont, int style)
>     {
>         QsciLexer::setFont(newFont, style);
>         QsciLexer::setDefaultFont(newFont);
>         QsciLexer::setFont(font(Number), Number);
>     }
>  
>     QsciStyle getStyle(int style)
>     {
>         switch (style)
>         {
>         case Default:
>         case String:
>         case Number:
>             return QsciStyle(style, description(style), color(style), Qt::white, font(style));
>         }
>         return QsciStyle(style);
>     }
>     void style(QRegExp regExp, Styles style, const QString& source, int start, int end, bool overWrite = false)
>     {
>         int pos = 0;
>         int count = 0;
>         startStyling(start, style);
>         while ((pos = regExp.indexIn(source, pos)) != -1)
>         {
>             setStyling(pos - count, getStyle(Default));
>             if (overWrite)
>             {
>                 startStyling(start + pos);
>             }
>             setStyling(regExp.matchedLength(), getStyle(style));
>             pos += regExp.matchedLength();
>             count = pos;
>             if (overWrite)
>             {
>                 startStyling(start + pos, style);
>             }
>         }
>         setStyling(end - (start + count), getStyle(Default));
>     }
> };
>  
> When I run the lexer above I get
> <image001.png>
>  
> Then commenting out the number styling I get
> <image002.png>
>  
> Our customer ID is 01-005505.
>  
> Kind regards
>  
> Jan-Georg van der Wath | Senior Software Developer – Altair EM Solutions
>  
> jgvdwath at altair.com | altair.com
>  
> Altair | Innovation Intelligence®
>  
> This message is a private communication intended only for the use of the addressee and may contain information that is PRIVILEGED and CONFIDENTIAL. If you are not the intended recipient, you are hereby notified that any dissemination of this communication is strictly prohibited. If you have received this communication in error, please erase all copies of the message and its attachments and notify the sender by replying to this message. Thank you.
>  
>  
>  
> From: Jan-Georg Van Der Wath 
> Sent: Thursday, October 25, 2018 4:04 PM
> To: 'qscintilla at riverbankcomputing.com' <qscintilla at riverbankcomputing.com>
> Subject: Syntax highlighting for custom lexer stopped working after upgrade
>  
> Hi Phil
>  
> Our custom lexer stopped working after upgrading from Qt4.8.6 to Qt5.9.6. With the upgrade we jumped a couple of QScintilla versions as well, from 2.7.1 to 2.9.3
> I noticed in the release notes for 2017-01-03:
> * qt/qscilexercustom.cpp, qt/qscilexercustom.h:
>         Fixed QsciLexerCustom::startStyling() now that the 2nd argument
>         isn't used.
>         [2d4cc3cdb123]
> What does this fix imply? The behavior I am seeing is that the last style I apply is either overwriting all my previous styling or resetting the style.
>  
> Regards
> Jan-Georg van der Wath | Senior Software Developer – Altair EM Solutions
>  
> jgvdwath at altair.com | altair.com
>  
> Altair | Innovation Intelligence®
>  
> This message is a private communication intended only for the use of the addressee and may contain information that is PRIVILEGED and CONFIDENTIAL. If you are not the intended recipient, you are hereby notified that any dissemination of this communication is strictly prohibited. If you have received this communication in error, please erase all copies of the message and its attachments and notify the sender by replying to this message. Thank you.



More information about the QScintilla mailing list