Hey guys,
What I am doing is inserting some text into a NSTextView using the code below:
- (void)applySnippet

TagSnippet *)snippet
{
target = [[[NSDocumentController sharedDocumentController] currentDocument] textView];
if (target != nil && snippet != nil)
{
NSRange selectedRange = [target selectedRange];
NSMutableString *text = [[target textStorage] mutableString];
NSMutableString *selectedText = [NSMutableString stringWithString:[text substringWithRange:selectedRange]];
int cursorPosition = [snippet applySnippetToString:selectedText];
cursorPosition += selectedRange.location;
[target replaceCharactersInRange:selectedRange withString:selectedText];
[target setSelectedRange:NSMakeRange(cursorPosition, 0)];
}
}
Now, the text inserts fine, but it seems to cause some undo problems with the NSTextView it was inserted in.
For example, if I first type into my text view some random text, then insert the text, and then type some more after it. If I then place my cursor at the end of the document and try to undo, it'll work until I get to the text I inserted, I then begin to get these messages in the run log:
2004-05-28 23:30:21.753 Tag[2456] *** Assertion failure in -[NSMutableRLEArray objectAtIndex:effectiveRange:], String.subproj/NSAttributedString.m:1009
2004-05-28 23:30:21.756 Tag[2456] *** Assertion failure in -[NSMutableRLEArray objectAtIndex:effectiveRange:], String.subproj/NSAttributedString.m:1009
2004-05-28 23:30:21.857 Tag[2456] Access invalid attribute location 18 (length 18)
2004-05-28 23:30:22.198 Tag[2456] undo: NSUndoManager 0x4aa3b60 is in invalid state, undo was called with too many nested undo groups
If I then try and click in the text view, I get:
2004-05-28 23:30:24.287 Tag[2456] *** NSRunStorage, _NSBlockNumberForIndex(): index (18) beyond array bounds (18)
And can no longer type in the text view. I have tried adding an undo method with undo manager, but I still get the same errors.
Any ideas at all appreciated. This is the last bug remaining in my application and I'm really wanting to release this on Sunday, so any help at all is appreciated.
Thanks,
Oliver