@@ -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 }
@@ -109,6 +110,12 @@ - (BOOL)tryExpandingSwitchStatement
109110 return NO ;
110111 }
111112
113+ // Check if it's the opening bracket for the switch statement or something else
114+ NSString *remainingText = [textView.string substringWithRange: NSMakeRange (switchRange.location + switchRange.length, openingBracketLocation - switchRange.location - switchRange.length)];
115+ if ([remainingText rangeOfString: @" }" ].location != NSNotFound ) {
116+ return NO ;
117+ }
118+
112119 // Insert the selected autocomplete item
113120 [self .session insertCurrentCompletion ];
114121
@@ -140,15 +147,28 @@ - (BOOL)tryExpandingSwitchStatement
140147 // Generate the items to insert and insert them at the end
141148 NSMutableString *replacementString = [NSMutableString string ];
142149
143- if ([switchContent stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]].length == 0 ) {
144- [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+ }
145165 }
146166
147167 for (IDEIndexSymbol *child in [((IDEIndexContainerSymbol*)symbol).children allObjects ]) {
148168 if ([switchContent rangeOfString: child.displayName].location == NSNotFound ) {
149169 if ([[SCXcodeSwitchExpander sharedSwitchExpander ] isSwift ]) {
150170 NSString *childDisplayName = [self correctEnumConstantIfFromCocoa: [NSString stringWithFormat: @" %@ " ,symbol] symbolName: symbolName cocoaEnumName: child.displayName];
151- [replacementString appendString: [NSString stringWithFormat: @" case .%@ : \n <#statement#>\n break \n\ n" , childDisplayName]];
171+ [replacementString appendString: [NSString stringWithFormat: @" case .%@ : \n <#statement#>\n " , childDisplayName]];
152172 } else {
153173 [replacementString appendString: [NSString stringWithFormat: @" case %@ : {\n <#statement#>\n break;\n }\n " , child.displayName]];
154174 }
@@ -161,16 +181,17 @@ - (BOOL)tryExpandingSwitchStatement
161181 switchContentRange = NSMakeRange (openingBracketLocation + 1 , closingBracketLocation - openingBracketLocation - 1 );
162182 switchContent = [textView.string substringWithRange: switchContentRange];
163183
164- // Insert the default case if necessary
165- if ([switchContent rangeOfString: @" default" ].location == NSNotFound ) {
166- if ([[SCXcodeSwitchExpander sharedSwitchExpander ] isSwift ]) {
167- replacementString = [NSMutableString stringWithString: @" default: \n break\n\n " ];
168- } else {
169- replacementString = [NSMutableString stringWithString: @" default: {\n break;\n }\n " ];
170- }
171- [textView insertText: replacementString replacementRange: NSMakeRange (switchContentRange.location + switchContentRange.length, 0 )];
172- closingBracketLocation += replacementString.length ;
173- }
184+ // // Insert the default case if necessary
185+ // if([switchContent rangeOfString:@"default"].location == NSNotFound) {
186+ // if ([[SCXcodeSwitchExpander sharedSwitchExpander] isSwift]) {
187+ // replacementString = [NSMutableString stringWithString:@"default: \nbreak\n\n"];
188+ // } else {
189+ // replacementString = [NSMutableString stringWithString:@"default: {\nbreak;\n}\n"];
190+ // }
191+ // [textView insertText:replacementString replacementRange:NSMakeRange(switchContentRange.location + switchContentRange.length, 0)];
192+ // closingBracketLocation += replacementString.length;
193+ // }
194+
174195 // Re-indent everything
175196 NSRange reindentRange = NSMakeRange (openingBracketLocation, closingBracketLocation - openingBracketLocation + 2 );
176197 [textView _indentInsertedTextIfNecessaryAtRange: reindentRange];
0 commit comments