1

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)
SketchyManDan
  • 176
  • 1
  • 10

1 Answers1

2

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • When i try your solution i still don't get the extra keywords to reigtser unless i remove the return mykeywords1 + keywords and replace it with just return mykeywords1. Whcih still kinda works because i can just print out the original values and re-add them anyway – SketchyManDan Apr 10 '14 at 04:47
  • @SketchyManDan. I got the indexes wrong: they start at one, not zero! Also, I forgot to guard against the base-class call returning `None`. These two things have now been fixed in my answer, and I've confirmed that they work. One final point: `mykeywords` should end with a space. – ekhumoro Apr 10 '14 at 05:07