views:

1550

answers:

4

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!

+1  A: 

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

Matthew Schinckel
Thank you Matthew. Sorry, but I forgot to include one more requirement. Thе source code should be editable (placed in the NSTextView, for example). Is it possible to use Pygments to incorporate highlighting into NSTextView to achieve this goal?
Stream
I haven't done anything with pygments other than generate HTML, but I know it can mark up into other formats. It may be that one of those can be a base for displaying the NSTextView.
Matthew Schinckel
+6  A: 

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.
Ahruman
+4  A: 

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!

Dunk
Thank you guys. It definitely looks like the right answer. I'll explore Uli's component carefully, but I'd like to leave this question unanswered for some days.Possibly, somebody will have come up with something new by this time.
Stream
+1  A: 

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/

Rob Evans