What is the best way to implement syntax highlighting of source code in Cocoa?

Vadim picture Vadim · Apr 16, 2009 · Viewed 10k times · Source

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!

Answer

Jens Ayton picture Jens Ayton · Apr 16, 2009

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.