Skip to content

2010年02月9日

解决StyledTextCtrl自定义高亮在大文件下的效率问题

StyledTextCtrl中自定义语法高亮,主要是设置Lexer为STC_LEX_CONTAINER,然后在EVT_STC_STYLENEEDED事件中对文档进行着色处理;当EVT_STC_STYLENEEDED事件发生时候,获取起始和结束位置进行SetStyling的操作。当打开一个较大文件的时候,修改了文档中的某个位置,EVT_STC_STYLENEEDED会多次发出第一次通知后,在滚动条拖动时候会接着上次的结束位置多次发出通知…具体原因还得再研究;这种情况下,大文件的编辑就会非常吃力,所以得想办法停止事件的发生。

查阅文档并且google后,再查到一国外强人回帖:

An earlier message indicated that you use SCLEX_NULL. SCLEX_NULL
tries to maintain the style information as all 0s. To do this without
performing lots of unnecessary writing it just updates the last style
byte so that the style cursor is moved to the end of the document.

If you want to perform your own styling then use SCLEX_CONTAINER.
If you don’t want to perform any styling within the SCN_STYLENEEDED
notification, you should still move the styling cursor by calling
SCI_STARTSTYLING with the notification position (or the end of the
document) so that Scintilla will believe that styling has been
performed up to that point and not continue asking for more styling.

修改程序,在EVT_STC_STYLENEEDED事件中的着色处理完成后,使用StartStyling方法把style cursor指向文档结束位置:“style cursor is moved to the end of the document”, 问题解决。

最后,着色的起始位置应该根据语法,做一些分析,不应该仅仅是行的开头。

Read more from Python, 笔记

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments