Cocoa TextView 中的语法高亮显示?经验?建议?想法? [英] Syntax Highlighting in Cocoa TextView? Experiences? Suggestions? Ideas?

查看:17
本文介绍了Cocoa TextView 中的语法高亮显示?经验?建议?想法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Cocoa 应用的语法着色

我对 Cocoa TextView 中的语法高亮感兴趣.

I'm interested in syntax highlighting in a Cocoa TextView.

我找到了几个资源:

  • 使用 flex 的方法,通过 flex 模式匹配 textStorageDidProcessEditingTextView 委托中.在这种方法中,每个输入事件都会解析整个字符串,因此会降低性能.

  • approach with flex, via a flex pattern matched against textStorageDidProcessEditing in a TextView 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:

  1. 使用NSTextStorageDidProcessEditingNotification,然后获取编辑后的范围,并在那里应用颜色.范围可能是单词边界或任何东西;这肯定会提高性能.

  1. 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 ..).

我知道像 pygments 等强大的 SH 现有库;和 PyObjC.

I am aware of robust existing libraries for SH like pygments; and of PyObjC.

问题:如何使用一些现有的库,例如像 pygments 在 Cocoa TextView 中具有可扩展且高性能的语法突出显示?

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 应用的语法着色p>

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屋!

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