Skip to content

Commit 9e2d479

Browse files
committed
Fix unwanted comment doc for the last comma (if exist in the body of a enum)
Signed-off-by: onevcat <onevcat@gmail.com>
1 parent 28fe6c5 commit 9e2d479

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

VVDocumenter-Xcode/Commenter/VVEnumCommenter.m

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,41 @@
1111
@implementation VVEnumCommenter
1212

1313
- (NSString *)document {
14-
NSString *enumDefinePattern = @"^\\s*(\\w+\\s+)?NS_ENUM.*\\{";
15-
NSString *enumPartsString = [[self.code vv_stringByReplacingRegexPattern:enumDefinePattern withString:@""]
16-
vv_stringByReplacingRegexPattern:@"[}]$" withString:@""];
17-
18-
NSArray *enumParts = [enumPartsString componentsSeparatedByString:@","];
19-
14+
//Regular comment documentation
2015
NSString *finalString = [NSString stringWithFormat:@"%@%@%@\n", [self startComment],
2116
[self sinceComment],
2217
[self endComment]];
2318

24-
NSRegularExpression *ex = [NSRegularExpression regularExpressionWithPattern:enumDefinePattern options:0 error:nil];
25-
NSTextCheckingResult *res = [ex firstMatchInString:self.code options:0 range:NSMakeRange(0, self.code.length)];
19+
NSString *enumDefinePattern = @"^\\s*(\\w+\\s+)?NS_ENUM.*\\{";
20+
21+
NSRegularExpression *enumDefineExpression = [NSRegularExpression regularExpressionWithPattern:enumDefinePattern options:0 error:nil];
22+
NSTextCheckingResult *enumDefineResult = [enumDefineExpression firstMatchInString:self.code options:0 range:NSMakeRange(0, self.code.length)];
2623

27-
finalString = [finalString stringByAppendingString:[self.code substringWithRange:[res rangeAtIndex:0]]];
24+
finalString = [finalString stringByAppendingString:[self.code substringWithRange:[enumDefineResult rangeAtIndex:0]]];
2825
finalString = [finalString stringByAppendingString:@"\n"];
2926

27+
NSString *endPattern = @"\\}\\s*;";
28+
NSString *enumPartsString = [[self.code vv_stringByReplacingRegexPattern:enumDefinePattern withString:@""]
29+
vv_stringByReplacingRegexPattern:endPattern withString:@""];
30+
NSArray *enumParts = [enumPartsString componentsSeparatedByString:@","];
31+
3032
for (NSString *part in enumParts) {
31-
NSString *temp = [NSString stringWithFormat:@"%@%@%@%@", [self startComment],
32-
[self sinceComment],
33-
[self endComment],
34-
part];
35-
if (part != [enumParts lastObject]) {
36-
temp = [temp stringByAppendingString:@",\n"];
33+
NSLog(@"%@",part);
34+
//Only append when there is a enum define. (In case of the last comma, followed no define)
35+
if ([part stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length != 0) {
36+
NSString *temp = [NSString stringWithFormat:@"%@%@%@%@", [self startComment],
37+
[self sinceComment],
38+
[self endComment],
39+
part];
40+
if (part != [enumParts lastObject]) {
41+
temp = [temp stringByAppendingString:@",\n"];
42+
}
43+
NSLog(@"%@",temp);
44+
finalString = [finalString stringByAppendingString:temp];
3745
}
38-
finalString = [finalString stringByAppendingString:temp];
3946
}
4047

41-
return finalString;
48+
return [finalString stringByAppendingString:@"};"];
4249
}
4350

4451
@end

0 commit comments

Comments
 (0)