Archived
1

Improved Code Style

This commit is contained in:
Kim Wittenburg
2014-09-16 20:08:25 +02:00
parent af8f26dee9
commit 9a8893796c
9 changed files with 36 additions and 58 deletions

View File

@@ -9,10 +9,12 @@
@import Cocoa;
#import "MPLayout.h"
#import "MPExpression.h"
#import "MPFunctionLayout.h"
@interface MPExpressionLayout : MPLayout
- (instancetype)initRootLayoutWithExpression:(MPExpression *)expression;
- (instancetype)initWithExpression:(MPExpression *)expression
parent:(MPFunctionLayout *)parent;
@property (readonly, nonatomic, weak) MPExpression *expression;

View File

@@ -41,35 +41,22 @@
@implementation MPExpressionLayout
# pragma mark Creation Methods
- (instancetype)initRootLayoutWithExpression:(MPExpression *)expression
- (instancetype)initWithExpression:(MPExpression *)expression
parent:(MPFunctionLayout *)parent
{
self = [super init];
self = [super initWithParent:parent];
if (self) {
_expression = expression;
}
return self;
}
- (instancetype)initWithElementAtPath:(NSIndexPath *)path
inRootExpression:(MPExpression *)rootExpression
parent:(MPLayout *)parent
{
self = [super initWithElementAtPath:path
inRootExpression:rootExpression
parent:parent];
if (self) {
_expression = [rootExpression elementAtIndexPath:path];
}
return self;
}
#pragma mark Cache Methods
- (MPLayout *)childLayoutAtIndex:(NSUInteger)index
{
id cachedObject = [self cachableObjectForIndex:index generator:^id{
NSIndexPath *indexPath = [self.expression.indexPath indexPathByAddingIndex:index];
MPFunctionLayout *layout = [MPFunctionLayout functionLayoutForFunctionAtIndexPath:indexPath
inRootExpression:self.expression.rootExpression
MPFunction *function = (MPFunction *)[self.expression elementAtIndex:index];
MPFunctionLayout *layout = [MPFunctionLayout functionLayoutForFunction:function
parent:self];
layout.flipped = self.flipped;
layout.usesSmallSize = self.usesSmallSize;
@@ -114,7 +101,7 @@
- (NSRect)boundingRectForRange:(NSRange)range
{
NSUInteger startOffset;
NSUInteger startElementIndex = [self.expression indexOfElementAtSymbolLocation:range.location
NSUInteger startElementIndex = [self.expression indexOfElementAtLocation:range.location
offset:&startOffset];
// Calculate x position
CGFloat x = 0, width = 0;
@@ -139,7 +126,7 @@
}
NSUInteger endOffset;
NSUInteger endElementIndex = [self.expression indexOfElementAtSymbolLocation:NSMaxRange(range)
NSUInteger endElementIndex = [self.expression indexOfElementAtLocation:NSMaxRange(range)
offset:&endOffset];
// Selection is inside of one string element

View File

@@ -24,7 +24,7 @@
{
self = [super initWithElements:elements];
if (self) {
_rootLayout = [[MPExpressionLayout alloc] initRootLayoutWithExpression:self];
_rootLayout = [[MPExpressionLayout alloc] initWithExpression:self parent:nil];
}
return self;
}
@@ -32,11 +32,11 @@
- (void)setExpressionView:(MPExpressionView *)expressionView
{
_expressionView = expressionView;
self.rootLayout = [[MPExpressionLayout alloc] initRootLayoutWithExpression:self];
self.rootLayout = [[MPExpressionLayout alloc] initWithExpression:self parent:nil];
self.rootLayout.flipped = expressionView.isFlipped;
}
- (void)didChangeElementsInRangePath:(MPRangePath *)rangePath
- (void)didChangeElementsInIndexedRangePath:(MPRangePath *)rangePath
replacementLength:(NSUInteger)replacementLength
{
if (rangePath.location.length == 0) {

View File

@@ -22,7 +22,7 @@
@property (readonly, nonatomic, strong) MPExpressionStorage *expressionStorage;
@property (nonatomic, getter = isEditable) BOOL editable;
// @property (nonatomic, getter = isEditable) BOOL editable;
@property (nonatomic, strong) MPRangePath *selection;
@property (nonatomic, weak) id target;

View File

@@ -112,7 +112,7 @@
MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:targetExpressionPath];
NSUInteger locationInTarget = selectionPath.lastIndex;
NSUInteger locationInElement;
NSUInteger targetElementIndex = [targetExpression indexOfElementAtSymbolLocation:locationInTarget
NSUInteger targetElementIndex = [targetExpression indexOfElementAtLocation:locationInTarget
offset:&locationInElement];
id<MPExpressionElement> targetElement;
@@ -172,7 +172,7 @@
MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:targetExpressionPath];
NSUInteger locationInTarget = selectionPath.lastIndex;
NSUInteger locationInElement;
NSUInteger targetElementIndex = [targetExpression indexOfElementAtSymbolLocation:locationInTarget
NSUInteger targetElementIndex = [targetExpression indexOfElementAtLocation:locationInTarget
offset:&locationInElement];
NSUInteger previousElementIndex = targetElementIndex - (locationInElement == 0 ? 1 : 0);

View File

@@ -11,8 +11,7 @@
@interface MPFunctionLayout : MPLayout
+ (MPFunctionLayout *)functionLayoutForFunctionAtIndexPath:(NSIndexPath *)path
inRootExpression:(MPExpression *)rootExpression
+ (MPFunctionLayout *)functionLayoutForFunction:(MPFunction *)function
parent:(MPExpressionLayout *)parent;
@property (readonly, nonatomic, weak) MPFunction *function;
@@ -21,6 +20,9 @@
@interface MPFunctionLayout (MPSubclassOverride)
- (instancetype)initWithFunction:(MPFunction *)function
parent:(MPExpressionLayout *)parent;
#pragma mark Cache Methods
- (CTLineRef)lineForPrivateCacheIndex:(NSUInteger)index
generator:(CTLineRef (^)())generator;

View File

@@ -16,31 +16,23 @@
@implementation MPFunctionLayout
#pragma mark Creation Methods
+ (MPFunctionLayout *)functionLayoutForFunctionAtIndexPath:(NSIndexPath *)path
inRootExpression:(MPExpression *)rootExpression
+ (MPFunctionLayout *)functionLayoutForFunction:(MPFunction *)function
parent:(MPExpressionLayout *)parent
{
MPFunction *function = [rootExpression elementAtIndexPath:path];
Class class = [function class];
if (class == [MPSumFunction class]) {
return [[MPSumFunctionLayout alloc] initWithElementAtPath:path
inRootExpression:rootExpression
parent:parent];
return [[MPSumFunctionLayout alloc] initWithFunction:function parent:parent];
}
return [[self alloc] initWithElementAtPath:path
inRootExpression:rootExpression
return [[self alloc] initWithFunction:function
parent:parent];
}
- (instancetype)initWithElementAtPath:(NSIndexPath *)path
inRootExpression:(MPExpression *)rootExpression
parent:(MPLayout *)parent
- (instancetype)initWithFunction:(MPFunction *)function
parent:(MPExpressionLayout *)parent
{
self = [super initWithElementAtPath:path
inRootExpression:rootExpression
parent:parent];
self = [super initWithParent:parent];
if (self) {
_function = [rootExpression elementAtIndexPath:path];
_function = function;
}
return self;
}
@@ -62,9 +54,8 @@
- (MPLayout *)childLayoutAtIndex:(NSUInteger)index
{
return [self cachableObjectForIndex:index generator:^id{
NSIndexPath *childPath = [self.function.indexPath indexPathByAddingIndex:index];
MPExpressionLayout *layout = [[MPExpressionLayout alloc] initWithElementAtPath:childPath
inRootExpression:self.function.rootExpression
MPExpression *child = [self.function childAtIndex:index];
MPExpressionLayout *layout = [[MPExpressionLayout alloc] initWithExpression:child
parent:self];
layout.flipped = self.flipped;
layout.usesSmallSize = (index == 0 || index == 1) ? YES : self.usesSmallSize;

View File

@@ -15,9 +15,7 @@
#pragma mark Creation Methods
- (instancetype)init;
- (instancetype)initWithElementAtPath:(NSIndexPath *)path
inRootExpression:(MPExpression *)rootExpression
parent:(MPLayout *)parent;
- (instancetype)initWithParent:(MPLayout *)parent;
#pragma mark Properties
- (NSFont *)font;

View File

@@ -35,9 +35,7 @@
return self;
}
- (instancetype)initWithElementAtPath:(NSIndexPath *)path
inRootExpression:(MPExpression *)rootExpression
parent:(MPLayout *)parent
- (instancetype)initWithParent:(MPLayout *)parent
{
self = [self init];
if (self) {