在现有的Scintilla词法分析器上创建新结构并为其着色 [英] Creating and colorizing new constructs on a existing Scintilla lexer

查看:585
本文介绍了在现有的Scintilla词法分析器上创建新结构并为其着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我正在使用QScintilla语法突出显示我的域特定语言(DSL).

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

由于我的DSL基于python,因此我将现有的Python Lexer用于QScintilla. 我设法创建新的关键字,如下所示:

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?

非常感谢!

推荐答案

要获得更大的灵活性,您可以考虑构建自己的自定义词法分析器,而不是从现有的QsciLexerPython派生.提防-这将需要更多工作.

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为此提供了QsciLexerCustom类.您必须像这样子类化它:

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 ---'''

请注意以下部分:

  • __init__(self, parent):通常在构造器中创建样式对象.

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

language(self):此方法必须返回语言的名称.您必须实现它,但是它实际用于什么尚不清楚.

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):返回给定样式的描述性名称.

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

styleText(self, start, end):每次编辑器文本更改时都会调用.在这里,您将实现语法高亮显示!

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

有关更多详细信息,您可以访问以下网站: https://qscintilla.com/subclass-qscilexercustom/

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

这篇关于在现有的Scintilla词法分析器上创建新结构并为其着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆