Skip to content

Commit b2c8852

Browse files
author
Dominik Hadl
committed
Added NS_ENUM support
1 parent d8e123f commit b2c8852

3 files changed

Lines changed: 39 additions & 45 deletions

File tree

VVDocumenter-Xcode/Commenter/VVEnumCommenter.m

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,33 @@
1111
@implementation VVEnumCommenter
1212

1313
- (NSString *)document {
14-
NSString *removedFirstPart = [self.code vv_stringByReplacingRegexPattern:@"^\\s*(\\w+\\s)?NS_ENUM.*\\{" withString:@""];
15-
removedFirstPart = [removedFirstPart vv_stringByReplacingRegexPattern:@"^\\s*(\\w+\\s)?enum.*\\{" withString:@""];
16-
VVLog(@"String without first part - %@", removedFirstPart);
17-
NSString *removedEnd = [removedFirstPart vv_stringByReplacingRegexPattern:@"[}]$" withString:@""];
18-
VVLog(@"String without end - %@", removedEnd);
19-
20-
NSArray *comp = [removedEnd componentsSeparatedByString:@","];
21-
VVLog(@"comp array - %@", comp);
22-
23-
NSString *final = [NSString stringWithFormat:@"%@%@%@\n",[self startComment],
24-
[self sinceComment],
25-
[self endComment]];
14+
NSString *enumPartsString = [[self.code vv_stringByReplacingRegexPattern:@"^\\s*(\\w+\\s)?NS_ENUM.*\\{" withString:@""]
15+
vv_stringByReplacingRegexPattern:@"[}]$" withString:@""];
16+
17+
NSArray *enumParts = [enumPartsString componentsSeparatedByString:@","];
18+
19+
NSString *finalString = [NSString stringWithFormat:@"%@%@%@\n", [self startComment],
20+
[self sinceComment],
21+
[self endComment]];
2622

2723
NSRegularExpression *ex = [NSRegularExpression regularExpressionWithPattern:@"^\\s*(\\w+\\s)?NS_ENUM.*\\{" options:0 error:nil];
2824
NSTextCheckingResult *res = [ex firstMatchInString:self.code options:0 range:NSMakeRange(0, self.code.length)];
2925

30-
final = [final stringByAppendingString:[self.code substringWithRange:[res rangeAtIndex:0]]];
31-
final = [final stringByAppendingString:@"\n"];
26+
finalString = [finalString stringByAppendingString:[self.code substringWithRange:[res rangeAtIndex:0]]];
27+
finalString = [finalString stringByAppendingString:@"\n"];
3228

33-
for (NSString *co in comp) {
34-
NSString *tem = [NSString stringWithFormat:@"%@%@%@%@", [self startComment],
35-
[self sinceComment],
36-
[self endComment],
37-
co];
38-
if (co != [comp lastObject]) {
39-
tem = [tem stringByAppendingString:@",\n"];
29+
for (NSString *part in enumParts) {
30+
NSString *temp = [NSString stringWithFormat:@"%@%@%@%@", [self startComment],
31+
[self sinceComment],
32+
[self endComment],
33+
part];
34+
if (part != [enumParts lastObject]) {
35+
temp = [temp stringByAppendingString:@",\n"];
4036
}
41-
final = [final stringByAppendingString:tem];
37+
finalString = [finalString stringByAppendingString:temp];
4238
}
43-
// final = [final stringByAppendingString:@"};"];
44-
45-
return final;
39+
40+
return finalString;
4641
}
4742

4843
@end

VVDocumenter-Xcode/VVDocumenter.m

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,9 @@ -(id) initWithCode:(NSString *)code
3434
self.code = [[code vv_stringByReplacingRegexPattern:@"\\s*(\\(.*\?\\))\\s*" withString:@"$1"]
3535
vv_stringByReplacingRegexPattern:@"\\s*\n\\s*" withString:@" "];
3636
}
37-
VVLog(@"VVDocumenter Code - %@", self.code);
38-
3937
}
4038
return self;
4139
}
42-
//**
43-
typedef NS_ENUM(NSInteger, ssss) {
44-
aaa,
45-
bbb,
46-
abbb
47-
};
4840

4941
-(NSString *) baseIndentation
5042
{
@@ -63,29 +55,21 @@ -(NSString *) document
6355

6456
VVBaseCommenter *commenter = nil;
6557

66-
if (self.isEnum) {
67-
VVLog(@"Type - enum");
58+
if (self.isEnum) {
6859
commenter = [[VVEnumCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
6960
} else if ([trimCode vv_isProperty]) {
70-
VVLog(@"Type - property");
7161
commenter = [[VVPropertyCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
7262
} else if ([trimCode vv_isCFunction]) {
73-
VVLog(@"Type - c function");
7463
commenter = [[VVFunctionCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
7564
} else if ([trimCode vv_isMacro]) {
76-
VVLog(@"Type - macro");
7765
commenter = [[VVMacroCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
7866
} else if ([trimCode vv_isStruct]) {
79-
VVLog(@"Type - struct");
8067
commenter = [[VVStructCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
8168
} else if ([trimCode vv_isUnion]) {
82-
VVLog(@"Type - union");
8369
commenter = [[VVStructCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
8470
} else if ([trimCode vv_isObjCMethod]) {
85-
VVLog(@"Type - objc-method");
8671
commenter = [[VVMethodCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
8772
} else {
88-
VVLog(@"Type - variable");
8973
commenter = [[VVVariableCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
9074
}
9175

VVDocumenter-Xcode/VVDocumenterManager.m

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ - (void) textStorageDidChange:(NSNotification *)noti {
9292
if ([currentLineResult.string vv_matchesPatternRegexPattern:[NSString stringWithFormat:@"^\\s*%@$",[NSRegularExpression escapedPatternForString:triggerString]]] && self.prefixTyped) {
9393
self.prefixTyped = NO;
9494
//Get a @"///" typed in by user. Do work!
95-
BOOL shouldReplace = NO;
95+
__block BOOL shouldReplace = NO;
9696
//Decide which is closer to the cursor. A semicolon or a half brace.
9797
//We just want to document the next valid line.
9898
VVTextResult *resultUntilSemiColon = [textView textResultUntilNextString:@";"];
@@ -125,15 +125,16 @@ - (void) textStorageDidChange:(NSNotification *)noti {
125125
//Set the doc comments in it
126126
[pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
127127
[pasteBoard setString:[doc document] forType:NSStringPboardType];
128-
128+
129129
//Begin to simulate keyborad pressing
130130
VVKeyboardEventSender *kes = [[VVKeyboardEventSender alloc] init];
131131
[kes beginKeyBoradEvents];
132132
//Cmd+delete Delete current line
133133
[kes sendKeyCode:kVK_Delete withModifierCommand:YES alt:NO shift:NO control:NO];
134-
if (shouldReplace) [textView setSelectedRange:resultToDocument.range];
134+
//if (shouldReplace) [textView setSelectedRange:resultToDocument.range];
135135
//Cmd+V, paste
136136
[kes sendKeyCode:kVK_ANSI_V withModifierCommand:YES alt:NO shift:NO control:NO];
137+
137138
//The key down is just a defined finish signal by me. When we receive this key, we know operation above is finished.
138139
[kes sendKeyCode:kVK_F20];
139140

@@ -146,6 +147,14 @@ - (void) textStorageDidChange:(NSNotification *)noti {
146147
//Restore previois patse board content
147148
[pasteBoard setString:originPBString forType:NSStringPboardType];
148149

150+
if (shouldReplace) {
151+
// Quick fix for newline and bad position with NS_ENUM
152+
// Should be fixed better
153+
[kes sendKeyCode:kVK_Delete withModifierCommand:NO alt:NO shift:NO control:NO];
154+
[kes sendKeyCode:kVK_DownArrow withModifierCommand:NO alt:NO shift:NO control:NO];
155+
[kes sendKeyCode:kVK_LeftArrow withModifierCommand:YES alt:NO shift:NO control:NO];
156+
}
157+
149158
//Set cursor before the inserted documentation. So we can use tab to begin edit.
150159
int baseIndentationLength = (int)[doc baseIndentation].length;
151160
[textView setSelectedRange:NSMakeRange(currentLineResult.range.location + baseIndentationLength, 0)];
@@ -154,8 +163,14 @@ - (void) textStorageDidChange:(NSNotification *)noti {
154163
[kes sendKeyCode:kVK_Tab];
155164
[kes endKeyBoradEvents];
156165

166+
shouldReplace = NO;
167+
157168
//Invalidate the finish signal, in case you set it to do some other thing.
158169
return nil;
170+
} else if ([incomingEvent type] == NSKeyDown && [incomingEvent keyCode] == kVK_ANSI_V && shouldReplace == YES) {
171+
172+
[textView setSelectedRange:[textView textResultUntilNextString:@";"].range];
173+
return incomingEvent;
159174
} else {
160175
return incomingEvent;
161176
}

0 commit comments

Comments
 (0)