@@ -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