Skip navigation.
 
mlNSTextStorage subclassing questions
FROM : Jesse Grosjean
DATE : Tue Mar 25 01:05:14 2008

I have a basic NSTextStorage subclass that I've included at the end of 
this email. I have two questions. First what is the proper way to 
implement syntax highlighting in an NSTextStorage subclass. (I want to 
do it in the subclass, not in an NSTextStorage delegate). The basic 
pattern that I'm using is this:

- (void)processEditing {        
   if ([self editedMask] & NSTextStorageEditedCharacters) {
       [self beginEditing];
       // perform syntax highlighting
       [self endEditing];
   }
   [super processEditing];
}

Is that the correct way to do it? Until recently I was calling [super 
processEditing] at the beginning of the method, but that seemed to 
cause problems if I added NSParagraphStyle attributes during the 
syntax highlighting while  setAllowsNonContiguousLayout was turned on 
in the layout manager. That problem made me wonder if there was 
anything else subtle that I was missing? Is it neccessary to call 
[self beginEditing], [self endEditing] around the changes in this 
case? Any other tips for keeping things as fast and correct as possible?

Second in profiling my app most of the processingEditing time is spent 
in the fixAttributesInRange method? Generally I'm careful about only 
updating highlighting for regions of the text that need it, but often 
all the text in the view will get completely replaced with new text so 
there's not really a way to get around doing a full re-highlight 
sometimes, and that's when fixAttributesInRange starts to get a little 
noticeable.

Because of that I've been looking at NSTextStorages - 
(BOOL)fixesAttributesLazily method, but I can't really figure out how 
it works and how to support it. Is there any example code (or hints) 
on how to make a NSTextStorage subclass fix attributes lazily. I've 
tried turning it one, and adding [self 
ensureAttributesAreFixedInRange:NSMakeRange(index, 0)] to the 
attributesAtIndex:effectiveRange: method, but my app soon crashes 
after doing that.

Thanks,
Jesse

@implementation TPParagraphAliasStorage

#pragma mark Init

- (id)init {
   if (self = [super init]) {
       backingStore = [[NSMutableAttributedString alloc] init];
   }
   return self;
}

- (void)processEditing {        
   if ([self editedMask] & NSTextStorageEditedCharacters) {
       [self beginEditing];
       // perform syntax highlighting
       [self endEditing];
   }
   [super processEditing];
}

- (NSString *)string {
   return [backingStore string];
}

- (NSDictionary *)attributesAtIndex:(NSUInteger)index effectiveRange:
(NSRangePointer)aRange {
   return [backingStore attributesAtIndex:index effectiveRange:aRange];
}

- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)aRange {
   [backingStore setAttributes:attributes range:aRange];
   [self edited:NSTextStorageEditedAttributes range:aRange 
changeInLength:0];    
}

- (void)addAttribute:(NSString *)name value:(id)value range:
(NSRange)aRange {
   [backingStore addAttribute:name value:value range:aRange]; // cover 
bug listed in cocoadev
   [self edited:NSTextStorageEditedAttributes range:aRange 
changeInLength:0];
}

- (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString 
*)aString {    
   NSUInteger length = [aString length];
   NSInteger changeInLength = length - aRange.length;
   [backingStore replaceCharactersInRange:aRange withString:aString];
   [self edited:NSTextStorageEditedCharacters range:aRange 
changeInLength:changeInLength];
}

@end

Related mailsAuthorDate
No related mails found.