Skip to content

Commit 9dcb903

Browse files
committed
Merge branch 'enum' of github.com:nickskull/VVDocumenter-Xcode into nickskull-enum
2 parents fc087c1 + ce96d8a commit 9dcb903

5 files changed

Lines changed: 80 additions & 13 deletions

File tree

VVDocumenter-Xcode/Commenter/VVBaseCommenter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@
1818
-(id) initWithIndentString:(NSString *)indent codeString:(NSString *)code;
1919
-(NSString *) document;
2020
-(void) parseArguments;
21+
22+
// Comment methods
23+
-(NSString *) startComment;
24+
-(NSString *) argumentsComment;
25+
-(NSString *) endComment;
26+
-(NSString *) returnComment;
27+
-(NSString *) sinceComment;
28+
2129
@end

VVDocumenter-Xcode/Commenter/VVEnumCommenter.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,34 @@
1010

1111
@implementation VVEnumCommenter
1212

13+
- (NSString *)document {
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]];
22+
23+
NSRegularExpression *ex = [NSRegularExpression regularExpressionWithPattern:@"^\\s*(\\w+\\s)?NS_ENUM.*\\{" options:0 error:nil];
24+
NSTextCheckingResult *res = [ex firstMatchInString:self.code options:0 range:NSMakeRange(0, self.code.length)];
25+
26+
finalString = [finalString stringByAppendingString:[self.code substringWithRange:[res rangeAtIndex:0]]];
27+
finalString = [finalString stringByAppendingString:@"\n"];
28+
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"];
36+
}
37+
finalString = [finalString stringByAppendingString:temp];
38+
}
39+
40+
return finalString;
41+
}
42+
1343
@end

VVDocumenter-Xcode/OCCategory/NSString+VVSyntax/NSString+VVSyntax.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ -(BOOL) vv_isObjCMethod
2323

2424
-(BOOL) vv_isCFunction
2525
{
26-
return ![self vv_isMacro] && ![self vv_isObjCMethod] && ![self vv_isProperty] && ![self vv_isComplieKeyword] && [self vv_matchesPatternRegexPattern:@".+\\s+.+\\("];
26+
return ![self vv_isEnum] && ![self vv_isMacro] && ![self vv_isObjCMethod] && ![self vv_isProperty] && ![self vv_isComplieKeyword] && [self vv_matchesPatternRegexPattern:@".+\\s+.+\\("];
2727
}
2828

2929
-(BOOL) vv_isProperty
@@ -43,7 +43,7 @@ -(BOOL) vv_isStruct
4343

4444
-(BOOL) vv_isEnum
4545
{
46-
return [self vv_matchesPatternRegexPattern:@"^\\s*(\\w+\\s)?enum.*\\{"];
46+
return [self vv_matchesPatternRegexPattern:@"^\\s*(\\w+\\s)?NS_ENUM.*\\{"];
4747
}
4848

4949
-(BOOL) vv_isUnion

VVDocumenter-Xcode/VVDocumenter.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
@interface VVDocumenter()
1414

1515
@property (nonatomic, copy) NSString *code;
16+
@property (nonatomic, assign) BOOL isEnum;
1617

1718
@end
1819

@@ -24,9 +25,15 @@ -(id) initWithCode:(NSString *)code
2425
if (self) {
2526
//Trim the space around the braces
2627
//Then trim the new line character
27-
self.code = [[code vv_stringByReplacingRegexPattern:@"\\s*(\\(.*\?\\))\\s*" withString:@"$1"]
28-
vv_stringByReplacingRegexPattern:@"\\s*\n\\s*" withString:@" "];
28+
self.isEnum = NO;
2929

30+
if ([code vv_isEnum]) {
31+
self.code = code;
32+
self.isEnum = YES;
33+
} else {
34+
self.code = [[code vv_stringByReplacingRegexPattern:@"\\s*(\\(.*\?\\))\\s*" withString:@"$1"]
35+
vv_stringByReplacingRegexPattern:@"\\s*\n\\s*" withString:@" "];
36+
}
3037
}
3138
return self;
3239
}
@@ -47,8 +54,9 @@ -(NSString *) document
4754
NSString *baseIndent = [self baseIndentation];
4855

4956
VVBaseCommenter *commenter = nil;
50-
if ([trimCode vv_isObjCMethod]) {
51-
commenter = [[VVMethodCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
57+
58+
if (self.isEnum) {
59+
commenter = [[VVEnumCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
5260
} else if ([trimCode vv_isProperty]) {
5361
commenter = [[VVPropertyCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
5462
} else if ([trimCode vv_isCFunction]) {
@@ -59,8 +67,8 @@ -(NSString *) document
5967
commenter = [[VVStructCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
6068
} else if ([trimCode vv_isUnion]) {
6169
commenter = [[VVStructCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
62-
} else if ([trimCode vv_isEnum]) {
63-
commenter = [[VVEnumCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
70+
} else if ([trimCode vv_isObjCMethod]) {
71+
commenter = [[VVMethodCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
6472
} else {
6573
commenter = [[VVVariableCommenter alloc] initWithIndentString:baseIndent codeString:trimCode];
6674
}

VVDocumenter-Xcode/VVDocumenterManager.m

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ -(void) addSettingMenu
5858
if (editMenuItem) {
5959
[[editMenuItem submenu] addItem:[NSMenuItem separatorItem]];
6060

61-
NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:@"VVDocumenter" action:@selector(showSettingPanle:) keyEquivalent:@""];
61+
NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:@"VVDocumenter" action:@selector(showSettingPanel:) keyEquivalent:@""];
6262

6363
[newMenuItem setTarget:self];
6464
[[editMenuItem submenu] addItem:newMenuItem];
6565
[newMenuItem release];
6666
}
6767
}
6868

69-
-(void) showSettingPanle:(NSNotification *)noti {
69+
-(void) showSettingPanel:(NSNotification *)noti {
7070
VVDSettingPanelWindowController *panelController = [[VVDSettingPanelWindowController alloc] initWithWindowNibName:@"VVDSettingPanelWindowController"];
7171
[panelController showWindow:panelController];
7272
}
@@ -92,12 +92,12 @@ - (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-
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:@";"];
9999
VVTextResult *resultUntilBrace = [textView textResultUntilNextString:@"{"];
100-
100+
101101
VVTextResult *resultToDocument = nil;
102102

103103
if (resultUntilSemiColon && resultUntilBrace) {
@@ -108,6 +108,11 @@ - (void) textStorageDidChange:(NSNotification *)noti {
108108
resultToDocument = resultUntilSemiColon;
109109
}
110110

111+
if ([resultToDocument.string vv_isEnum]) {
112+
resultToDocument = resultUntilSemiColon;
113+
shouldReplace = YES;
114+
}
115+
111116
VVDocumenter *doc = [[VVDocumenter alloc] initWithCode:resultToDocument.string];
112117

113118
//Now we are using a simulation of keyboard event to insert the docs, instead of using the IDE's private method.
@@ -120,14 +125,16 @@ - (void) textStorageDidChange:(NSNotification *)noti {
120125
//Set the doc comments in it
121126
[pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
122127
[pasteBoard setString:[doc document] forType:NSStringPboardType];
123-
128+
124129
//Begin to simulate keyborad pressing
125130
VVKeyboardEventSender *kes = [[VVKeyboardEventSender alloc] init];
126131
[kes beginKeyBoradEvents];
127132
//Cmd+delete Delete current line
128133
[kes sendKeyCode:kVK_Delete withModifierCommand:YES alt:NO shift:NO control:NO];
134+
//if (shouldReplace) [textView setSelectedRange:resultToDocument.range];
129135
//Cmd+V, paste
130136
[kes sendKeyCode:kVK_ANSI_V withModifierCommand:YES alt:NO shift:NO control:NO];
137+
131138
//The key down is just a defined finish signal by me. When we receive this key, we know operation above is finished.
132139
[kes sendKeyCode:kVK_F20];
133140

@@ -140,6 +147,14 @@ - (void) textStorageDidChange:(NSNotification *)noti {
140147
//Restore previois patse board content
141148
[pasteBoard setString:originPBString forType:NSStringPboardType];
142149

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+
143158
//Set cursor before the inserted documentation. So we can use tab to begin edit.
144159
int baseIndentationLength = (int)[doc baseIndentation].length;
145160
[textView setSelectedRange:NSMakeRange(currentLineResult.range.location + baseIndentationLength, 0)];
@@ -148,8 +163,14 @@ - (void) textStorageDidChange:(NSNotification *)noti {
148163
[kes sendKeyCode:kVK_Tab];
149164
[kes endKeyBoradEvents];
150165

166+
shouldReplace = NO;
167+
151168
//Invalidate the finish signal, in case you set it to do some other thing.
152169
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;
153174
} else {
154175
return incomingEvent;
155176
}

0 commit comments

Comments
 (0)