Line numbering and syntax highlighting

Discussion in 'Mac Programming' started by Cabbit, Jun 6, 2010.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    Hey there i am building a simple PHP IDE using Objective C via Xcode and Interface Builder.

    So far i've made what is basically a text editor that saves out text files so... meh on that front for now, i'll still need to work out how to build panels like the side bar used in Finder for my file management(I am thinking something like mail with smart folders grouping projects that belong to particular clients).

    Though onto todays question, if i wanted to add line numbering and syntax highlighting to my application is there any tutorials floating about how to do this with NSTextView, and indeed before i go any further is NSTextView the right component for such a task.
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    Syntax coloring, and even facing (font changes) should be handled by scanning and manipulating NSMutableAttributedString - you can retain a NSScanner on the text storage and just shift its location around to track what is being edited, should be simple enough.

    For line numbering, I would suggest you add a NSTableView to the side of the text view and match its row height to the line height of the text view, do -setAutohidesScrollers:NO on its enclosingScrollView and hide the scrollers. You would then have to use the NSTextView's NSLayoutManager to figure out where the lines break so that your table view will either have blank cells for multiple row lines or varying row height. The advantage to the tableView method is that the line numbers are not part of the text view, so they cannot be hit in editing.
     
  3. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #3
    I've done a fair amount of work to get a syntax highlighter working, and tried a number of methods. I'll try to outline what has worked best for me as well as other options.

    To know when editing has been done, use the textStorageDidProcessEditing: delegate method. You can then call NSTextStorage's editedRange: and changeInLength: methods to intelligently determine what needs to be re-highlighted.

    The scanning can be done using an NSScanner; implementing a custom scanner (using NSString's getCharacterAtIndex: or getCharacters:range: methods); or if you want a lot of flexibility, use RegExKitLite. While I haven't done extensive tests, I believe the custom scanner to be the most efficient in most cases.

    The actual colouring is best done using NSTextView's layoutManger temporary attribute methods. In my tests these have been significantly faster than using NSTextStorage's permanent attribute methods.

    If you want line numbering that just works, this blog entry gives you one.

    http://www.noodlesoft.com/blog/2008/10/05/displaying-line-numbers-with-nstextview/
     
  4. Truffy macrumors 6502a

    Truffy

    Joined:
    May 9, 2005
    Location:
    somewhere outside your window...
  5. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #5
    Thanks for the advice and line numbering classes, i'll take a look at them. At this stage of development as i am wanting to learn Objective C at the same time i am wondering if its best to make my own classes or glue together some pre made classes, since i plan on open sourcing it once i've got something close to the level of what i am wanting there is that to consider also.

    BB Edit isn't made by me, wheres the fun in always using everyone else's tools?

    Besides i use Netbeans for most of my development.
    I set out to create my own IDE as a fun project to learn Objective C so thats what i'll do. Same as i could have used Zend Framework for my PHP development but its been far more fun creating my own framework.

    Thanks for the advice i'll noodle into these methods.
     

Share This Page