Author: ericwa
Date: Thu Mar 20 07:17:09 2014
New Revision: 10597
URL: http://svn.gna.org/viewcvs/etoile?rev=10597&view=rev
Log:
COAttributedStringWrapper: merge adjacent chunks when possible
Modified:
trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedStringWrapper.m
trunk/Etoile/Frameworks/CoreObject/Tests/Extras/Model/TestAttributedStringWrapper.m
Modified:
trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedStringWrapper.m
URL:
http://svn.gna.org/viewcvs/etoile/trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedStringWrapper.m?rev=10597&r1=10596&r2=10597&view=diff
==============================================================================
--- trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedStringWrapper.m
(original)
+++ trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedStringWrapper.m
Thu Mar 20 07:17:09 2014
@@ -423,6 +423,7 @@
NSUInteger chunkIndex = 0, chunkStart = 0;
COAttributedStringChunk *chunk = [_backing chunkContainingIndex:
aRange.location chunkStart: &chunkStart chunkIndex: &chunkIndex];
+ const NSUInteger firstChunkIndex = chunkIndex;
/* Sepecial case: empty string */
if ([self length] == 0)
@@ -474,6 +475,8 @@
[[_backing mutableArrayValueForKey: @"chunks"]
removeObjectAtIndex: chunkIndex--];
}
}
+
+ [self mergeChunksInChunkRange: NSMakeRange(firstChunkIndex, chunkIndex
+ 1 - firstChunkIndex)];
// TODO: Add tests that check for this
const NSInteger delta = [aString length] - aRange.length;
@@ -538,6 +541,34 @@
aChunk.attributes = [self ourAttributesForAttributeDict: attrs];
}
+- (void)mergeChunksInChunkRange: (NSRange)range
+{
+ NSMutableArray *chunksProxy = [self.backing mutableArrayValueForKey:
@"chunks"];
+
+ for (NSUInteger i = range.location; i <= NSMaxRange(range); i++)
+ {
+ if (i >= [chunksProxy count])
+ break;
+
+ if (i == 0)
+ continue;
+
+ COAttributedStringChunk *chunkI = chunksProxy[i];
+ COAttributedStringChunk *chunkLeftOfI = chunksProxy[i - 1];
+
+ if ([COAttributedStringAttribute isAttributeSet:
chunkI.attributes
+
equalToSet: chunkLeftOfI.attributes])
+ {
+ // we can merge them!
+
+ chunkLeftOfI.text = [chunkLeftOfI.text
stringByAppendingString: chunkI.text];
+
+ [chunksProxy removeObjectAtIndex: i];
+ i--; // N.B.: Won't underflow because i > 0 (see if (i
== 0) continue; above)
+ }
+ }
+}
+
- (void)setAttributes: (NSDictionary *)aDict range: (NSRange)aRange
{
//NSLog(@"%p (%@) Set attributes %@ range %@", self, [self string],
aDict, NSStringFromRange(aRange));
@@ -583,6 +614,8 @@
[self setAttributes: aDict forChunk: chunk];
}
+ [self mergeChunksInChunkRange: NSMakeRange(splitChunk1, splitChunk2 -
splitChunk1)];
+
// TODO: Add tests that check for this
[self edited: NSTextStorageEditedAttributes range: aRange
changeInLength: 0];
Modified:
trunk/Etoile/Frameworks/CoreObject/Tests/Extras/Model/TestAttributedStringWrapper.m
URL:
http://svn.gna.org/viewcvs/etoile/trunk/Etoile/Frameworks/CoreObject/Tests/Extras/Model/TestAttributedStringWrapper.m?rev=10597&r1=10596&r2=10597&view=diff
==============================================================================
---
trunk/Etoile/Frameworks/CoreObject/Tests/Extras/Model/TestAttributedStringWrapper.m
(original)
+++
trunk/Etoile/Frameworks/CoreObject/Tests/Extras/Model/TestAttributedStringWrapper.m
Thu Mar 20 07:17:09 2014
@@ -458,6 +458,27 @@
UKObjectsEqual(@"d", [attributedString.chunks[1] text]);
}
+- (void) testRemovingAttributeMergesAdjacentChunks
+{
+ [self appendHTMLString: @"a<B>b</B>c" toAttributedString:
attributedString];
+ UKIntsEqual(3, [attributedString.chunks count]);
+
+ [self setFontTraits: 0 inRange: NSMakeRange(1,1) inTextStorage:as];
+ UKObjectsEqual(@"abc", [as string]);
+ UKIntsEqual(1, [attributedString.chunks count]);
+ UKObjectsEqual(@"abc", [attributedString.chunks[0] text]);
+}
+
+- (void) testRemovingSubstringMergesAdjacentChunks
+{
+ [self appendHTMLString: @"a<B>b</B>c" toAttributedString:
attributedString];
+
+ [as replaceCharactersInRange: NSMakeRange(1, 1) withString: @""];
+ UKObjectsEqual(@"ac", [as string]);
+ UKIntsEqual(1, [attributedString.chunks count]);
+ UKObjectsEqual(@"ac", [attributedString.chunks[0] text]);
+}
+
@end
/**
_______________________________________________
Etoile-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/etoile-cvs