语法高亮显示的最佳方法 [英] Best method for syntax highlighting

查看:105
本文介绍了语法高亮显示的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们.

我正在研究一种语法突出显示工具,其中我当前的突出显示方法(RichTextBox + TextChanged +正则表达式)变得不流畅和缓慢.我花了几天的时间寻找其他方法,而我找到的最好的方法是ICSharpCode.TextEditor,但是当我研究它时,我找不到能突出显示其代码的代码. >
那么你们中有人在这里有一些经验吗?任何帮助将不胜感激,但是代码示例更多.PS:我当前的荧光笔大约需要10秒才能突出显示2100行代码,而ICSharpCode.TextEditor大约需要1-2秒.

Theo

Hey guys.

I''m working on a syntax highlighter, in which my current method of highlighting (RichTextBox + TextChanged + Regular Expressions) has become to unsmooth and slow. I''ve literally spent days searching for other methods, and the best one I''ve found was the ICSharpCode.TextEditor, however when taking I look into it I was unable to find the code in which it highlights its code.

So any of you guys have some experience here? Any help''d be appreciated, but code examples more.
PS: my current highlighter is about 10 seconds about highlighting 2100 lines of code, where ICSharpCode.TextEditor is about 1 - 2 seconds.

Theo

推荐答案

您好,

您正在按照合乎逻辑的方式进行操作,就像大多数人在此询问有关该主题的问题一样;但是,IMO出于多种原因是完全不合适的:

1.实时出价工具是一种愚蠢的控件,它无法很好地缩放,因为它会将所有文本都保存为一个大字符串,因此它可能无法正常运行100行,它注定无法为您提供10,000行文本.我根据面板进行了自己的编辑.这需要:
-键盘和鼠标处理程序自己进行编辑;
-使用Graphics.DrawString的Paint处理程序.

2.正则表达式是功能强大的工具,具有很高的性能.我倾向于不使用它,除了在您的应用程序用户手中提供复杂的搜索/替换功能.为了获得最大速度,请使用诸如IndexOf和Contains之类的字符串方法编写搜索代码并自己替换内容.

3.最后,我的语法着色器位于我的Paint处理程序中:它跳过所有滚动的行,然后仅解析可见的20至40行,并在到达最后一个可见行时停止.为了正确地跳过文本的开头,我的数据结构使我为文本的每一行保留一些标志,这些标志与多行注释的打开和关闭有关.我的想法是我可以开始解析(修改后的)行,而无需再次解析所有先前的行.


最终结果是:
-我有一个编辑器可以按照我喜欢的方式工作它;
-它的语法会立即变色;
-当我选择一些文本时,它会立即在文本的可见部分突出显示相同的字符串(这对Visual Studio来说非常有用). br>
如果要保留RTB,我对您的建议是用字符串操作替换正则表达式部分.

:)

Hi,

You are doing it in the logical way, just like most people asking questions here about the subject; however IMO it is completely inappropriate for a range of reasons:

1. an RTB is a stupid control that doesn''t scale well as it holds all the text as one large string, so it may or may not work well for 100 lines, it is bound to fail at serving you right for 10,000 lines of text. I did my own editor based on a Panel; that takes:
- keyboard and mouse handlers to do the editing myself;
- a Paint handler using Graphics.DrawString.

2. Regex is a powerful tool with big performance hits; I tend not to use it, except for offering complex search/replace capabilities in the hands of your apps'' users. For maximum speed, code the search and replace stuff yourself, using string methods such as IndexOf and Contains.

3. Finally, my syntax colorizer is inside my Paint handler: it skips all the lines that are scrolled over, then parses just the 20 to 40 lines that are visible, and stops when it reaches the last visible line. In order to correctly skip the beginning of the text, my data structures are such that I keep a few flags for each line of text, related to opening and closing of multi-line comments; the idea is I can start parsing a (modified) line without the need to parse again all previous lines.


The net result is:
- I have an editor that works the way I like it;
- it syntax-colorizes instantaneously;
- when I select some text, it immediately highlights identical strings in the visible part of the text (something extremely useful Visual Studio doesn''t do for me).

My advice to you, assuming you will want to keep the RTB, is to replace the Regex parts by string operations.

:)


这篇关于语法高亮显示的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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