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