Creating and colorizing new constructs on a existing Scintilla lexer

对着背影说爱祢 提交于 2019-12-10 21:51:33

问题


All,

I'm using QScintilla to syntax-highlight my domain specific language (DSL).

Since my DSL is based on python, I use the existing Python Lexer for QScintilla. I manage to create new keywords as following:

self.text = Qscintilla(self)
pythonLexer = QsciLexerPython(self.text)
self.text.setLexer(pythonLexer)
self.text.SendScintilla(QsciScintilla.SCI_SETKEYWORDS,1,bytes('WARNING', 'utf-8'))

Now, how do I choose a color to highlight my newly created keywords?

Thanks a lot!


回答1:


The QsciLexerPython is quite limited when it comes to highlighting sets of keywords, as it only gives you two to play with. This limitation is imposed by the Python Lexer class from the underlying Scintilla library, so there's not much that can be done about it (unless you want to create a patch).

However, if you only need to highlight one extra set of keywords, then you can subclass QsciLexerPython and reimplement its keywords method:

class CustomLexer(QsciLexerPython):
    def keywords(self, keyset):
        if keyset == QsciLexerPython.HighlightedIdentifier:
            return b'WARNING'
        return QsciLexerPython.keywords(self, keyset)

With that in place, you can then set the color, font, etc for the style:

    pythonLexer = CustomLexer(self.text)
    pythonLexer.setColor(
        QColor('purple'), QsciLexerPython.HighlightedIdentifier)
    ...

(PS: note that keywords can only contain single-byte characters in the range 0-255)




回答2:


To gain even more flexibility, you can consider to build your own custom lexer, not derived from the existing QsciLexerPython one. Watch out - it will be more work.

QScintilla provides the QsciLexerCustom class for this purpose. You have to subclass it like this:

class MyLexer(QsciLexerCustom):

    def __init__(self, parent):
        super(MyLexer, self).__init__(parent)
        [...]
    ''''''

    def language(self):
        [...]
    ''''''

    def description(self, style):
        [...]
    ''''''

    def styleText(self, start, end):
        # Called everytime the editors text has changed
        [...]
    ''''''

'''--- end class ---'''

Please notice the following parts:

  • __init__(self, parent) : The constructor is typically where you create style-objects.

  • language(self) : This method must return the name of the language. You have to implement it, but what it actually gets used for is unclear to me.

  • description(self, style_nr) : Returns the descriptive name for a given style.

  • styleText(self, start, end) : Gets called everytime editors text has changed. Here you implement syntax highlighting!

For more details, you can visit the following website: https://qscintilla.com/subclass-qscilexercustom/



来源:https://stackoverflow.com/questions/22021294/creating-and-colorizing-new-constructs-on-a-existing-scintilla-lexer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!