Subclass QsciLexerCustom

Building a custom lexer involves several steps. We’ll go through them one-by-one. The lexer we’re going to build here will be very simple. It’s sole purpose is showing you the basics.

Everything starts at the PyQt5.Qsci.QsciLexerCustom class. You have to subclass this one and implement some methods. The code snippet gives an overview of the overall structure – the skeleton of your subclass. As you can see, these are the methods to override/implement:

  • __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!
     
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 ---'''

The following pages treat these topics one-by-one.