Syntax Coloring with a NSTextView

Discussion in 'Mac Programming' started by jpanetta, May 1, 2006.

  1. jpanetta macrumors newbie

    May 1, 2006
    I am working on an IDE for writing Z80 assembly programs for my Ti-83+ calculator. Well, really I'm just making a source code editor, a project manager, and a graphical interface to a command-line assembler. Everything mostly "works" now, so I'm starting to go back and fix some of my worse ideas. I wanted to know if this falls in that category:

    I want my text editor to do syntax coloring a la Xcode. I implemented it by programatically setting the font color of tokens that my my program recognizes. It appears to run fast enough as it is accepting input (even though I'm developing/testing this on a 266mhz Powerbook G3!) and only messes up in a few situations. I was wondering if there is a better method than setting font color to investigate before I go ahead and improve my current code. The main problem I have with it now is that when you open a source code file, it scans the entire thing before displaying the window, which takes as much as 10 seconds for my longer z80 programs (remember the computer I'm on). Now either I'm doing something horribly wrong algorithm-wise or I need to make the initial coloring a separate thread. I think Xcode does the coloring this way because if I really look I can see code that is just opened without syntax coloring, and then it almost immediately changes colors. Threading the program seemed to dramatically improve open time when I first tried, but it had the rather adverse side effect of crashing every freaking time I tried it except on really really small files. Maybe I better actually learn how to do multithreaded programs. Perhaps locks will help.

    Yeah, so I know I was rambling a bit, but my main questions is: set the font color using setTextColor: range: or find some other way of implementing coloring?


    Oh, and the reason why it runs at a decent speed when typing (it actually runs much faster than Xcode, which lags on my computer) and not when opening a file is that when it is accepting input, it only looks at the particular token that has changed (space/tab/newline/etc are my delimiters). Currently it doesn't work with copy/pasted code, but I should be able to fix that without too much trouble. When it determines the token that the user has changed/entered, it searches for it in a couple of NSStrings, each of which stands for a certain category of instruction (and therefore will get a certain color).

    So, yeah, does anybody see anything fundamentally wrong with what I'm doing? I haven't asked anybody up until now, and I don't wanna feel like fool later if my ideas suck.


    P.S. This isn't really my first post here. I used to post as "altivec 2003," but I've forgotten my password and decided to take advantage of the fact to change to something less painful. Not that you would recognize that username anyway, but I'm just a little embarrassed to be back at the big "0."
  2. gekko513 macrumors 603


    Oct 16, 2003
    You could look at NSAttributedString, NSMutableAttributedString and NSTextStorage and see if they can be used and if they offer any advantages over your current approach.

    I don't see any revolutionary better ways of doing what you want, if that is any comfort. To that it must be said that I don't have much experience with advanced text editing in Cocoa.
  3. robbieduncan Moderator emeritus


    Jul 24, 2002

Share This Page