Skip to content
This repository was archived by the owner on Feb 21, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add comment support for Swift Class
  • Loading branch information
tonyli508 committed Jun 17, 2016
commit 2ea2d801eb66b4148aaad7de612807d83ce7a878
8 changes: 8 additions & 0 deletions VVDocumenter-Xcode.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
1082F0001D13ECF800DDAC35 /* VVSwiftClassCommenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 1082EFFF1D13ECF800DDAC35 /* VVSwiftClassCommenter.m */; };
10E70BFA1D13FFD000C85E27 /* VVSwiftClassCommenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 1082EFFF1D13ECF800DDAC35 /* VVSwiftClassCommenter.m */; };
4B06DF741B315E9D007D69BE /* VVSwiftExtensionCommenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B50A26F1B315CF3008A4400 /* VVSwiftExtensionCommenter.m */; };
4B50A2701B315CF3008A4400 /* VVSwiftExtensionCommenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B50A26F1B315CF3008A4400 /* VVSwiftExtensionCommenter.m */; };
53C67935184501030030C553 /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 893D8F7F18262EF500E8A00C /* Carbon.framework */; };
Expand Down Expand Up @@ -73,6 +75,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
1082EFFE1D13ECF800DDAC35 /* VVSwiftClassCommenter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VVSwiftClassCommenter.h; sourceTree = "<group>"; };
1082EFFF1D13ECF800DDAC35 /* VVSwiftClassCommenter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VVSwiftClassCommenter.m; sourceTree = "<group>"; };
4B50A26E1B315CF3008A4400 /* VVSwiftExtensionCommenter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VVSwiftExtensionCommenter.h; sourceTree = "<group>"; };
4B50A26F1B315CF3008A4400 /* VVSwiftExtensionCommenter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VVSwiftExtensionCommenter.m; sourceTree = "<group>"; };
893D8F7F18262EF500E8A00C /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = System/Library/Frameworks/Carbon.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -309,6 +313,8 @@
D16AFCE81796E0D6006719AA /* VVVariableCommenter.m */,
4B50A26E1B315CF3008A4400 /* VVSwiftExtensionCommenter.h */,
4B50A26F1B315CF3008A4400 /* VVSwiftExtensionCommenter.m */,
1082EFFE1D13ECF800DDAC35 /* VVSwiftClassCommenter.h */,
1082EFFF1D13ECF800DDAC35 /* VVSwiftClassCommenter.m */,
);
path = Commenter;
sourceTree = "<group>";
Expand Down Expand Up @@ -517,6 +523,7 @@
D124614717AC16820095F9D6 /* VVDocumenterSetting.m in Sources */,
C4052CFB1B3BB10700ED0CF0 /* VVWorkspaceManager.m in Sources */,
D124614817AC16820095F9D6 /* VVDSettingPanelWindowController.m in Sources */,
1082F0001D13ECF800DDAC35 /* VVSwiftClassCommenter.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -537,6 +544,7 @@
D1A6B1D1198A0D8600BCED83 /* VVSwiftPropertyCommenter.m in Sources */,
D14110B51988C12F00A7083F /* NSTextView+VVTextGetter.m in Sources */,
D14110B61988C12F00A7083F /* VVDocumenterManager.m in Sources */,
10E70BFA1D13FFD000C85E27 /* VVSwiftClassCommenter.m in Sources */,
D14110B71988C12F00A7083F /* VVDocumenter.m in Sources */,
D14110A81988C12000A7083F /* VVArgument.m in Sources */,
D1C6124919891B3700FDB554 /* VVSwiftEnumCommenter.m in Sources */,
Expand Down
1 change: 1 addition & 0 deletions VVDocumenter-Xcode/Commenter/VVCommenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
#import "VVSwiftEnumCommenter.h"
#import "VVSwiftPropertyCommenter.h"
#import "VVSwiftExtensionCommenter.h"
#import "VVSwiftClassCommenter.h"

#endif
13 changes: 13 additions & 0 deletions VVDocumenter-Xcode/Commenter/VVSwiftClassCommenter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// VVSwiftClassCommenter.h
// VVDocumenter-Xcode
//
// Created by Li Jiantang on 16/06/2016.
// Copyright © 2016 OneV's Den. All rights reserved.
//

#import "VVCommenter.h"

@interface VVSwiftClassCommenter : VVBaseCommenter

@end
59 changes: 59 additions & 0 deletions VVDocumenter-Xcode/Commenter/VVSwiftClassCommenter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// VVSwiftClassCommenter.m
// VVDocumenter-Xcode
//
// Created by Li Jiantang on 16/06/2016.
// Copyright © 2016 OneV's Den. All rights reserved.
//

#import "VVSwiftClassCommenter.h"
#import "NSString+VVTextGetter.h"
#import "VVTextResult.h"
#import "VVArgument.h"

@interface VVSwiftClassCommenter()

@property (nonatomic, strong) NSString *className;
@property (nonatomic, strong) NSArray *inheritances;

@end

@implementation VVSwiftClassCommenter

-(void) captureClassName
{
NSArray *groups = [self.code vv_stringsByExtractingGroupsUsingRegexPattern:@"^\\s*((.*\\s)?class\\s+)([^:]+):?(.*)\\{"];

self.className = [groups objectAtIndex:1];
if ([groups count] > 3) {
self.className = [groups objectAtIndex:2];
}

self.inheritances = [[[groups lastObject] componentsSeparatedByString:@","] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSString *evaluatedString, NSDictionary<NSString *,id> * _Nullable bindings) {

NSString *strippedString = [evaluatedString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

return strippedString.length > 0;
}]];
}

-(NSString *) document
{
[self captureClassName];

NSMutableString *finalString = [NSMutableString new];
[finalString appendFormat:@"/**\n%@class %@\n\n<#Description#>\n\n", self.indent, self.className];

if ([self.inheritances count] > 0) {
[finalString appendString:@"inherit from or conform to:\n"];
for (NSString *inheritance in self.inheritances) {
[finalString appendFormat:@"%@: <#Description#>\n", [inheritance stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
}

[finalString appendFormat:@"\n %@*/", self.indent];

return finalString;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ -(NSArray *) vv_stringsByExtractingGroupsUsingRegexPattern:(NSString *)regex cas
__block NSMutableArray *retVal = [NSMutableArray array];
[pattern enumerateMatchesInString:self options:0 range:NSMakeRange(0, [self length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
//Note, we only want to return the things in parens, so we're skipping index 0 intentionally
for (int i=1; i<[result numberOfRanges]; i++) {
NSString *matchedString=[self substringWithRange:[result rangeAtIndex:i]];
[retVal addObject:matchedString];
if ([result numberOfRanges] > 1) {
for (int i=1; i<[result numberOfRanges]; i++) {
NSRange range = [result rangeAtIndex:i];
if (range.location != NSNotFound) {
NSString *matchedString=[self substringWithRange:range];
[retVal addObject:matchedString];
}
}
}
}];
return retVal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
-(BOOL) vv_isStruct;
-(BOOL) vv_isUnion;
-(BOOL) vv_isComplieKeyword;
-(BOOL) vv_isSwiftClass;
-(BOOL) vv_isSwiftFunction;
-(BOOL) vv_isSwiftEnum;
-(BOOL) vv_isSwiftProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,20 @@ -(BOOL) vv_isSwiftFunction
return ![self vv_isObjCMethod] && ![self vv_isSwiftProperty] && [self vv_matchesPatternRegexPattern:@"^\\s*(.*\\s+)?(func\\s+)|(init|deinit|subscript)"];
}

-(BOOL) vv_isSwiftClass
{
return ![self vv_isSwiftFunction] && [self vv_matchesPatternRegexPattern:@"^\\s*(.*\\s+)?(class\\s+).*\\{"];
}

-(BOOL) vv_isSwiftEnum
{
return ![self vv_isSwiftProperty] && [self vv_matchesPatternRegexPattern:@"^\\s*(.*\\s+)?enum\\s+"];
}

-(BOOL) vv_isSwiftProperty
{
// Opt out the situation of `class func`
if ([self vv_matchesPatternRegexPattern:@"class func"]) {
// Opt out the situation of `class func` and `class`
if ([self vv_matchesPatternRegexPattern:@"^\\s*(.*\\s+)?(class\\s+).*\\{"]) {
return NO;
}

Expand Down
2 changes: 2 additions & 0 deletions VVDocumenter-Xcode/VVDocumenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ -(NSString *) document
commenter = [[VVStructCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
} else if ([trimCode vv_isObjCMethod]) {
commenter = [[VVMethodCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
} else if ([trimCode vv_isSwiftClass]) {
commenter = [[VVSwiftClassCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
} else if ([trimCode vv_isSwiftFunction]) {
commenter = [[VVSwiftFunctionCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
} else if ([trimCode vv_isSwiftProperty]) {
Expand Down
Loading