Search code examples
c++qtqt4qt5qscintilla

QScintilla - Add color to words in a custom lexer


I am trying to create a custom lexer based off of JavaScript for QScintilla. I have figured out how to add keywords the the lexer. However, I can not figure out how to alter the way they look when typed in like it does when you type the word function, for example.

Like here

I need to figure out how to do this with, for example, the word "fill".

Here's the code I currently have:

    QsciLexer *lexer=new QsciLexerJavaScript;
    QsciAPIs *api = new QsciAPIs(lexer);
    api->add("fill");
    api->prepare();
    ui->textEdit->setLexer(lexer);

Solution

  • You need to subclass the QsciLexerCustom class. Then you need to make/configure several QsciStyle objects inside that class. The actual syntax highlighting is done in the styleText() function, which you need to override.

    You can find detailed explanation on this website:

    https://qscintilla.com/

    More specifically on this page:

    https://qscintilla.com/syntax-highlighting/

    I hope it helps