Skip to content

Commit f929c3f

Browse files
committed
Merge pull request #25 from erwinmaza/master
Minor code cleanup for Swift
2 parents 2a0f162 + 2c314a7 commit f929c3f

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

SCXcodeSwitchExpander/DVTTextCompletionController+SCXcodeSwitchExpander.m

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ - (BOOL)tryExpandingSwitchStatement
9898
}
9999

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

149-
if([switchContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length == 0) {
150-
[replacementString appendString:@"\n"];
150+
NSString *trimmedContent = [switchContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
151+
152+
if(trimmedContent.length == 0) {
153+
// remove extraneous empty lines if existing content is only whitespace
154+
if (switchContent.length > 0) {
155+
[textView insertText:@"" replacementRange:switchContentRange];
156+
closingBracketLocation -= switchContent.length;
157+
switchContentRange.length = 0;
158+
[replacementString appendString:@"\n"];
159+
} else {
160+
// keep Swift code compact
161+
if (![[SCXcodeSwitchExpander sharedSwitchExpander] isSwift]) {
162+
[replacementString appendString:@"\n"];
163+
}
164+
}
151165
}
152166

153167
for(IDEIndexSymbol *child in [((IDEIndexContainerSymbol*)symbol).children allObjects]) {
154168
if([switchContent rangeOfString:child.displayName].location == NSNotFound) {
155169
if ([[SCXcodeSwitchExpander sharedSwitchExpander] isSwift]) {
156170
NSString *childDisplayName = [self correctEnumConstantIfFromCocoa:[NSString stringWithFormat:@"%@",symbol] symbolName:symbolName cocoaEnumName:child.displayName];
157-
[replacementString appendString:[NSString stringWithFormat:@"case .%@: \n<#statement#>\nbreak\n\n", childDisplayName]];
171+
[replacementString appendString:[NSString stringWithFormat:@"case .%@: \n<#statement#>\n", childDisplayName]];
158172
} else {
159173
[replacementString appendString:[NSString stringWithFormat:@"case %@: {\n<#statement#>\nbreak;\n}\n", child.displayName]];
160174
}

0 commit comments

Comments
 (0)