From 28b384b11bdddcb1b6ac4a406331236a5a02ac28 Mon Sep 17 00:00:00 2001 From: Fabio Ritrovato Date: Thu, 31 Mar 2016 12:59:09 +0100 Subject: [PATCH] Add support for Swift class variables --- Crayons/Classes/IDEWorkspace+Crayons.m | 47 ++++++++++++------- Crayons/XcodeHeaders/DVTFoundation.h | 2 + Crayons/XcodeHeaders/IDEFoundation.h | 13 +++-- .../CrayonsExample/Base.lproj/Main.storyboard | 5 +- .../CrayonsExample/SwiftPalette.swift | 4 ++ README.md | 7 ++- 6 files changed, 53 insertions(+), 25 deletions(-) diff --git a/Crayons/Classes/IDEWorkspace+Crayons.m b/Crayons/Classes/IDEWorkspace+Crayons.m index 6de4b19..71d327a 100644 --- a/Crayons/Classes/IDEWorkspace+Crayons.m +++ b/Crayons/Classes/IDEWorkspace+Crayons.m @@ -60,9 +60,9 @@ - (void)updatePalettes CrayonsPalette *palette = palettesForResolutions[symbol.resolution] ?: [CrayonsPalette paletteWithSymbol:symbol]; [currentResolutions removeObject:symbol.resolution]; if ([symbol isKindOfClass:[IDEIndexClassSymbol class]]) { - [self updateColors:palette methods:((IDEIndexClassSymbol *)symbol).classMethods]; + [self updateColors:palette symbols:((IDEIndexClassSymbol *)symbol).children]; } else if ([symbol isKindOfClass:[IDEIndexCategorySymbol class]]) { - [self updateColors:palette methods:((IDEIndexCategorySymbol *)symbol).classMethods]; + [self updateColors:palette symbols:((IDEIndexCategorySymbol *)symbol).children]; } palettesForResolutions[symbol.resolution] = palette; for (IDEIndexSymbolOccurrence *occurrence in [symbol occurrences]) { @@ -111,26 +111,37 @@ - (void)invalidatePalettesForClassNames:(NSArray *)names } } -- (void)updateColors:(CrayonsPalette *)palette methods:(id)methods +- (void)updateColors:(CrayonsPalette *)palette symbols:(id)symbols { NSMutableSet *currentColors = [[palette.colors allKeys] mutableCopy]; - for (IDEIndexCallableSymbol *method in methods) { - NSString *methodName = method.name; - if (([[[method returnType] name] isEqualToString:@"UIColor"] || [[method resolution] hasSuffix:@"UIColor"]) && [method numArguments] == 0) { - if ([methodName hasSuffix:@"()"]) { - methodName = [methodName substringToIndex:methodName.length - 2]; + for (IDEIndexSymbol *symbol in symbols) { + if (symbol.symbolKind == [DVTSourceCodeSymbolKind classMethodSymbolKind]) { + IDEIndexCallableSymbol *method = (IDEIndexCallableSymbol *)symbol; + NSString *methodName = method.name; + if (([[[method returnType] name] isEqualToString:@"UIColor"] || [[method resolution] hasSuffix:@"UIColor"]) && [method numArguments] == 0) { + if ([methodName hasSuffix:@"()"]) { + methodName = [methodName substringToIndex:methodName.length - 2]; + } + if ([currentColors containsObject:methodName]) { + [currentColors removeObject:methodName]; + } else { + palette.colors[methodName] = [NSNull null]; + } + } else if ([[method resolution] hasSuffix:@"ERR"]) { + // Sometimes the index gets corrupted, so we recreate it from scratch + //TODO: is there a better way to detect this? It looks like the index stops updating on corruption, so what happens + // if it's other methods that are corrupted instead of the ones we use? + [self.index _reopenDatabaseWithRemoval:YES]; + DLog(@"🖍 Index corruption detected with %@, rebuilding", [method resolution]); } - if ([currentColors containsObject:methodName]) { - [currentColors removeObject:methodName]; - } else { - palette.colors[methodName] = [NSNull null]; + } else if ([symbol.symbolKind.identifier isEqualToString:@"Xcode.SourceCodeSymbolKind.ClassProperty"]) { + if ([[symbol resolution] hasSuffix:@"UIColor"]) { + if ([currentColors containsObject:symbol.name]) { + [currentColors removeObject:symbol.name]; + } else { + palette.colors[symbol.name] = [NSNull null]; + } } - } else if ([[method resolution] hasSuffix:@"ERR"]) { - // Sometimes the index gets corrupted, so we recreate it from scratch - //TODO: is there a better way to detect this? It looks like the index stops updating on corruption, so what happens - // if it's other methods that are corrupted instead of the ones we use? - [self.index _reopenDatabaseWithRemoval:YES]; - DLog(@"🖍 Index corruption detected with %@, rebuilding", [method resolution]); } } for (NSString *oldColor in currentColors) { diff --git a/Crayons/XcodeHeaders/DVTFoundation.h b/Crayons/XcodeHeaders/DVTFoundation.h index 2a9e6e9..68c90b3 100644 --- a/Crayons/XcodeHeaders/DVTFoundation.h +++ b/Crayons/XcodeHeaders/DVTFoundation.h @@ -11,6 +11,8 @@ + (id)classMethodSymbolKind; + (id)classSymbolKind; +@property(readonly, copy) NSString *identifier; + @end @interface DVTUserNotificationCenter : NSObject diff --git a/Crayons/XcodeHeaders/IDEFoundation.h b/Crayons/XcodeHeaders/IDEFoundation.h index f16e2f5..1fcdbd9 100644 --- a/Crayons/XcodeHeaders/IDEFoundation.h +++ b/Crayons/XcodeHeaders/IDEFoundation.h @@ -35,6 +35,7 @@ @interface IDEIndexSymbol : IDEIndexSymbolOccurrence @property(readonly, nonatomic) NSString *name; +@property(readonly, nonatomic) DVTSourceCodeSymbolKind *symbolKind; @property(readonly, nonatomic) NSString *resolution; @property(readonly, nonatomic) IDEIndexCollection *definitions; @property(readonly, nonatomic, getter=isInProject) BOOL inProject; @@ -52,16 +53,20 @@ @end -@interface IDEIndexClassSymbol : IDEIndexSymbol +@interface IDEIndexContainerSymbol : IDEIndexSymbol + +- (id)children; + +@end + +@interface IDEIndexClassSymbol : IDEIndexContainerSymbol -- (id)classMethods; - (id)categories; @end -@interface IDEIndexCategorySymbol : IDEIndexSymbol +@interface IDEIndexCategorySymbol : IDEIndexContainerSymbol - (id)relatedClass; -- (id)classMethods; @end diff --git a/Example/CrayonsExample/CrayonsExample/Base.lproj/Main.storyboard b/Example/CrayonsExample/CrayonsExample/Base.lproj/Main.storyboard index dc065bd..6aa422f 100644 --- a/Example/CrayonsExample/CrayonsExample/Base.lproj/Main.storyboard +++ b/Example/CrayonsExample/CrayonsExample/Base.lproj/Main.storyboard @@ -1,7 +1,8 @@ - + - + + diff --git a/Example/CrayonsExample/CrayonsExample/SwiftPalette.swift b/Example/CrayonsExample/CrayonsExample/SwiftPalette.swift index 9ea5a56..65ad90f 100644 --- a/Example/CrayonsExample/CrayonsExample/SwiftPalette.swift +++ b/Example/CrayonsExample/CrayonsExample/SwiftPalette.swift @@ -18,4 +18,8 @@ class SwiftPalette: NSObject { return UIColor.purpleColor() } + class var varColor: UIColor { + return UIColor.orangeColor() + } + } \ No newline at end of file diff --git a/README.md b/README.md index 4e1b305..200e3b9 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,12 @@ Define a palette by adding the class method ``` to any class (Class has to derive from NSObject at some level, so no pure Swift classes at the moment). -To define a color, add a class method with any name that returns UIColor, for example +To define a color, add a class method or variable with any name that returns UIColor, for example ``` + (UIColor *)colorName + class func colorName() -> UIColor + class var colorName: UIColor ``` Only methods with no parameters are supported at the moment, and only UIColors in the RGB and White color space (so no `colorWithPatternImage:`) @@ -53,6 +55,9 @@ Look at the example project for more infos. Either way, restart Xcode to make it load ##Release notes +###1.3 +- Add support for Swift class variables + ###1.2 - Xcode 7.3 support - Improved internal logic + bugfixes