Skip to content

Commit

Permalink
Add support for Swift class variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Sephiroth87 committed Mar 31, 2016
1 parent 75609d1 commit 28b384b
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 25 deletions.
47 changes: 29 additions & 18 deletions Crayons/Classes/IDEWorkspace+Crayons.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions Crayons/XcodeHeaders/DVTFoundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
+ (id)classMethodSymbolKind;
+ (id)classSymbolKind;

@property(readonly, copy) NSString *identifier;

@end

@interface DVTUserNotificationCenter : NSObject <NSUserNotificationCenterDelegate>
Expand Down
13 changes: 9 additions & 4 deletions Crayons/XcodeHeaders/IDEFoundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9532" systemVersion="15D21" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand Down
4 changes: 4 additions & 0 deletions Example/CrayonsExample/CrayonsExample/SwiftPalette.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ class SwiftPalette: NSObject {
return UIColor.purpleColor()
}

class var varColor: UIColor {
return UIColor.orangeColor()
}

}
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:`)
Expand All @@ -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
Expand Down

1 comment on commit 28b384b

@Sephiroth87
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#9

Please sign in to comment.