Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Highlighting in Cocoa TextView? Experiences? Suggestions? Ideas? [duplicate]

Possible Duplicate:
Syntax coloring for Cocoa app

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

I found several resources:

  • 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 has an own page on the topic of syntax highlighting:

    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.

    2. 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?

    3. 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.

    4. Custom Helper like UKSyntaxColoredDocument – nice, but language definition is done via property list; how to use additional/existing language definitions?

None of the approaches seem really extensible or robust to me (except the 4. maybe ..).

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

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.


Found another similar thread on that matter: Syntax coloring for Cocoa app

like image 864
miku Avatar asked Jun 09 '09 22:06

miku


1 Answers

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.

like image 64
Daniel Avatar answered Nov 07 '22 18:11

Daniel