如何让我的“查找"对话框不干扰 StyledTextCtrl 中的词法分析器? [英] How do I make my Find dialog not interfere with lexer in StyledTextCtrl?

查看:18
本文介绍了如何让我的“查找"对话框不干扰 StyledTextCtrl 中的词法分析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助让我的查找对话框不干扰我的词法分析器.我在这个问题中使用了查找对话框,但它使用干扰我的词法分析器的 SetStyles.除了弄乱语法突出显示,当我再次尝试执行 Find 时,它只会突出显示随机的东西.任何帮助表示赞赏!

I need help in making my find dialog not interfere with my lexer. I used the find dialog in this question but it uses SetStyles which interferes with my lexer. Along with messing up the syntax highlighting, when I try to do the Find again, it just highlights random things. Any help is appreciated!

import wx
import wx.stc as stc
import keyword

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.tc = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
        self.bt_find = wx.Button(self, -1, "find")
        self.bt_css = wx.Button(self, -1, "CSS")

        self.Bind(wx.EVT_BUTTON, self.on_button, self.bt_find)
        self.Bind(wx.EVT_FIND, self.on_find)
        self.Bind(wx.EVT_BUTTON, self.CSS, self.bt_css)

        self.pos = 0
        self.size = 0
        #
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tc, 1, wx.EXPAND, 0)
        sizer.Add(self.bt_find, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        sizer.Add(self.bt_css, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()

    def on_button(self, event):
        self.txt = self.tc.GetValue()
        self.data = wx.FindReplaceData()   # initializes and holds search parameters
        dlg = wx.FindReplaceDialog(self.tc, self.data, 'Find')
        dlg.Show()

    def on_find(self, event):
        self.tc.StartStyling(pos=0, mask=0xFF)
        self.tc.SetStyling(length=len(self.txt), style=0)
        fstring = event.GetFindString()
        self.size = len(fstring)
        while True:
            self.pos = self.txt.find(fstring, self.pos)
            if self.pos < 0:
                break
            self.tc.StyleSetSpec(1, "fore:#FF0000,back:#000000")
            self.tc.StartStyling(pos=self.pos, mask=0xFF)
            self.tc.SetStyling(length=self.size, style=1)
            self.pos += 1
        self.pos = 0

    def CSS(self, e):
        self.tc.SetLexer(stc.STC_LEX_CSS)
        self.tc.SetKeyWords(0, " ".join(keyword.kwlist))
        self.tc.StyleSetSpec(wx.stc.STC_CSS_ATTRIBUTE, 'fore:#0000FF')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_CLASS, 'fore:#0000FF')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_COMMENT, 'fore:#008000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_DEFAULT, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_DIRECTIVE, 'fore:#0000FF')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_DOUBLESTRING, 'fore:#800080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_ID, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_IDENTIFIER, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_IDENTIFIER2, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_IMPORTANT, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_OPERATOR, 'fore:#800000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_PSEUDOCLASS, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_SINGLESTRING, 'fore:#800080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_TAG, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_UNKNOWN_IDENTIFIER, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_UNKNOWN_PSEUDOCLASS, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_VALUE, 'fore:#668B8B')

if __name__ == "__main__":

    app = wx.App()
    frame_1 = MyFrame(None, wx.ID_ANY, "")
    frame_1.Show()
    app.MainLoop()

推荐答案

首先,您应该使用词法分析器未使用的样式编号,请参阅 _stc.py 中的样式代码.您还可以使用 Colourise(0, -1) 方法清除自定义样式并将默认词法分析器的颜色重新应用于整个文档(请参阅 StyledTextCtrl 文档).

At first, you should use style number that is not used by lexer, see style codes in _stc.py. You can also use Colourise(0, -1) method to clear your custom styling and reapply default lexer's colorizing to whole document (see StyledTextCtrl documentation).

import wx
import wx.stc as stc
import keyword

SCE_FIND = 24  # style for highlighting of matching text

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.tc = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
        self.bt_find = wx.Button(self, -1, "find")
        self.bt_css = wx.Button(self, -1, "CSS")

        self.Bind(wx.EVT_BUTTON, self.on_button, self.bt_find)
        self.Bind(wx.EVT_FIND, self.on_find)
        self.Bind(wx.EVT_FIND_CLOSE, self.on_find_close)
        self.Bind(wx.EVT_BUTTON, self.CSS, self.bt_css)

        self.pos = 0
        self.size = 0
        #
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.tc, 1, wx.EXPAND, 0)
        sizer.Add(self.bt_find, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        sizer.Add(self.bt_css, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        self.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()

        self.tc.StyleSetSpec(SCE_FIND, "fore:#FF0000,back:#000000")

    def on_button(self, event):
        self.txt = self.tc.GetValue()
        self.data = wx.FindReplaceData()   # initializes and holds search parameters
        dlg = wx.FindReplaceDialog(self.tc, self.data, 'Find', wx.FR_NOUPDOWN | wx.FR_NOMATCHCASE)
        dlg.Show()

    def on_find(self, event):
        fstring = event.GetFindString()
        self.size = len(fstring)
        while True:
            self.pos = self.txt.find(fstring, self.pos)
            if self.pos < 0:
                break

            self.tc.StartStyling(pos=self.pos, mask=0xFF)
            self.tc.SetStyling(length=self.size, style=SCE_FIND)
            self.pos += 1
        self.pos = 0

    def on_find_close(self, event):
        self.tc.Colourise(0, -1)

    def CSS(self, e):
        self.tc.SetLexer(stc.STC_LEX_CSS)
        self.tc.SetKeyWords(0, " ".join(keyword.kwlist))
        self.tc.StyleSetSpec(wx.stc.STC_CSS_ATTRIBUTE, 'fore:#0000FF')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_CLASS, 'fore:#0000FF')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_COMMENT, 'fore:#008000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_DEFAULT, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_DIRECTIVE, 'fore:#0000FF')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_DOUBLESTRING, 'fore:#800080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_ID, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_IDENTIFIER, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_IDENTIFIER2, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_IMPORTANT, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_OPERATOR, 'fore:#800000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_PSEUDOCLASS, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_SINGLESTRING, 'fore:#800080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_TAG, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_UNKNOWN_IDENTIFIER, 'fore:#000000')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_UNKNOWN_PSEUDOCLASS, 'fore:#008080')
        self.tc.StyleSetSpec(wx.stc.STC_CSS_VALUE, 'fore:#668B8B')

if __name__ == "__main__":

    app = wx.App()
    frame_1 = MyFrame(None, wx.ID_ANY, "")
    frame_1.Show()
    app.MainLoop()

这篇关于如何让我的“查找"对话框不干扰 StyledTextCtrl 中的词法分析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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