Skip to content

Commit

Permalink
Merge pull request #25 from erwinmaza/master
Browse files Browse the repository at this point in the history
Minor code cleanup for Swift
  • Loading branch information
stefanceriu committed Dec 12, 2015
2 parents 2a0f162 + 2c314a7 commit f929c3f
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ - (BOOL)tryExpandingSwitchStatement
}

// See if the current line has a switch statement
NSRange switchRange = [textView.string rangeOfString:@"\\s+switch\\s*\\\(" options:NSRegularExpressionSearch range:NSMakeRange(newLineRange.location, self.session.wordStartLocation - newLineRange.location)];
NSString *regPattern = [[SCXcodeSwitchExpander sharedSwitchExpander] isSwift] ? @"\\s+switch\\s*" : @"\\s+switch\\s*\\\(";
NSRange switchRange = [textView.string rangeOfString:regPattern options:NSRegularExpressionSearch range:NSMakeRange(newLineRange.location, self.session.wordStartLocation - newLineRange.location)];
if(switchRange.location == NSNotFound) {
return NO;
}
Expand Down Expand Up @@ -146,15 +147,28 @@ - (BOOL)tryExpandingSwitchStatement
// Generate the items to insert and insert them at the end
NSMutableString *replacementString = [NSMutableString string];

if([switchContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0) {
[replacementString appendString:@"\n"];
NSString *trimmedContent = [switchContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

if(trimmedContent.length == 0) {
// remove extraneous empty lines if existing content is only whitespace
if (switchContent.length > 0) {
[textView insertText:@"" replacementRange:switchContentRange];
closingBracketLocation -= switchContent.length;
switchContentRange.length = 0;
[replacementString appendString:@"\n"];
} else {
// keep Swift code compact
if (![[SCXcodeSwitchExpander sharedSwitchExpander] isSwift]) {
[replacementString appendString:@"\n"];
}
}
}

for(IDEIndexSymbol *child in [((IDEIndexContainerSymbol*)symbol).children allObjects]) {
if([switchContent rangeOfString:child.displayName].location == NSNotFound) {
if ([[SCXcodeSwitchExpander sharedSwitchExpander] isSwift]) {
NSString *childDisplayName = [self correctEnumConstantIfFromCocoa:[NSString stringWithFormat:@"%@",symbol] symbolName:symbolName cocoaEnumName:child.displayName];
[replacementString appendString:[NSString stringWithFormat:@"case .%@: \n<#statement#>\nbreak\n\n", childDisplayName]];
[replacementString appendString:[NSString stringWithFormat:@"case .%@: \n<#statement#>\n", childDisplayName]];
} else {
[replacementString appendString:[NSString stringWithFormat:@"case %@: {\n<#statement#>\nbreak;\n}\n", child.displayName]];
}
Expand Down

0 comments on commit f929c3f

Please sign in to comment.