Skip to content

Commit

Permalink
-Correctly handle object types with both a class name and a protocol …
Browse files Browse the repository at this point in the history
…(ie. NSObject<NSCopying> *) referenced in methods and ivars

-Add imports for protocols referenced in types
  • Loading branch information
aricha committed Jan 6, 2014
1 parent 7cd199d commit 397cb45
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Source/CDMultiFileVisitor.m
Expand Up @@ -192,6 +192,11 @@ - (void)typeController:(CDTypeController *)typeController didReferenceClassName:
[self addReferenceToClassName:name];
}

- (void)typeController:(CDTypeController *)typeController didReferenceProtocolNames:(NSArray *)names
{
[self addReferencesToProtocolNamesInArray:names];
}

#pragma mark -

- (NSString *)frameworkForClassName:(NSString *)name;
Expand Down
1 change: 1 addition & 0 deletions Source/CDType.h
Expand Up @@ -10,6 +10,7 @@
- (id)init;
- (id)initSimpleType:(int)type;
- (id)initIDType:(CDTypeName *)name;
- (id)initIDType:(CDTypeName *)name withProtocols:(NSArray *)protocols;
- (id)initIDTypeWithProtocols:(NSArray *)protocols;
- (id)initStructType:(CDTypeName *)name members:(NSArray *)members;
- (id)initUnionType:(CDTypeName *)name members:(NSArray *)members;
Expand Down
26 changes: 21 additions & 5 deletions Source/CDType.m
Expand Up @@ -108,6 +108,11 @@ - (id)initSimpleType:(int)type;
}

- (id)initIDType:(CDTypeName *)name;
{
return [self initIDType:name withProtocols:nil];
}

- (id)initIDType:(CDTypeName *)name withProtocols:(NSArray *)protocols
{
if ((self = [self init])) {
if (name != nil) {
Expand All @@ -116,8 +121,9 @@ - (id)initIDType:(CDTypeName *)name;
} else {
_primitiveType = '@';
}
_protocols = protocols;
}

return self;
}

Expand Down Expand Up @@ -315,17 +321,27 @@ - (NSString *)formattedString:(NSString *)previousName formatter:(CDTypeFormatte
currentName = self.variableName;
else
currentName = previousName;

if ([_protocols count])
[typeFormatter formattingDidReferenceProtocolNames:_protocols];

switch (self.primitiveType) {
case T_NAMED_OBJECT:
case T_NAMED_OBJECT: {
assert(self.typeName != nil);
[typeFormatter formattingDidReferenceClassName:self.typeName.name];

NSString *typeName = nil;
if (_protocols == nil)
typeName = [NSString stringWithFormat:@"%@", self.typeName];
else
typeName = [NSString stringWithFormat:@"%@ <%@>", self.typeName, [_protocols componentsJoinedByString:@", "]];

if (currentName == nil)
result = [NSString stringWithFormat:@"%@ *", self.typeName];
result = [NSString stringWithFormat:@"%@ *", typeName];
else
result = [NSString stringWithFormat:@"%@ *%@", self.typeName, currentName];
result = [NSString stringWithFormat:@"%@ *%@", typeName, currentName];
break;
}
case '@':
if (currentName == nil) {
if (_protocols == nil)
Expand Down
2 changes: 2 additions & 0 deletions Source/CDTypeController.h
Expand Up @@ -28,6 +28,7 @@
- (CDType *)typeFormatter:(CDTypeFormatter *)typeFormatter replacementForType:(CDType *)type;
- (NSString *)typeFormatter:(CDTypeFormatter *)typeFormatter typedefNameForStructure:(CDType *)structureType level:(NSUInteger)level;
- (void)typeFormatter:(CDTypeFormatter *)typeFormatter didReferenceClassName:(NSString *)name;
- (void)typeFormatter:(CDTypeFormatter *)typeFormatter didReferenceProtocolNames:(NSArray *)names;

- (void)appendStructuresToString:(NSMutableString *)resultString;

Expand Down Expand Up @@ -58,4 +59,5 @@
@protocol CDTypeControllerDelegate <NSObject>
@optional
- (void)typeController:(CDTypeController *)typeController didReferenceClassName:(NSString *)name;
- (void)typeController:(CDTypeController *)typeController didReferenceProtocolNames:(NSArray *)names;
@end
6 changes: 6 additions & 0 deletions Source/CDTypeController.m
Expand Up @@ -133,6 +133,12 @@ - (void)typeFormatter:(CDTypeFormatter *)typeFormatter didReferenceClassName:(NS
[self.delegate typeController:self didReferenceClassName:name];
}

- (void)typeFormatter:(CDTypeFormatter *)typeFormatter didReferenceProtocolNames:(NSArray *)names
{
if ([self.delegate respondsToSelector:@selector(typeController:didReferenceProtocolNames:)])
[self.delegate typeController:self didReferenceProtocolNames:names];
}

#pragma mark -

- (void)appendStructuresToString:(NSMutableString *)resultString;
Expand Down
1 change: 1 addition & 0 deletions Source/CDTypeFormatter.h
Expand Up @@ -20,5 +20,6 @@
- (NSString *)typedefNameForStructure:(CDType *)structureType level:(NSUInteger)level;

- (void)formattingDidReferenceClassName:(NSString *)name;
- (void)formattingDidReferenceProtocolNames:(NSArray *)names;

@end
5 changes: 5 additions & 0 deletions Source/CDTypeFormatter.m
Expand Up @@ -272,4 +272,9 @@ - (void)formattingDidReferenceClassName:(NSString *)name;
[self.typeController typeFormatter:self didReferenceClassName:name];
}

- (void)formattingDidReferenceProtocolNames:(NSArray *)names
{
[self.typeController typeFormatter:self didReferenceProtocolNames:names];
}

@end
18 changes: 15 additions & 3 deletions Source/CDTypeParser.m
Expand Up @@ -245,9 +245,21 @@ - (CDType *)_parseTypeInStruct:(BOOL)isInStruct;
#endif
if (_lookahead == TK_QUOTED_STRING && (isInStruct == NO || [self.lexer.lexText isFirstLetterUppercase] || [self isTokenInTypeStartSet:self.lexer.peekChar] == NO)) {
NSString *str = self.lexer.lexText;
if ([str hasPrefix:@"<"] && [str hasSuffix:@">"]) {
str = [str substringWithRange:NSMakeRange(1, [str length] - 2)];
result = [[CDType alloc] initIDTypeWithProtocols:[str componentsSeparatedByString:@","]];

NSUInteger protocolOpenIdx = NSMaxRange([str rangeOfString:@"<"]);
NSUInteger protocolCloseIdx = [str rangeOfString:@">" options:NSBackwardsSearch].location;
if (protocolOpenIdx != NSNotFound && protocolCloseIdx != NSNotFound) {
NSRange protocolRange = NSMakeRange(protocolOpenIdx, protocolCloseIdx - protocolOpenIdx);
NSArray *protocols = [[str substringWithRange:protocolRange] componentsSeparatedByString:@","];

NSString *typeNameStr = [[str substringToIndex:(protocolOpenIdx - 1)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
CDTypeName *typeName = nil;
if (![typeNameStr isEqualToString:@"id"]) {
typeName = [[CDTypeName alloc] init];
typeName.name = typeNameStr;
}

result = [[CDType alloc] initIDType:typeName withProtocols:protocols];
} else {
CDTypeName *typeName = [[CDTypeName alloc] init];
typeName.name = str;
Expand Down

0 comments on commit 397cb45

Please sign in to comment.