I've got an NSTextView
subclass acting as its NSTextStorage
delegate. I'm trying to do 2 things:
- Highlight the text in some ways
- Evaluate the text and then append the answer to the textview.
I'm doing this in two different methods, both invoked by the - (void)textStorageWillProcessEditing:(NSNotification *)notification
delegate callback.
I can do the syntax highlighting just fine, but when it comes to appending my answer, the insertion point jumps to the end of the line and I don't really know why. My evaluate method looks like the following:
NSString *result = ..;
NSRange lineRange = [[textStorage string] lineRangeForRange:[self selectedRange]];
NSString *line = [[textStorage string] substringWithRange:lineRange];
line = [self appendResult:result toLine:line]; // appends the answer
[textStorage replaceCharactersInRange:lineRange withString:line];
Doing that will append my result just fine, but the problem is, as mentioned, the insertion point jumps to the end.
I've tried:
- Wrapping those above calls up in
[textStorage beginEditing]
and-endEditing
. - Saving the selection range (i.e., the insertion point) before changing the text storage so I can reset it afterwards, but no dice.
Am I doing this right? I'm trying to do this the least hackish way, and I'm also unsure if this is the ideal place to be doing my parsing/highlighting. The docs lead me to believe this, but maybe it's wrong.