Question

I'm writing an RSL editor for a personal project, and i'd like to customize the CPP lexer available in QScintilla because all i need is just a few extra keywords to be highlighted, but i cant really find out how to add them.

any help? cheers

edit - Ive been playing with snippets ive found and ive managed to get new keywords to work by subclssing the CPP lexer and creating a key set, but it only works if o overwrite the existing keyset on index 1

from PyQt4 import Qsci

class RSLLexer(Qsci.QsciLexerCPP): 
    def __init__(self, parent): 
        super(RSLLexer, self).__init__()

def keywords(self, keyset):
    if keyset == 1:
        return b'surface'
    return Qsci.QsciLexerCPP.keywords(self, keyset)
Was it helpful?

Solution

Create a subclass of QsciLexerCPP and reimplement the keywords method:

class RSLLexer(Qsci.QsciLexerCPP):
    def keywords(self, index):
        keywords = Qsci.QsciLexerCPP.keywords(self, index) or ''
        # primary keywords
        if index == 1:
            return 'foo ' + keywords
        # secondary keywords
        if index == 2:
            return 'bar ' + keywords
        # doc comment keywords
        if index == 3:
            return keywords
        # global classes
        if index == 4:
            return keywords
        return keywords

Each of these keyword sets has a different style associated with it, so they can be highlighted differently. See the style enumeration for which ones to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top