简体   繁体   English

在现有的Scintilla词法分析器上创建和着色新构造

[英]Creating and colorizing new constructs on a existing Scintilla lexer

All, 所有,

I'm using QScintilla to syntax-highlight my domain specific language (DSL). 我正在使用QScintilla语法突出我的域特定语言(DSL)。

Since my DSL is based on python, I use the existing Python Lexer for QScintilla. 由于我的DSL基于python,我使用现有的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? 现在,如何选择颜色以突出显示我新创建的关键字?

Thanks a lot! 非常感谢!

The QsciLexerPython is quite limited when it comes to highlighting sets of keywords, as it only gives you two to play with. QsciLexerPython在突出显示关键字集方面非常有限,因为它只为您提供了两个关键字。 This limitation is imposed by the Python Lexer class from the underlying Scintilla library, so there's not much that can be done about it (unless you want to create a patch). 这个限制是由基础Scintilla库中的Python Lexer类强加的,所以没有太多可以做的事情(除非你想创建一个补丁)。

However, if you only need to highlight one extra set of keywords, then you can subclass QsciLexerPython and reimplement its keywords method: 但是,如果您只需要突出显示一组额外的关键字,那么您可以QsciLexerPython并重新实现其关键字方法:

class CustomLexer(QsciLexerPython):
    def keywords(self, keyset):
        if keyset == QsciLexerPython.HighlightedIdentifier:
            return b'WARNING'
        return QsciLexerPython.keywords(self, keyset)

With that in place, you can then set the color, font, etc for the style: 有了它,您可以设置样式的颜色,字体等

    pythonLexer = CustomLexer(self.text)
    pythonLexer.setColor(
        QColor('purple'), QsciLexerPython.HighlightedIdentifier)
    ...

(PS: note that keywords can only contain single-byte characters in the range 0-255) (PS:请注意,关键字只能包含0-255范围内的单字节字符)

To gain even more flexibility, you can consider to build your own custom lexer, not derived from the existing QsciLexerPython one. 为了获得更大的灵活性,您可以考虑构建自己的自定义词法分析器,而不是从现有的QsciLexerPython派生。 Watch out - it will be more work. 小心 - 这将是更多的工作。

QScintilla provides the QsciLexerCustom class for this purpose. QScintilla为此提供了QsciLexerCustom类。 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 ---'''

Please notice the following parts: 请注意以下部分:

  • __init__(self, parent) : The constructor is typically where you create style-objects. __init__(self, parent) :构造函数通常是创建样式对象的位置。

  • language(self) : This method must return the name of the language. language(self) :此方法必须返回语言的名称。 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. description(self, style_nr) :返回给定样式的描述性名称。

  • styleText(self, start, end) : Gets called everytime editors text has changed. styleText(self, start, end) :每次编辑器文本更改时都会调用。 Here you implement syntax highlighting! 在这里实现语法高亮!

For more details, you can visit the following website: https://qscintilla.com/subclass-qscilexercustom/ 有关详细信息,请访问以下网站: https//qscintilla.com/subclass-qscilexercustom/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM