Skip to content

--Fix Bug With Swift Support #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EC/EC.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>2WQE6AU5PD.group.com.music4kid.easycode</string>
<string>$(TeamIdentifierPrefix)group.com.sito.easycode</string>
</array>
</dict>
</plist>
2 changes: 0 additions & 2 deletions EC/ECFileParser/ECFileParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ - (FSElementCache*)getImpElementByContent:(NSString*)content
}

- (FSElementCache*)getElementByContent:(NSString*)content selection:(NSRange)range {

FSElementCache* element = nil;

FSImpProcessor* p = [FSImpProcessor new];
NSArray* elementsInFile = [p createElements:content];
for (FSElementCache* e in elementsInFile) {
Expand Down
2 changes: 1 addition & 1 deletion EC/ECFileParser/FSElementPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@property (nonatomic, strong) NSMutableDictionary* elementMap;

- (void)parseElementFromProjectFile:(NSString*)filePath complete:(dispatch_block_t)completeBlock;
//- (void)parseElementFromProjectFile:(NSString*)filePath complete:(dispatch_block_t)completeBlock;
- (void)parseHeaderFile:(NSString*)filePath;

- (FSElementCache*)getElementFromCache:(NSString*)elementName;
Expand Down
3 changes: 2 additions & 1 deletion EC/ECMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
//

#import <Foundation/Foundation.h>
#import "ECSnippetEntry.h"

@interface ECMapping : NSObject
- (NSDictionary*)provideMapping;
+ (NSArray*)defaultEntries;
@end

2 changes: 1 addition & 1 deletion EC/ECMapping.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import "ECMapping.h"

@implementation ECMapping
- (NSDictionary*)provideMapping {
+ (NSArray*)defaultEntries {
return nil;
}
@end
Expand Down
4 changes: 1 addition & 3 deletions EC/ECMappingForObjectiveC.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
#import "ECMapping.h"

@interface ECMappingForObjectiveC : ECMapping

- (NSDictionary*)provideMapping;

+ (NSArray*)defaultEntries;
@end
14 changes: 10 additions & 4 deletions EC/ECMappingForObjectiveC.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

#import "ECMappingForObjectiveC.h"
#import "OCMapping.h"
#import "ECSnippetEntry.h"

@implementation ECMappingForObjectiveC

- (NSDictionary*)provideMapping {
+ (NSArray*)defaultEntries {
NSDictionary* mapping = @{
//UIViewController
KeyOC_UIViewController_VDL:KeyOC_UIViewController_VDL_Value,
Expand Down Expand Up @@ -63,9 +64,14 @@ - (NSDictionary*)provideMapping {
KeyOC_Template_Label:KeyOC_Template_Label_Value,
KeyOC_Template_ImageView:KeyOC_Template_ImageView_Value,

}.mutableCopy;

return mapping;
};
NSMutableArray* snippetList = [NSMutableArray arrayWithCapacity:mapping.count];
[mapping enumerateKeysAndObjectsUsingBlock:^(NSString* _Nonnull key, NSString* _Nonnull code, BOOL * _Nonnull stop) {
ECSnippetEntry* snippet = [ECSnippetEntry snippetWithKey:key code:code];
[snippetList addObject:snippet];
}];
NSArray* snippets = [snippetList copy];
return snippets;
}


Expand Down
4 changes: 1 addition & 3 deletions EC/ECMappingForSwift.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
#import "ECMapping.h"

@interface ECMappingForSwift : ECMapping

- (NSDictionary*)provideMapping;

+ (NSArray*)defaultEntries;
@end
64 changes: 59 additions & 5 deletions EC/ECMappingForSwift.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,70 @@

#import "ECMappingForSwift.h"
#import "SwiftMapping.h"
#import "ECSnippetEntry.h"

@implementation ECMappingForSwift

- (NSDictionary*)provideMapping {
+ (NSArray*)defaultEntries {
NSDictionary* mapping = @{

//UIViewController
KeySwift_UIViewController_VDL:KeySwift_UIViewController_VDL_Value,
KeySwift_UIViewController_VWA:KeySwift_UIViewController_VWA_Value,
KeySwift_UIViewController_VDA:KeySwift_UIViewController_VDA_Value,
KeySwift_UIViewController_VWD:KeySwift_UIViewController_VWD_Value,
KeySwift_UIViewController_VDD:KeySwift_UIViewController_VDD_Value,
KeySwift_UIViewController_DRM:KeySwift_UIViewController_DRM_Value,
KeySwift_UIViewController_SIO:KeySwift_UIViewController_SIO_Value,
KeySwift_UIViewController_PIO:KeySwift_UIViewController_PIO_Value,

}.mutableCopy;

return mapping;
//UIView
KeySwift_UIView_HTW:KeySwift_UIView_HTW_Value,
KeySwift_UIView_PIE:KeySwift_UIView_PIE_Value,
KeySwift_UIView_CPT:KeySwift_UIView_CPT_Value,
KeySwift_UIView_CPF:KeySwift_UIView_CPF_Value,
KeySwift_UIView_CRP:KeySwift_UIView_CRP_Value,
KeySwift_UIView_CRF:KeySwift_UIView_CRF_Value,
KeySwift_UIView_DR:KeySwift_UIView_DR_Value,

//UIApplication
KeySwift_UIApplication_DRU:KeySwift_UIApplication_DRU_Value,
KeySwift_UIApplication_DRF:KeySwift_UIApplication_DRF_Value,
KeySwift_UIApplication_DRT:KeySwift_UIApplication_DRT_Value,
KeySwift_UIApplication_DRR:KeySwift_UIApplication_DRR_Value,
KeySwift_UIApplication_DRL:KeySwift_UIApplication_DRL_Value,

//GCD
KeySwift_GCD_DAFM:KeySwift_GCD_DAFM_Value,
KeySwift_GCD_DASM:KeySwift_GCD_DASM_Value,
KeySwift_GCD_DAFG:KeySwift_GCD_DAFG_Value,
KeySwift_GCD_DASG:KeySwift_GCD_DASG_Value,

//MISC
KeySwift_MISC_DEL:KeySwift_MISC_DEL_Value,
KeySwift_MISC_V:KeySwift_MISC_V_Value,
KeySwift_MISC_C:KeySwift_MISC_C_Value,
KeySwift_MISC_P:KeySwift_MISC_P_Value,
KeySwift_MISC_W:KeySwift_MISC_W_Value,
KeySwift_MISC_N:KeySwift_MISC_N_Value,
KeySwift_MISC_U:KeySwift_MISC_U_Value,
KeySwift_MISC_F:KeySwift_MISC_F_Value,
KeySwift_MISC_M:KeySwift_MISC_M_Value,
KeySwift_MISC_IMG:KeySwift_MISC_IMG_Value,
KeySwift_MISC_BUN:KeySwift_MISC_BUN_Value,

//Template
KeySwift_Template_Button:KeySwift_Template_Button_Value,
KeySwift_Template_Label:KeySwift_Template_Label_Value,
KeySwift_Template_ImageView:KeySwift_Template_ImageView_Value,

};
NSMutableArray* snippetList = [NSMutableArray arrayWithCapacity:mapping.count];
[mapping enumerateKeysAndObjectsUsingBlock:^(NSString* _Nonnull key, NSString* _Nonnull code, BOOL * _Nonnull stop) {
ECSnippetEntry* snippet = [ECSnippetEntry snippetWithKey:key code:code];
[snippetList addObject:snippet];
}];
NSArray* snippets = [snippetList copy];
return snippets;
}


Expand Down
128 changes: 35 additions & 93 deletions EC/ECMappingHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,41 @@
//

#import "ECMappingHelper.h"
#import "ECMappingForObjectiveC.h"
#import "ECMappingForSwift.h"
#import "NSFileManager+Additions.h"
#import "ESharedUserDefault.h"
#import "ECSnippet.h"
#import "ECSnippetHelper.h"

@interface ECMappingHelper ()
@property (nonatomic, strong) NSMutableDictionary* mappingOC;
@property (nonatomic, strong) NSMutableDictionary* mappingSwift;
@property (nonatomic, strong) ECSnippet* curSnippets;
@end

@implementation ECMappingHelper

+ (instancetype)sharedInstance
{
+ (instancetype)sharedInstance {
static ECMappingHelper* instance = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [ECMappingHelper new];
instance = [[ECMappingHelper alloc] init];
});

return instance;
}

- (instancetype)init
{
self = [super init];
if (self) {

}
return self;
}

- (void)checkForDuplicatedKeys:(NSDictionary*)mapping
{
//check for duplicated keys
NSArray* keys = mapping.allKeys;
NSMutableDictionary* dic = @{}.mutableCopy;
for (NSString* key in keys) {
if ([dic objectForKey:keys]) {
NSLog(@"detect duplicated keys!");
}
else
{
dic[key] = key;
}
- (void)reloadSnippetBySourceType:(ECSourceType)sourceType {
NSString* dirname = [ECSnippetHelper directoryForSourceType:sourceType];
NSString* versionKey = [NSString stringWithFormat:kVersionFormat,dirname];
NSNumber* latestVer = [ESharedUserDefault objectForKey:versionKey];

if ([_curSnippets.version isEqualToNumber:latestVer] == NO) { //if versions are not equal,reload from UserDefaults.
NSData* data = [ESharedUserDefault dataForKey:dirname];
_curSnippets = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
}

- (BOOL)handleInvocation:(XCSourceEditorCommandInvocation *)invocation {

//read from NSUserDefault each time
[self clearMapping];
ECSourceType sourceType = [ECSnippetHelper sourceTypeForContentUTI:invocation.buffer.contentUTI];
[self reloadSnippetBySourceType:sourceType];

XCSourceTextRange *selection = invocation.buffer.selections.firstObject;
NSMutableArray* lines = invocation.buffer.lines;
Expand All @@ -67,12 +50,11 @@ - (BOOL)handleInvocation:(XCSourceEditorCommandInvocation *)invocation {

int matchedCount = 0;

if (index > lines.count-1) {
if (index > lines.count - 1) {
return false;
}

NSString* originalLine = lines[index];

int matchLength = 8;//max match length for shortcut
while (matchLength >= 1) {

Expand All @@ -81,19 +63,14 @@ - (BOOL)handleInvocation:(XCSourceEditorCommandInvocation *)invocation {
NSRange targetRange = NSMakeRange(column-matchLength, matchLength);
NSString* lastNStr = [originalLine substringWithRange:targetRange];

BOOL isOC = true;
if ([invocation.buffer.contentUTI isEqualToString:@"public.swift-source"]) {
isOC = false;
}
NSString* matchedVal = [self getMatchedCode:lastNStr isOC:isOC];
NSString* matchedVal = [self matchedCode:lastNStr forSourceType:sourceType];

if (matchedVal.length > 0) {

int numberOfSpaceIndent = (int)[originalLine rangeOfString:lastNStr].location;
NSString* indentStr = @"";
while (numberOfSpaceIndent>0) {
indentStr = [indentStr stringByAppendingString:@" "];
numberOfSpaceIndent --;
numberOfSpaceIndent--;
}

NSArray* linesToInsert = [self convertToLines:matchedVal];
Expand All @@ -105,7 +82,7 @@ - (BOOL)handleInvocation:(XCSourceEditorCommandInvocation *)invocation {
range:targetRange];

//insert the rest
for (int i = 1; i < linesToInsert.count; i ++) {
for (int i = 1; i < linesToInsert.count; i++) {
NSString* lineToInsert = linesToInsert[i];
//indent
lineToInsert = [NSString stringWithFormat:@"%@%@", indentStr, lineToInsert];
Expand All @@ -118,11 +95,10 @@ - (BOOL)handleInvocation:(XCSourceEditorCommandInvocation *)invocation {
}
}

matchLength --;
matchLength--;

}


//adjust selection
if (matchedCount > 0) {
selection.start = XCSourceTextPositionMake(selection.start.line, selection.start.column-matchedCount);
Expand All @@ -132,62 +108,28 @@ - (BOOL)handleInvocation:(XCSourceEditorCommandInvocation *)invocation {
}


- (NSString*)getMatchedCode:(NSString*)abbr isOC:(BOOL)isOC
{
- (NSString*)matchedCode:(NSString*)abbr forSourceType:(ECSourceType)type {
//need to detect swift or oc
NSDictionary* mappingDic = nil;

if (isOC) {
mappingDic = self.mappingOC;
}
else
{
mappingDic = self.mappingSwift;
}
NSArray<ECSnippetEntry*>* entries = _curSnippets.entries;
__block NSInteger index = NSNotFound;
[_curSnippets.entries enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(ECSnippetEntry * _Nonnull entry, NSUInteger idx, BOOL * _Nonnull stop) {
if ([entry.key hasPrefix:abbr] || [entry.key isEqual:abbr]) {
index = idx;
*stop = YES;
}
}];

if ([mappingDic objectForKey:abbr] != nil) {
return [mappingDic objectForKey:abbr];
if (index != NSNotFound) {
NSString* matchedCode = [entries[index] code];
return matchedCode;
}

return nil;
}


- (NSArray*)convertToLines:(NSString*)codeStr
{
NSMutableArray* lines = @[].mutableCopy;

- (NSArray*)convertToLines:(NSString*)codeStr {
NSArray* arr = [codeStr componentsSeparatedByString:@"\n"];

for (NSString* line in arr) {
[lines addObject:line];
}

return lines;
}

- (NSMutableDictionary*)mappingOC
{
if (_mappingOC == nil) {
_mappingOC = [_UD readMappingForOC].mutableCopy;
}
return _mappingOC;
}

- (NSMutableDictionary*)mappingSwift
{
if (_mappingSwift == nil) {
_mappingSwift = [_UD readMappingForSwift].mutableCopy;
}
return _mappingSwift;
}

- (void)clearMapping
{
self.mappingOC = nil;
self.mappingSwift = nil;

[_UD clearMapping];
return arr;
}

@end
Loading