I'm working on Cocoa Application that should be able to analyze and highlight a source code written in such languages as Objective-C, SQL, JavaScript, Python etc. The text should be editable too (by using NSTextView, for example).
Please advise what is the best way to implement such feature in Mac OS X 10.5.
There is no need to support auto-completion and other extra-features. Simple formatting by keywords, strings, #includes, and numbers but with cool performance is just OK.
Also, it would be great if configuration by languages were supported.
Thanks!
In preparing a project that will require something similar, I’ve found the following (but not evaluated them in depth):
- /Developer/Examples/OpenGL/Cocoa/GLSLEditorSample implements syntax highlighting.
- IDEKit, “an open-source framework that provides programmer source code editing, including syntax coloring, popup functions, and split frames”. This hasn’t been updated for a while (it touts 10.2 and 10.3 compatibility, and comes with a Project Builder project – can be converted with Xcode 2.5).
- UKSyntaxColoredTextDocument.
I'd second Ahruman's advice - particulary looking at Uli's UKSyntaxColoredTextDocument as that will point you in the right direction as to which classes etc. you are going to have to learn about.
This is another excellent source of information:
http://www.cocoadev.com/index.pl?ImplementSyntaxHighlighting
Good luck - Syntax highlighting is hard to get right, but lots of fun along the way!
I had to look for a syntax highlighting solution for Cocoa recently and after searching on Stack Overflow, Google and GitHub couldn't find anything modern, easy to re-use and still in active development.
It seems most syntax highlighters these days are written for the web. Fortunately it's actually quite easy to embed them in a WebView to use in any Cocoa app. Communicating with the editor can be done through WebKit's WebScriptObject protocol.
Here's an example wrapper for the popular JavaScript text editor CodeMirror for Cocoa:
https://github.com/swisspol/CodeMirrorView
You can use Pygments, which is written in python, to generate an HTML representation of the source code, which can then be presented in a Web View.
(Note, as reflected in comment below - this answer was to the original question, which did not mention editable text.)
You might consider using flex as described (in German) here [1]. You can find an example of this approach here [2].
[1] http://www.osxentwicklerforum.de/thread.php?postid=14764
[2] http://objectiveous.github.com/davenport/