Skip to content

Commit ac794a7

Browse files
committed
[Update] Add Setter & filter basic data type
1 parent 807ab24 commit ac794a7

File tree

7 files changed

+89
-33
lines changed

7 files changed

+89
-33
lines changed

CodeGenerator/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.0.1</string>
20+
<string>1.1.0</string>
2121
<key>CFBundleVersion</key>
22-
<string>3</string>
22+
<string>6</string>
2323
<key>LSMinimumSystemVersion</key>
2424
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
2525
<key>NSHumanReadableCopyright</key>

Source/AccessCodeGenerator.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,19 @@
1414
/**
1515
pattern matching the string, recognize Objective-C propertyies, generate their lazy getter code and return.
1616
17+
basic data type and id will be ignored.
18+
1719
@param string a string
1820
@return lazy getter code
1921
*/
2022
+ (NSArray<NSString *> *)lazyGetterForString:(NSString *)string;
2123

24+
/**
25+
pattern matching the string, recognize Objective-C propertyies, generate their setter code and return.
26+
27+
@param string a string
28+
@return setter code
29+
*/
30+
+ (NSArray<NSString *> *)setterForString:(NSString *)string;
31+
2232
@end

Source/AccessCodeGenerator.m

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ @implementation AccessCodeGenerator
1919
return result;
2020
}
2121

22+
+ (NSArray<NSString *> *)setterForString:(NSString *)string {
23+
NSArray<PSProperty *> *props = [self propertyWithContent:string];
24+
NSMutableArray *result = [NSMutableArray array];
25+
for (PSProperty *model in props) {
26+
[result addObject:[self setterWithPSProperty:model]];
27+
}
28+
return result;
29+
}
30+
2231
+ (NSArray<PSProperty *> *)propertyWithContent:(NSString *)content
2332
{
2433
NSMutableArray *result = [NSMutableArray array];
@@ -48,6 +57,7 @@ @implementation AccessCodeGenerator
4857
proMoel.keywords = keywords;
4958
proMoel.dataType = [[dataTypeAndName subarrayWithRange:NSMakeRange(0, dataTypeAndName.count - 1)] componentsJoinedByString:@" *"];
5059
proMoel.name = [dataTypeAndName lastObject];
60+
proMoel.isObjectType = [propertyString containsString:@"*"];
5161
[result addObject:proMoel];
5262
}
5363
}
@@ -70,6 +80,7 @@ @implementation AccessCodeGenerator
7080
NSString *resultString = [[propertyStr1 substringWithRange:firstHalfRange] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
7181
resultString = [resultString stringByReplacingOccurrencesOfString:@"(" withString:@""];
7282
resultString = [resultString stringByReplacingOccurrencesOfString:@")" withString:@""];
83+
resultString = [resultString stringByReplacingOccurrencesOfString:@" " withString:@""];
7384
return [resultString componentsSeparatedByString:@","];
7485
}
7586
}
@@ -111,9 +122,6 @@ + (NSMutableArray *)propertyTypeAndNameWithProperty:(NSString *)propertyStr;
111122
[result removeObject:obj];
112123
}
113124
}];
114-
if (![result containsObject:ID]) {
115-
[result removeAllObjects];
116-
}
117125
}
118126
return result;
119127
}
@@ -138,5 +146,24 @@ + (NSString *)getterWithPSProperty:(PSProperty *)model
138146
return lazyGetter;
139147
}
140148

149+
+ (NSString *)setterWithPSProperty:(PSProperty *)model {
150+
NSString *setter = @"";
151+
if (model.isObjectType) {
152+
setter = [NSString stringWithFormat:@"\n- (void)set%@:(%@ *)%@ {\n _%@ = %@;\n}",
153+
[model.name capitalizedString],
154+
model.dataType,
155+
model.name,
156+
model.name,
157+
model.name];
158+
} else {
159+
setter = [NSString stringWithFormat:@"\n- (void)set%@:(%@)%@ {\n _%@ = %@;\n}",
160+
[model.name capitalizedString],
161+
model.dataType,
162+
model.name,
163+
model.name,
164+
model.name];
165+
}
166+
return setter;
167+
}
141168

142169
@end

Source/PSProperty.h

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

1111
static NSString *const ASSIGN = @"assign";
1212
static NSString *const WEAK = @"weak";
13-
1413
static NSString *const ID = @"id";
15-
1614
static NSString *const IB_OUTLET = @"IBOutlet";
15+
//static NSString *const BASIC_DATA_TYPE = @"NSInteger,NSUInteger,char,int,float,double,long,short,signed,unsigned,BOOL,Bool,bool,Boolean";
1716

1817

1918
@interface PSProperty : NSObject
@@ -24,9 +23,10 @@ static NSString *const IB_OUTLET = @"IBOutlet";
2423
/**
2524
* 数据类型
2625
*/
27-
@property (nonatomic,strong) NSString * dataType;
26+
@property (nonatomic,strong) NSString *dataType;
2827
/**
2928
* 属性名称
3029
*/
31-
@property (nonatomic,strong) NSString * name;
30+
@property (nonatomic,strong) NSString *name;
31+
@property (nonatomic, assign) BOOL isObjectType;/**< 是不是带 * 的类型*/
3232
@end

Source/PSProperty.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ @interface PSProperty ()
1313
@property (nonatomic, strong) NSString *string;
1414
@property (nonatomic, strong) NSArray *array;
1515
@property (nonatomic, strong) PSProperty *node;
16+
@property (nonatomic, assign) NSInteger haha;
17+
@property (nonatomic, strong) id idnAme;
1618

1719
@end
1820

SourceEditorPlugin/Info.plist

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<key>CFBundlePackageType</key>
1818
<string>XPC!</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.0.1</string>
20+
<string>1.1.0</string>
2121
<key>CFBundleVersion</key>
22-
<string>3</string>
22+
<string>6</string>
2323
<key>LSMinimumSystemVersion</key>
2424
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
2525
<key>NSExtension</key>
@@ -36,6 +36,14 @@
3636
<key>XCSourceEditorCommandName</key>
3737
<string>Lazy Getter</string>
3838
</dict>
39+
<dict>
40+
<key>XCSourceEditorCommandClassName</key>
41+
<string>SourceEditorCommand</string>
42+
<key>XCSourceEditorCommandIdentifier</key>
43+
<string>$(PRODUCT_BUNDLE_IDENTIFIER).Setter</string>
44+
<key>XCSourceEditorCommandName</key>
45+
<string>Setter</string>
46+
</dict>
3947
</array>
4048
<key>XCSourceEditorExtensionPrincipalClass</key>
4149
<string>SourceEditorExtension</string>

SourceEditorPlugin/SourceEditorCommand.m

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)