@@ -13,37 +13,46 @@ @implementation SourceEditorCommand
1313
1414- (void )performCommandWithInvocation : (XCSourceEditorCommandInvocation *)invocation completionHandler : (void (^)(NSError * _Nullable nilOrError))completionHandler
1515{
16-
17- if ([invocation.commandIdentifier isEqualToString: @" net.shengpan.CodeGenerator.SourceEditorPlugin.SourceEditorCommand" ])
16+ // get selected String
17+ NSMutableArray *selections = [NSMutableArray array ];
18+ for ( XCSourceTextRange *range in invocation.buffer .selections )
1819 {
19- NSMutableArray *selections = [NSMutableArray array ];
20- for ( XCSourceTextRange *range in invocation.buffer .selections )
20+ NSInteger startLine = range.start .line ;
21+ // make user select line easier
22+ NSInteger endLine = range.end .column > 0 ? range.end .line + 1 : range.end .line ;
23+
24+ for ( NSInteger i = startLine; i < endLine ; i++)
2125 {
22- NSInteger startLine = range.start .line ;
23- // make user select line easier
24- NSInteger endLine = range.end .column > 0 ? range.end .line + 1 : range.end .line ;
25-
26- for ( NSInteger i = startLine; i < endLine ; i++)
27- {
28- [selections addObject: invocation.buffer.lines[i]];
29- }
26+ [selections addObject: invocation.buffer.lines[i]];
3027 }
31- NSString *selectedString = [selections componentsJoinedByString: @" " ];
32-
28+ }
29+ NSString *selectedString = [selections componentsJoinedByString: @" " ];
30+
31+ // find the insert location
32+ NSString *interface = [self interfaceContainerInBuffer: invocation.buffer
33+ initialLine: invocation.buffer.selections.firstObject.start.line];
34+ NSInteger insertIndex = [self endLineOfImplementationForInterface: interface buffer: invocation.buffer];
35+ insertIndex = (insertIndex == NSNotFound )
36+ ? invocation.buffer .selections .lastObject .end .line + 1
37+ : insertIndex;
38+
39+ // do something for each command
40+ if ([invocation.commandIdentifier isEqualToString: @" net.shengpan.CodeGenerator.SourceEditorPlugin.SourceEditorCommand" ])
41+ {
3342 NSArray *getter = [AccessCodeGenerator lazyGetterForString: selectedString];
34-
35- NSString *interface = [self interfaceContainerInBuffer: invocation.buffer
36- initialLine: invocation.buffer.selections.firstObject.start.line];
37- NSInteger insertIndex = [self endLineOfImplementationForInterface: interface buffer: invocation.buffer];
38- insertIndex = (insertIndex == NSNotFound )
39- ? invocation.buffer .selections .lastObject .end .line + 1
40- : insertIndex;
41-
4243 for (NSInteger i = 0 ; i < getter.count ; i++)
4344 {
4445 [invocation.buffer.lines insertObject: getter[i] atIndex: insertIndex];
4546 }
47+ } else if ([invocation.commandIdentifier isEqualToString: @" net.shengpan.CodeGenerator.SourceEditorPlugin.Setter" ]) {
48+
49+ NSArray *setter = [AccessCodeGenerator setterForString: selectedString];
50+ for (NSInteger i = 0 ; i < setter.count ; i++)
51+ {
52+ [invocation.buffer.lines insertObject: setter[i] atIndex: insertIndex];
53+ }
4654 }
55+
4756 completionHandler (nil );
4857}
4958
0 commit comments