Improved Model
Added Keyboard Selection Support Added Mouse Selection Support Added Keyboard Editing Support Corrected Some Bugs Abstracted the Layout System further Added Functions Button (test)
This commit is contained in:
@@ -20,13 +20,10 @@
|
||||
@interface MPExpression (MPExpressionPrivate)
|
||||
|
||||
- (NSUInteger)lengthOfElements:(NSArray *)elements;
|
||||
- (NSUInteger)indexOfElementAtLocation:(NSUInteger)location;
|
||||
|
||||
- (void)validateElements:(NSArray *)elements;
|
||||
- (BOOL)splitElementsAtLocation:(NSUInteger)location
|
||||
insertionIndex:(out NSUInteger *)insertionIndex;
|
||||
- (NSUInteger)calculateSplitOffsetForSplitLocation:(NSUInteger)location
|
||||
inElementAtIndex:(out NSUInteger *)elementIndex;
|
||||
|
||||
@end
|
||||
|
||||
@@ -41,13 +38,6 @@
|
||||
return length;
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfElementAtLocation:(NSUInteger)location
|
||||
{
|
||||
NSUInteger index = 0;
|
||||
[self calculateSplitOffsetForSplitLocation:location inElementAtIndex:&index];
|
||||
return index;
|
||||
}
|
||||
|
||||
- (void)validateElements:(NSArray *)elements
|
||||
{
|
||||
for (id element in elements) {
|
||||
@@ -66,18 +56,14 @@
|
||||
*insertionIndex = 0;
|
||||
return NO;
|
||||
}
|
||||
NSUInteger splitElementIndex;
|
||||
NSUInteger splitOffset = [self calculateSplitOffsetForSplitLocation:location
|
||||
inElementAtIndex:&splitElementIndex];
|
||||
id<MPExpressionElement> splitElement = self.elements[splitElementIndex];
|
||||
if (splitOffset == splitElement.length) {
|
||||
splitOffset = 0;
|
||||
splitElementIndex++;
|
||||
}
|
||||
|
||||
NSUInteger splitOffset;
|
||||
NSUInteger splitElementIndex = [self indexOfElementAtSymbolLocation:location
|
||||
offset:&splitOffset];
|
||||
if (splitOffset != 0) {
|
||||
NSString *stringElement = (NSString *)splitElement;
|
||||
NSString *leftPart = [stringElement substringToIndex:splitOffset];
|
||||
NSString *rightPart = [stringElement substringFromIndex:splitOffset];
|
||||
NSString *splitElement = (NSString *)self.elements[splitElementIndex];
|
||||
NSString *leftPart = [splitElement substringToIndex:splitOffset];
|
||||
NSString *rightPart = [splitElement substringFromIndex:splitOffset];
|
||||
[self.elements replaceObjectsInRange:NSMakeRange(splitElementIndex, 1)
|
||||
withObjectsFromArray:@[leftPart, rightPart]];
|
||||
++splitElementIndex;
|
||||
@@ -86,24 +72,6 @@
|
||||
return splitOffset != 0;
|
||||
}
|
||||
|
||||
- (NSUInteger)calculateSplitOffsetForSplitLocation:(NSUInteger)location
|
||||
inElementAtIndex:(out NSUInteger *)elementIndex
|
||||
{
|
||||
NSUInteger length = 0;
|
||||
NSUInteger index = 0;
|
||||
NSUInteger elementLength = 0;
|
||||
for (id<MPExpressionElement> element in self.elements) {
|
||||
elementLength = element.length;
|
||||
length += elementLength;
|
||||
if (length >= location) {
|
||||
break;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
*elementIndex = index;
|
||||
return elementLength - (length - location);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation MPExpression {
|
||||
@@ -215,6 +183,41 @@
|
||||
return [self.elements indexOfObject:element];
|
||||
}
|
||||
|
||||
- (NSUInteger)indexOfElementAtSymbolLocation:(NSUInteger)location
|
||||
offset:(out NSUInteger *)offset
|
||||
{
|
||||
if (location == 0) {
|
||||
if (offset != NULL) {
|
||||
*offset = 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Calculating elementIndex and splitOffset
|
||||
NSUInteger totalLength = 0;
|
||||
NSUInteger elementIndex = 0;
|
||||
NSUInteger elementLength = 0;
|
||||
for (id<MPExpressionElement> element in self.elements) {
|
||||
elementLength = element.length;
|
||||
totalLength += elementLength;
|
||||
if (totalLength >= location) {
|
||||
break;
|
||||
}
|
||||
++elementIndex;
|
||||
}
|
||||
NSUInteger splitOffset = elementLength - (totalLength - location);
|
||||
|
||||
id<MPExpressionElement> element = self.elements[elementIndex];
|
||||
if (splitOffset == element.length) {
|
||||
splitOffset = 0;
|
||||
elementIndex++;
|
||||
}
|
||||
if (offset != NULL) {
|
||||
*offset = splitOffset;
|
||||
}
|
||||
return elementIndex;
|
||||
}
|
||||
|
||||
- (void)replaceSymbolsInRange:(NSRange)range
|
||||
withElements:(NSArray *)elements
|
||||
{
|
||||
@@ -276,7 +279,7 @@
|
||||
}
|
||||
|
||||
#pragma mark Basic NSObject Methods
|
||||
|
||||
/*
|
||||
- (BOOL)isEqual:(id)object
|
||||
{
|
||||
if (self == object) {
|
||||
@@ -295,7 +298,7 @@
|
||||
{
|
||||
return [self.elements isEqualToArray:anExpression.elements];
|
||||
}
|
||||
|
||||
*/
|
||||
- (NSString *)description
|
||||
{
|
||||
#warning Bad Implementation
|
||||
@@ -359,6 +362,14 @@
|
||||
@implementation MPExpression (MPExpressionExtension)
|
||||
|
||||
#pragma mark Working With the Expression Tree
|
||||
- (MPExpression *)rootExpression
|
||||
{
|
||||
if (self.parent == nil) {
|
||||
return self;
|
||||
}
|
||||
return [self.parent rootExpression];
|
||||
}
|
||||
|
||||
- (id)elementAtIndexPath:(NSIndexPath *)indexPath
|
||||
{
|
||||
if (indexPath.length == 0) {
|
||||
@@ -369,7 +380,7 @@
|
||||
return element;
|
||||
}
|
||||
if ([element isFunction]) {
|
||||
return [(MPFunction *)element elementAtIndexPath:[indexPath indexPathByRemovingLastIndex]];
|
||||
return [(MPFunction *)element elementAtIndexPath:[indexPath indexPathByRemovingFirstIndex]];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user