自定义PyQt Qscintilla CPP 词法分析器
我正在为一个个人项目写一个RSL编辑器,我想自定义QScintilla中现有的CPP词法分析器,因为我只需要高亮显示几个额外的关键字,但我找不到如何添加它们的方法。
有没有人能帮帮我?谢谢!
补充一下 - 我一直在玩一些找到的代码片段,成功通过子类化CPP词法分析器并创建一个关键字集合来让新关键字生效,但这只有在我覆盖索引为1的现有关键字集合时才有效。
从PyQt4导入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)
1 个回答
2
创建一个 QsciLexerCPP
的子类,并重新实现 keywords 方法:
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
每一组关键字都有不同的样式,这样它们可以被以不同的方式高亮显示。可以查看 样式枚举 来了解应该使用哪些样式。