Skip to content

Commit

Permalink
[Update] Add Setter & filter basic data type
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPans committed Jun 8, 2017
1 parent 807ab24 commit ac794a7
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 33 deletions.
4 changes: 2 additions & 2 deletions CodeGenerator/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.1</string>
<string>1.1.0</string>
<key>CFBundleVersion</key>
<string>3</string>
<string>6</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
10 changes: 10 additions & 0 deletions Source/AccessCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSString *> *)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<NSString *> *)setterForString:(NSString *)string;

@end
33 changes: 30 additions & 3 deletions Source/AccessCodeGenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ @implementation AccessCodeGenerator
return result;
}

+ (NSArray<NSString *> *)setterForString:(NSString *)string {
NSArray<PSProperty *> *props = [self propertyWithContent:string];
NSMutableArray *result = [NSMutableArray array];
for (PSProperty *model in props) {
[result addObject:[self setterWithPSProperty:model]];
}
return result;
}

+ (NSArray<PSProperty *> *)propertyWithContent:(NSString *)content
{
NSMutableArray *result = [NSMutableArray array];
Expand Down Expand Up @@ -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];
}
}
Expand All @@ -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:@","];
}
}
Expand Down Expand Up @@ -111,9 +122,6 @@ + (NSMutableArray *)propertyTypeAndNameWithProperty:(NSString *)propertyStr;
[result removeObject:obj];
}
}];
if (![result containsObject:ID]) {
[result removeAllObjects];
}
}
return result;
}
Expand All @@ -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
8 changes: 4 additions & 4 deletions Source/PSProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions Source/PSProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions SourceEditorPlugin/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<key>CFBundlePackageType</key>
<string>XPC!</string>
<key>CFBundleShortVersionString</key>
<string>1.0.1</string>
<string>1.1.0</string>
<key>CFBundleVersion</key>
<string>3</string>
<string>6</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSExtension</key>
Expand All @@ -36,6 +36,14 @@
<key>XCSourceEditorCommandName</key>
<string>Lazy Getter</string>
</dict>
<dict>
<key>XCSourceEditorCommandClassName</key>
<string>SourceEditorCommand</string>
<key>XCSourceEditorCommandIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER).Setter</string>
<key>XCSourceEditorCommandName</key>
<string>Setter</string>
</dict>
</array>
<key>XCSourceEditorExtensionPrincipalClass</key>
<string>SourceEditorExtension</string>
Expand Down
53 changes: 31 additions & 22 deletions SourceEditorPlugin/SourceEditorCommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit ac794a7

Please sign in to comment.