diff --git a/CodeGenerator/Info.plist b/CodeGenerator/Info.plist index ac41fcc..1b31717 100644 --- a/CodeGenerator/Info.plist +++ b/CodeGenerator/Info.plist @@ -17,9 +17,9 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.0.1 + 1.1.0 CFBundleVersion - 3 + 6 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSHumanReadableCopyright diff --git a/Source/AccessCodeGenerator.h b/Source/AccessCodeGenerator.h index 3cdbb9c..aa53dc7 100644 --- a/Source/AccessCodeGenerator.h +++ b/Source/AccessCodeGenerator.h @@ -14,9 +14,19 @@ /** pattern matching the string, recognize Objective-C propertyies, generate their lazy getter code and return. + basic data type and id will be ignored. + @param string a string @return lazy getter code */ + (NSArray *)lazyGetterForString:(NSString *)string; +/** + pattern matching the string, recognize Objective-C propertyies, generate their setter code and return. + + @param string a string + @return setter code + */ ++ (NSArray *)setterForString:(NSString *)string; + @end diff --git a/Source/AccessCodeGenerator.m b/Source/AccessCodeGenerator.m index 9f17593..704de28 100644 --- a/Source/AccessCodeGenerator.m +++ b/Source/AccessCodeGenerator.m @@ -19,6 +19,15 @@ @implementation AccessCodeGenerator return result; } ++ (NSArray *)setterForString:(NSString *)string { + NSArray *props = [self propertyWithContent:string]; + NSMutableArray *result = [NSMutableArray array]; + for (PSProperty *model in props) { + [result addObject:[self setterWithPSProperty:model]]; + } + return result; +} + + (NSArray *)propertyWithContent:(NSString *)content { NSMutableArray *result = [NSMutableArray array]; @@ -48,6 +57,7 @@ @implementation AccessCodeGenerator proMoel.keywords = keywords; proMoel.dataType = [[dataTypeAndName subarrayWithRange:NSMakeRange(0, dataTypeAndName.count - 1)] componentsJoinedByString:@" *"]; proMoel.name = [dataTypeAndName lastObject]; + proMoel.isObjectType = [propertyString containsString:@"*"]; [result addObject:proMoel]; } } @@ -70,6 +80,7 @@ @implementation AccessCodeGenerator NSString *resultString = [[propertyStr1 substringWithRange:firstHalfRange] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; resultString = [resultString stringByReplacingOccurrencesOfString:@"(" withString:@""]; resultString = [resultString stringByReplacingOccurrencesOfString:@")" withString:@""]; + resultString = [resultString stringByReplacingOccurrencesOfString:@" " withString:@""]; return [resultString componentsSeparatedByString:@","]; } } @@ -111,9 +122,6 @@ + (NSMutableArray *)propertyTypeAndNameWithProperty:(NSString *)propertyStr; [result removeObject:obj]; } }]; - if (![result containsObject:ID]) { - [result removeAllObjects]; - } } return result; } @@ -138,5 +146,24 @@ + (NSString *)getterWithPSProperty:(PSProperty *)model return lazyGetter; } ++ (NSString *)setterWithPSProperty:(PSProperty *)model { + NSString *setter = @""; + if (model.isObjectType) { + setter = [NSString stringWithFormat:@"\n- (void)set%@:(%@ *)%@ {\n _%@ = %@;\n}", + [model.name capitalizedString], + model.dataType, + model.name, + model.name, + model.name]; + } else { + setter = [NSString stringWithFormat:@"\n- (void)set%@:(%@)%@ {\n _%@ = %@;\n}", + [model.name capitalizedString], + model.dataType, + model.name, + model.name, + model.name]; + } + return setter; +} @end diff --git a/Source/PSProperty.h b/Source/PSProperty.h index 99596e3..e43f90d 100644 --- a/Source/PSProperty.h +++ b/Source/PSProperty.h @@ -10,10 +10,9 @@ static NSString *const ASSIGN = @"assign"; static NSString *const WEAK = @"weak"; - static NSString *const ID = @"id"; - static NSString *const IB_OUTLET = @"IBOutlet"; +//static NSString *const BASIC_DATA_TYPE = @"NSInteger,NSUInteger,char,int,float,double,long,short,signed,unsigned,BOOL,Bool,bool,Boolean"; @interface PSProperty : NSObject @@ -24,9 +23,10 @@ static NSString *const IB_OUTLET = @"IBOutlet"; /** * 数据类型 */ -@property (nonatomic,strong) NSString * dataType; +@property (nonatomic,strong) NSString *dataType; /** * 属性名称 */ -@property (nonatomic,strong) NSString * name; +@property (nonatomic,strong) NSString *name; +@property (nonatomic, assign) BOOL isObjectType;/**< 是不是带 * 的类型*/ @end diff --git a/Source/PSProperty.m b/Source/PSProperty.m index 3a6fac1..c0f64ab 100644 --- a/Source/PSProperty.m +++ b/Source/PSProperty.m @@ -13,6 +13,8 @@ @interface PSProperty () @property (nonatomic, strong) NSString *string; @property (nonatomic, strong) NSArray *array; @property (nonatomic, strong) PSProperty *node; +@property (nonatomic, assign) NSInteger haha; +@property (nonatomic, strong) id idnAme; @end diff --git a/SourceEditorPlugin/Info.plist b/SourceEditorPlugin/Info.plist index 710ac9f..a63d748 100644 --- a/SourceEditorPlugin/Info.plist +++ b/SourceEditorPlugin/Info.plist @@ -17,9 +17,9 @@ CFBundlePackageType XPC! CFBundleShortVersionString - 1.0.1 + 1.1.0 CFBundleVersion - 3 + 6 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSExtension @@ -36,6 +36,14 @@ XCSourceEditorCommandName Lazy Getter + + XCSourceEditorCommandClassName + SourceEditorCommand + XCSourceEditorCommandIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER).Setter + XCSourceEditorCommandName + Setter + XCSourceEditorExtensionPrincipalClass SourceEditorExtension diff --git a/SourceEditorPlugin/SourceEditorCommand.m b/SourceEditorPlugin/SourceEditorCommand.m index 8853277..4ae5ec4 100644 --- a/SourceEditorPlugin/SourceEditorCommand.m +++ b/SourceEditorPlugin/SourceEditorCommand.m @@ -13,37 +13,46 @@ @implementation SourceEditorCommand - (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation *)invocation completionHandler:(void (^)(NSError * _Nullable nilOrError))completionHandler { - - if ([invocation.commandIdentifier isEqualToString:@"net.shengpan.CodeGenerator.SourceEditorPlugin.SourceEditorCommand"]) + // get selected String + NSMutableArray *selections = [NSMutableArray array]; + for ( XCSourceTextRange *range in invocation.buffer.selections ) { - NSMutableArray *selections = [NSMutableArray array]; - for ( XCSourceTextRange *range in invocation.buffer.selections ) + NSInteger startLine = range.start.line; + // make user select line easier + NSInteger endLine = range.end.column > 0 ? range.end.line + 1 : range.end.line; + + for ( NSInteger i = startLine; i < endLine ; i++) { - NSInteger startLine = range.start.line; - // make user select line easier - NSInteger endLine = range.end.column > 0 ? range.end.line + 1 : range.end.line; - - for ( NSInteger i = startLine; i < endLine ; i++) - { - [selections addObject:invocation.buffer.lines[i]]; - } + [selections addObject:invocation.buffer.lines[i]]; } - NSString *selectedString = [selections componentsJoinedByString:@""]; - + } + NSString *selectedString = [selections componentsJoinedByString:@""]; + + // find the insert location + NSString *interface = [self interfaceContainerInBuffer:invocation.buffer + initialLine:invocation.buffer.selections.firstObject.start.line]; + NSInteger insertIndex = [self endLineOfImplementationForInterface:interface buffer:invocation.buffer]; + insertIndex = (insertIndex == NSNotFound) + ? invocation.buffer.selections.lastObject.end.line + 1 + : insertIndex; + + // do something for each command + if ([invocation.commandIdentifier isEqualToString:@"net.shengpan.CodeGenerator.SourceEditorPlugin.SourceEditorCommand"]) + { NSArray *getter = [AccessCodeGenerator lazyGetterForString:selectedString]; - - NSString *interface = [self interfaceContainerInBuffer:invocation.buffer - initialLine:invocation.buffer.selections.firstObject.start.line]; - NSInteger insertIndex = [self endLineOfImplementationForInterface:interface buffer:invocation.buffer]; - insertIndex = (insertIndex == NSNotFound) - ? invocation.buffer.selections.lastObject.end.line + 1 - : insertIndex; - for (NSInteger i = 0; i < getter.count; i++) { [invocation.buffer.lines insertObject:getter[i] atIndex:insertIndex]; } + } else if ([invocation.commandIdentifier isEqualToString:@"net.shengpan.CodeGenerator.SourceEditorPlugin.Setter"]) { + + NSArray *setter = [AccessCodeGenerator setterForString:selectedString]; + for (NSInteger i = 0; i < setter.count; i++) + { + [invocation.buffer.lines insertObject:setter[i] atIndex:insertIndex]; + } } + completionHandler(nil); }