语法高亮显示在Cocoa TextView?经验?建议?想法? [英] Syntax Highlighting in Cocoa TextView? Experiences? Suggestions? Ideas?
问题描述
可能重复:
Cocoa应用程式的语法着色
在 Cocoa TextView中语法高亮。
I'm interested in syntax highlighting in a Cocoa TextView.
我找到了几个资源:
-
使用flex的方法,通过与
textStorageDidProcessEditing
$匹配的 flex 模式b $ b在TextView
委托中。在这种方法中,整个字符串在每个输入事件上被解析,因此性能下降。
approach with flex, via a flex pattern matched against
textStorageDidProcessEditing
in aTextView
delegate. In this approach the whole string get parsed on each input event, hence performance degrades.
CocoaDev有自己的语法高亮主题页面:
CocoaDev has an own page on the topic of syntax highlighting:
-
使用
NSTextStorageDidProcessEditingNotification
,然后获取编辑的范围,并只应用着色。范围可能是wordbound或任何东西;
Use
NSTextStorageDidProcessEditingNotification
, then get the edited range, and just apply the coloring there. The range might be wordboundaries or anything; this definitely improves performance.
此处提及:例如,Xcode仅对当前屏幕上的文本进行着色,
Mentioned there: Xcode, for example, only colorizes text that's currently on-screen, and defers colorizing the rest of the document until you scroll through it. How would one implement this?
使用 NSLayoutManager
- 通过临时属性[which]仅用于屏幕绘图,并且不以任何方式持久化... ,但文档说,但是不会对最后编辑的范围着色
Use NSLayoutManager
– via Temporary attributes [which] are used only for on-screen drawing and are not persistent in any way... as the docs say, but that doesn't color the last edited range, until a whitespace character is entered.
自定义帮助器,例如 UKSyntaxColoredDocument - 不错,但是语言定义是通过属性列表完成的;如何使用附加/现有语言定义?
Custom Helper like UKSyntaxColoredDocument – nice, but language definition is done via property list; how to use additional/existing language definitions?
没有一种方法对我来说真的可扩展或强大(除了4.也许..)。
None of the approaches seem really extensible or robust to me (except the 4. maybe ..).
我知道强大的现有库SH像 pygments ;和 PyObjC 。
I am aware of robust existing libraries for SH like pygments; and of PyObjC.
问题:如何使用一些现有的库,例如像 pygments 在Cocoa TextView $ c $中具有可扩展和高效的语法高亮显示c>
Question: How would it be possible to use some existing library e.g. like pygments to have an extensible and performant syntax highlighting in a Cocoa TextView
?
:我知道这个问题很宽泛欢迎经验和建议以及解决方案。谢谢。
Note: I know this question is very broad (and much too long). Experiences and suggestions as well as solutions are welcome. Thanks.
找到另一个类似的主题: Cocoa应用程序的语法着色
Found another similar thread on that matter: Syntax coloring for Cocoa app
推荐答案
请查看 Smultron 的源代码。它有非常好的语法高亮。它使用NSTextView的子类来做大部分繁重的工作。该代码使用布局管理器添加属性到文本,并使用一些其他聪明的技巧,只强调文档必要的尽可能多。
I would suggest taking a look at the source code to Smultron. It has very nice syntax highlighting. It uses a subclass of NSTextView to do most of the heavy lifting. The code uses the layout manager to add attributes to the text and uses some other clever tricks to only highlight as much of the document as necessary.
这篇关于语法高亮显示在Cocoa TextView?经验?建议?想法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!