【Qt】 Scintilla 自定义语法着色器(Lexer)

来源:nabble.com

上文:代码编辑器Scintilla的使用方法 说到 Scintilla 内置众多代码高亮方案,而本文则是自定义一套方案。

class CustomLexer(Qsci.QsciLexerCustom):

    def __init__(self, parent): 
        Qsci.QsciLexerCustom.__init__(self, parent) 
        self._styles = { 
         0: 'Default', 
         1: 'Comment', 
         2: 'Key', 
         3: 'Assignment', 
         4: 'Value', 
        }
        for key,value in self._styles.items(): 
            setattr(self, value, key) 

    def description(self, style): 
        return self._styles.get(style, '') 

    def defaultColor(self, style): 
        if style == self.Default: 
            return QtGui.QColor('#000000') 
        elif style == self.Comment: 
            return QtGui.QColor('#C0C0C0') 
        elif style == self.Key: 
            return QtGui.QColor('#0000CC') 
        elif style == self.Assignment: 
            return QtGui.QColor('#CC0000') 
        elif style == self.Value: 
            return QtGui.QColor('#00CC00') 
        return Qsci.QsciLexerCustom.defaultColor(self, style) 

    def styleText(self, start, end): 
        editor = self.editor() 
        if editor is None: 
            return

        # scintilla works with encoded bytes, not decoded characters. 
        # this matters if the source contains non-ascii characters and 
        # a multi-byte encoding is used (e.g. utf-8) 
        source = '' 
        if end > editor.length(): 
            end = editor.length() 
        if end > start: 
            # if sys.hexversion >= 0x02060000: 
                # faster when styling big files, but needs python 2.6 
                source = bytearray(end - start) 
                editor.SendScintilla(editor.SCI_GETTEXTRANGE, start, end, source)
            # else: 
            #     source = unicode(editor.text() 
            #                     ).encode('utf-8')[start:end] 
        if not source: 
            return 

        # the line index will also be needed to implement folding 
        index = editor.SendScintilla(editor.SCI_LINEFROMPOSITION, start) 
        if index > 0: 
            # the previous state may be needed for multi-line styling 
            pos = editor.SendScintilla(editor.SCI_GETLINEENDPOSITION, index - 1) 
            state = editor.SendScintilla(editor.SCI_GETSTYLEAT, pos) 
        else: 
            state = self.Default

        self.startStyling(start, 0x1f) 

        # scintilla always asks to style whole lines 
        for line in source.splitlines(True): 
            length = len(line) 
            if line.startswith(b'#'): 
                state = self.Comment 
            else: 
                # the following will style lines like "x = 0" 
                pos = line.find(b'=') 
                if pos > 0: 
                    self.setStyling(pos, self.Key) 
                    self.setStyling(1, self.Assignment) 
                    length = length - pos - 1 
                    state = self.Value 
                else: 
                    state = self.Default 
            self.setStyling(length, state) 
            # folding implementation goes here 
            index += 1 

以上代码可以实现两个小功能——注释和赋值的高亮显示。

效果图
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 酒不醉人,人自醉,但愿潇潇洒洒,仗剑走天涯,歌声在哪里,快乐就会飞向哪里,你说春天在哪里,她说幸福就在我的心里 嘎...
    拉小提琴的罗宾汉阅读 215评论 1 0
  • 看到这个题目,想起早一阵子我听到的一个传闻:行业里一家公司从新三板退市了,且可能会被行业里的一家大公司收购,听闻这...
    扁舟侠阅读 258评论 0 1
  • 有一个很有趣的现象,就是不管男女老少很少有人讨厌胡歌,不一定每个人都是胡歌的粉丝,但是几乎很难找出不喜欢他的人,出...
    蹙眉_871e阅读 380评论 0 0
  • 如果我能够从头活过, 我会试着犯更多的错。 我会放松一点,我会灵活一点。 我会比这一趟过得傻...
    文痞看世界阅读 319评论 0 0