1010
1111#import " SCXcodeSwitchExpander.h"
1212
13+ #import " DVTSourceCodeLanguage+SCXcodeSwitchExpander.h"
1314#import " DVTSourceTextView.h"
15+ #import " DVTTextStorage.h"
1416
1517#import " IDEIndex.h"
1618#import " IDEIndexCollection.h"
1719#import " IDEIndexCompletionItem.h"
1820#import " IDEIndexContainerSymbol.h"
21+ #import " IDEWorkspace.h"
1922#import " DVTSourceCodeSymbolKind.h"
23+ #import " DVTSourceTextView.h"
2024
2125#import " DVTTextCompletionSession.h"
2226
2327#import < objc/objc-class.h>
2428
2529// / Returns symbol names from swift style full symbol name (e.g. from `Mirror.DisplayStyle` to `[Mirror, DisplayStyle]`).
26- NSArray <NSString *>* symbolNamesFromFullSymbolName (NSString * fullSymbolName);
30+ NSArray <NSString *>* symbolNamesFromFullSymbolName (NSString * fullSymbolName, DVTSourceCodeLanguageKind language );
2731
2832// / Returns normalized symbol name for -[IDEIndex allSymbolsMatchingName:kind:].
29- NSString * normalizedSymbolName (NSString * symbolName);
33+ NSString * normalizedSymbolName (NSString * symbolName, DVTSourceCodeLanguageKind language );
3034
3135// / Returns a symbol name removed swift generic parameter (e.g. `SomeType<T>` to `SomeType`).
32- NSString * symbolNameRemovingGenericParameter (NSString * symbolName);
36+ NSString * symbolNameRemovingGenericParameter (NSString * symbolName, DVTSourceCodeLanguageKind language );
3337
3438// / Returns a symbol name replaced syntax suggered optional to formal type name type in Swift (e.g. `Int?` to `Optional).
35- NSString * symbolNameReplacingOptionalName (NSString * symbolName);
39+ NSString * symbolNameReplacingOptionalName (NSString * symbolName, DVTSourceCodeLanguageKind language );
3640
37- NSArray <NSString *>* symbolNamesFromFullSymbolName (NSString * fullSymbolName)
41+ NSArray <NSString *>* symbolNamesFromFullSymbolName (NSString * fullSymbolName, DVTSourceCodeLanguageKind language )
3842{
3943 NSMutableArray <NSString *> *names = [[fullSymbolName componentsSeparatedByString: @" ." ] mutableCopy ];
4044
4145 for (NSInteger nameIndex = 0 ; nameIndex != names.count ; ++nameIndex)
4246 {
43- names[nameIndex] = normalizedSymbolName (names[nameIndex]);
47+ names[nameIndex] = normalizedSymbolName (names[nameIndex], language );
4448 }
4549
4650 return [names copy ];
4751}
4852
49- NSString * normalizedSymbolName (NSString * symbolName)
53+ NSString * normalizedSymbolName (NSString * symbolName, DVTSourceCodeLanguageKind language )
5054{
5155 NSString *result = symbolName;
5256
53- result = symbolNameRemovingGenericParameter (result);
54- result = symbolNameReplacingOptionalName (result);
57+ result = symbolNameRemovingGenericParameter (result, language );
58+ result = symbolNameReplacingOptionalName (result, language );
5559
5660 return result;
5761}
5862
59- NSString * symbolNameRemovingGenericParameter (NSString * symbolName)
63+ NSString * symbolNameRemovingGenericParameter (NSString * symbolName, DVTSourceCodeLanguageKind language )
6064{
61- if ([[SCXcodeSwitchExpander sharedSwitchExpander ] isSwift ])
62- {
63- return [symbolName stringByReplacingOccurrencesOfString: @" <[^>]+>$"
64- withString: @" "
65- options: NSRegularExpressionSearch
66- range: NSMakeRange (0 , symbolName.length)];
67- }
68- else
65+ switch (language)
6966 {
70- return symbolName;
67+ case DVTSourceCodeLanguageKindSwift:
68+ return [symbolName stringByReplacingOccurrencesOfString: @" <[^>]+>$"
69+ withString: @" "
70+ options: NSRegularExpressionSearch
71+ range: NSMakeRange (0 , symbolName.length)];
72+
73+ case DVTSourceCodeLanguageKindObjectiveC:
74+ case DVTSourceCodeLanguageKindOthers:
75+ return symbolName;
7176 }
7277}
7378
74- NSString * symbolNameReplacingOptionalName (NSString * symbolName)
79+ NSString * symbolNameReplacingOptionalName (NSString * symbolName, DVTSourceCodeLanguageKind language )
7580{
76- if ([[SCXcodeSwitchExpander sharedSwitchExpander ] isSwift ] )
81+ switch (language )
7782 {
78- if ([symbolName hasSuffix: @" ?" ])
79- {
80- return @" Optional" ;
81- }
82- else if ([symbolName hasSuffix: @" !" ])
83+ case DVTSourceCodeLanguageKindSwift:
8384 {
84- return @" ImplicitlyUnwrappedOptional" ;
85+ if ([symbolName hasSuffix: @" ?" ])
86+ {
87+ return @" Optional" ;
88+ }
89+ else if ([symbolName hasSuffix: @" !" ])
90+ {
91+ return @" ImplicitlyUnwrappedOptional" ;
92+ }
93+ else
94+ {
95+ return symbolName;
96+ }
8597 }
86- else
87- {
98+
99+ case DVTSourceCodeLanguageKindObjectiveC:
100+ case DVTSourceCodeLanguageKindOthers:
88101 return symbolName;
89- }
90- }
91- else
92- {
93- return symbolName;
94102 }
95103}
96104
105+ @interface DVTTextCompletionController (SCXCodeSwitchExpander)
106+
107+ // / Returns the kind of language in current editor.
108+ - (DVTSourceCodeLanguageKind)currentLanguage ;
109+
110+ @end
111+
97112@interface DVTTextCompletionListWindowController (SCXcodeSwitchExpander)
98113
99- - (BOOL )tryExpandingSwitchStatement ;
114+
115+ - (BOOL )tryExpandingSwitchStatementForLanguage : (DVTSourceCodeLanguageKind)language ;
116+
117+ // / Returns symbols specified by `fullSymbolName` in Index.
118+ - (NSArray <IDEIndexSymbol*>*)getSymbolsByFullName : (NSString *)fullSymbolName forLanguage : (DVTSourceCodeLanguageKind)language fromIndex : (IDEIndex*)index ;
100119
101120// / Returns symbols that found in top `collection` by names recursively.
102121- (NSArray <IDEIndexSymbol*>*)findSymbolsWithNames : (NSArray <NSString*>*)names fromCollection : (IDEIndexCollection*)collection ;
@@ -129,13 +148,22 @@ + (void)load
129148
130149- (BOOL )scSwizzledAcceptCurrentCompletion
131150{
132- if ([self .currentSession.listWindowController tryExpandingSwitchStatement ]) {
151+ if ([self .currentSession.listWindowController tryExpandingSwitchStatementForLanguage: self .currentLanguage ]) {
133152 return YES ;
134153 }
135154
136155 return [self scSwizzledAcceptCurrentCompletion ];
137156}
138157
158+ - (DVTSourceCodeLanguageKind)currentLanguage
159+ {
160+ DVTSourceTextView *textView = (DVTSourceTextView *)self.textView ;
161+ DVTTextStorage *textStorage = (DVTTextStorage *)textView.textStorage ;
162+ DVTSourceCodeLanguage *language = textStorage.language ;
163+
164+ return language.kind ;
165+ }
166+
139167@end
140168
141169@implementation DVTTextCompletionListWindowController (SCXcodeSwitchExpander)
@@ -191,17 +219,17 @@ @implementation DVTTextCompletionListWindowController (SCXcodeSwitchExpander)
191219 }
192220}
193221
194- - (NSArray <IDEIndexSymbol*>*)getSymbolsByFullName : (NSString *)fullSymbolName fromIndex : (IDEIndex*)index
222+ - (NSArray <IDEIndexSymbol*>*)getSymbolsByFullName : (NSString *)fullSymbolName forLanguage : (DVTSourceCodeLanguageKind) language fromIndex : (IDEIndex*)index
195223{
196- NSArray <NSString *> *names = symbolNamesFromFullSymbolName (fullSymbolName);
224+ NSArray <NSString *> *names = symbolNamesFromFullSymbolName (fullSymbolName, language );
197225 IDEIndexCollection *collection = [index allSymbolsMatchingName: names.firstObject kind: nil ];
198226
199227 return [self findSymbolsWithNames: names fromCollection: collection];
200228}
201229
202- - (BOOL )tryExpandingSwitchStatement
230+ - (BOOL )tryExpandingSwitchStatementForLanguage : (DVTSourceCodeLanguageKind) language
203231{
204- IDEIndex *index = [[SCXcodeSwitchExpander sharedSwitchExpander ] index ];
232+ IDEIndex *index = [[[ SCXcodeSwitchExpander sharedSwitchExpander ] currentWorkspace ] index ];
205233
206234 if (index == nil ) {
207235 return NO ;
@@ -214,7 +242,7 @@ - (BOOL)tryExpandingSwitchStatement
214242 symbolName = [[symbolName componentsSeparatedByString: @" ::" ] lastObject ]; // Remove C++ namespaces
215243 symbolName = [[symbolName componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet ]] lastObject ]; // Remove enum keyword
216244
217- NSArray <IDEIndexSymbol*> *symbols = [self getSymbolsByFullName: symbolName fromIndex: index];
245+ NSArray <IDEIndexSymbol*> *symbols = [self getSymbolsByFullName: symbolName forLanguage: language fromIndex: index];
218246
219247 // Find the first one of them that is a container
220248 for (IDEIndexSymbol *symbol in symbols) {
@@ -244,8 +272,21 @@ - (BOOL)tryExpandingSwitchStatement
244272 }
245273
246274 // See if the current line has a switch statement
247- NSString *regPattern = [[SCXcodeSwitchExpander sharedSwitchExpander ] isSwift ] ? @" \\ s+switch\\ s*" : @" \\ s+switch\\ s*\\ \( " ;
248- NSRange switchRange = [textView.string rangeOfString: regPattern options: NSRegularExpressionSearch range: NSMakeRange (newLineRange.location, self .session.wordStartLocation - newLineRange.location)];
275+ NSString *regPattern;
276+
277+ switch (language)
278+ {
279+ case DVTSourceCodeLanguageKindObjectiveC:
280+ case DVTSourceCodeLanguageKindOthers:
281+ regPattern = @" \\ s+switch\\ s*\\ \( " ;
282+ break ;
283+
284+ case DVTSourceCodeLanguageKindSwift:
285+ regPattern = @" \\ s+switch\\ s*" ;
286+ break ;
287+ }
288+
289+ NSRange switchRange = [textView.string rangeOfString: regPattern options: NSRegularExpressionSearch range: NSMakeRange (newLineRange.location, self .session.wordStartLocation - newLineRange.location)];
249290 if (switchRange.location == NSNotFound ) {
250291 return NO ;
251292 }
@@ -276,10 +317,16 @@ - (BOOL)tryExpandingSwitchStatement
276317
277318 NSRange defaultAutocompletionRange;
278319 // Get rid of the default autocompletion if necessary
279- if ([[SCXcodeSwitchExpander sharedSwitchExpander ] isSwift ]) {
280- defaultAutocompletionRange = [textView.string rangeOfString: @" \\ s*case .<#constant#>:\\ s*<#statements#>\\ s*break\\ s*default:\\ s*break\\ s*" options: NSRegularExpressionSearch range: NSMakeRange (openingBracketLocation, closingBracketLocation - openingBracketLocation)];
281- } else {
282- defaultAutocompletionRange = [textView.string rangeOfString: @" \\ s*case <#constant#>:\\ s*<#statements#>\\ s*break;\\ s*default:\\ s*break;\\ s*" options: NSRegularExpressionSearch range: NSMakeRange (openingBracketLocation, closingBracketLocation - openingBracketLocation)];
320+ switch (language) {
321+
322+ case DVTSourceCodeLanguageKindSwift:
323+ defaultAutocompletionRange = [textView.string rangeOfString: @" \\ s*case .<#constant#>:\\ s*<#statements#>\\ s*break\\ s*default:\\ s*break\\ s*" options: NSRegularExpressionSearch range: NSMakeRange (openingBracketLocation, closingBracketLocation - openingBracketLocation)];
324+ break ;
325+
326+ case DVTSourceCodeLanguageKindObjectiveC:
327+ case DVTSourceCodeLanguageKindOthers:
328+ defaultAutocompletionRange = [textView.string rangeOfString: @" \\ s*case <#constant#>:\\ s*<#statements#>\\ s*break;\\ s*default:\\ s*break;\\ s*" options: NSRegularExpressionSearch range: NSMakeRange (openingBracketLocation, closingBracketLocation - openingBracketLocation)];
329+ break ;
283330 }
284331
285332 if (defaultAutocompletionRange.location != NSNotFound ) {
@@ -304,7 +351,7 @@ - (BOOL)tryExpandingSwitchStatement
304351 [replacementString appendString: @" \n " ];
305352 } else {
306353 // keep Swift code compact
307- if (![[SCXcodeSwitchExpander sharedSwitchExpander ] isSwift ] ) {
354+ if (language != DVTSourceCodeLanguageKindSwift ) {
308355 [replacementString appendString: @" \n " ];
309356 }
310357 }
@@ -319,11 +366,19 @@ - (BOOL)tryExpandingSwitchStatement
319366 }
320367
321368 if ([switchContent rangeOfString: child.displayName].location == NSNotFound ) {
322- if ([[SCXcodeSwitchExpander sharedSwitchExpander ] isSwift ]) {
323- NSString *childDisplayName = [self correctEnumConstantIfFromCocoa: [NSString stringWithFormat: @" %@ " ,symbol] symbolName: symbolName cocoaEnumName: child.displayName];
324- [replacementString appendString: [NSString stringWithFormat: @" case .%@ : \n <#statement#>\n " , childDisplayName]];
325- } else {
326- [replacementString appendString: [NSString stringWithFormat: @" case %@ : {\n <#statement#>\n break;\n }\n " , child.displayName]];
369+ switch (language) {
370+
371+ case DVTSourceCodeLanguageKindSwift:
372+ {
373+ NSString *childDisplayName = [self correctEnumConstantIfFromCocoa: [NSString stringWithFormat: @" %@ " ,symbol] symbolName: symbolName cocoaEnumName: child.displayName];
374+ [replacementString appendString: [NSString stringWithFormat: @" case .%@ : \n <#statement#>\n " , childDisplayName]];
375+ break ;
376+ }
377+
378+ case DVTSourceCodeLanguageKindObjectiveC:
379+ case DVTSourceCodeLanguageKindOthers:
380+ [replacementString appendString: [NSString stringWithFormat: @" case %@ : {\n <#statement#>\n break;\n }\n " , child.displayName]];
381+ break ;
327382 }
328383 }
329384 }
0 commit comments