Fixed broken editing algorithm
Added some helpers
This commit is contained in:
@@ -603,6 +603,9 @@
|
||||
- (void)replaceElementsInIndexedRange:(NSRange)range
|
||||
withElements:(NSArray *)elements;
|
||||
|
||||
- (void)replaceSymbolsInRangePath:(MPRangePath *)rangePath
|
||||
withElements:(NSArray *)elements;
|
||||
|
||||
|
||||
/*!
|
||||
@method appendElement:
|
||||
|
||||
@@ -91,7 +91,9 @@
|
||||
|
||||
@implementation MPExpression {
|
||||
NSUInteger _cachedLength;
|
||||
|
||||
NSRange _editedRange;
|
||||
BOOL _didSplitStartOnEditing;
|
||||
BOOL _didSplitEndOnEditing;
|
||||
NSUInteger _replacementLength;
|
||||
}
|
||||
@@ -144,15 +146,17 @@
|
||||
[_elements replaceObjectsInRange:NSMakeRange(index, 2)
|
||||
withObjectsFromArray:@[new]];
|
||||
NSUInteger maxReplacementIndex = _editedRange.location + _replacementLength;
|
||||
if (index == _editedRange.location - 1) {
|
||||
if (index == _editedRange.location-1 && !_didSplitStartOnEditing) {
|
||||
--_editedRange.location;
|
||||
++_editedRange.length;
|
||||
if (index == maxReplacementIndex-1 && !_didSplitEndOnEditing) {
|
||||
++_editedRange.length;
|
||||
++_replacementLength;
|
||||
}
|
||||
} else if (index >= _editedRange.location && index < maxReplacementIndex - 1) {
|
||||
--_replacementLength;
|
||||
} else if (index == maxReplacementIndex - 1) {
|
||||
if (!_didSplitEndOnEditing) {
|
||||
++_editedRange.length;
|
||||
}
|
||||
} else if (index == maxReplacementIndex-1 && !_didSplitEndOnEditing) {
|
||||
++_editedRange.length;
|
||||
}
|
||||
--index;
|
||||
}
|
||||
@@ -222,12 +226,28 @@
|
||||
_cachedLength = 0;
|
||||
|
||||
NSUInteger editLocation = startIndex - (didSplitStart ? 1 : 0);
|
||||
NSUInteger editLength = range.length > 0 ? (endIndex - startIndex) : 0;
|
||||
NSUInteger editLength = endIndex - startIndex;
|
||||
if (range.length == 0 && didSplitStart) {
|
||||
editLength = 1;
|
||||
}
|
||||
|
||||
_editedRange = NSMakeRange(editLocation, editLength);
|
||||
_didSplitStartOnEditing = didSplitStart;
|
||||
_didSplitEndOnEditing = didSplitEnd;
|
||||
_replacementLength = elements.count + (didSplitStart ? 1 : 0) + (didSplitEnd ? 1 : 0);
|
||||
_replacementLength = elements.count;
|
||||
if (didSplitStart) {
|
||||
_replacementLength++;
|
||||
if (range.length == 0) {
|
||||
_replacementLength++;
|
||||
}
|
||||
}
|
||||
if (didSplitEnd) {
|
||||
_replacementLength++;
|
||||
}
|
||||
|
||||
[self fixElements];
|
||||
[self.evaluator expressionDidChangeInRange:_editedRange
|
||||
replacementLength:_replacementLength];
|
||||
[self didChangeElementsInIndexedRangePath:[[MPRangePath alloc] initWithRange:_editedRange]
|
||||
replacementLength:_replacementLength];
|
||||
}
|
||||
@@ -546,6 +566,14 @@
|
||||
withElements:elements];
|
||||
}
|
||||
|
||||
- (void)replaceSymbolsInRangePath:(MPRangePath *)rangePath
|
||||
withElements:(NSArray *)elements
|
||||
{
|
||||
MPExpression *targetExpression = [self elementAtIndexPath:[rangePath.location indexPathByRemovingLastIndex]];
|
||||
[targetExpression replaceSymbolsInRange:rangePath.rangeAtLastIndex
|
||||
withElements:elements];
|
||||
}
|
||||
|
||||
- (void)appendElement:(id<MPExpressionElement>)anElement
|
||||
{
|
||||
[self appendElements:@[anElement]];
|
||||
|
||||
Reference in New Issue
Block a user