Skip to content

Commit 1e9a33b

Browse files
committed
adding support for Swift
1 parent 4de0299 commit 1e9a33b

File tree

8 files changed

+153
-41
lines changed

8 files changed

+153
-41
lines changed

SCXcodeSwitchExpander.xcodeproj/project.pbxproj

100644100755
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@
1616
/* End PBXBuildFile section */
1717

1818
/* Begin PBXFileReference section */
19-
18142E1E18E89497006BD329 /* DVTCompletingTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTCompletingTextView.h; sourceTree = "<group>"; };
20-
18142E1F18E89497006BD329 /* DVTSourceCodeSymbolKind.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTSourceCodeSymbolKind.h; sourceTree = "<group>"; };
21-
18142E2018E89497006BD329 /* DVTSourceTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTSourceTextView.h; sourceTree = "<group>"; };
22-
18142E2118E89497006BD329 /* DVTTextCompletionController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTTextCompletionController.h; sourceTree = "<group>"; };
23-
18142E2218E89497006BD329 /* DVTTextCompletionListWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTTextCompletionListWindowController.h; sourceTree = "<group>"; };
24-
18142E2318E89497006BD329 /* DVTTextCompletionSession.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTTextCompletionSession.h; sourceTree = "<group>"; };
25-
18142E2418E89497006BD329 /* IDEIndex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndex.h; sourceTree = "<group>"; };
26-
18142E2518E89497006BD329 /* IDEIndexCollection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndexCollection.h; sourceTree = "<group>"; };
27-
18142E2618E89497006BD329 /* IDEIndexCompletionItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndexCompletionItem.h; sourceTree = "<group>"; };
28-
18142E2718E89497006BD329 /* IDEIndexContainerSymbol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndexContainerSymbol.h; sourceTree = "<group>"; };
29-
18142E2818E89497006BD329 /* IDEIndexSymbol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndexSymbol.h; sourceTree = "<group>"; };
19+
0144D9921B06F9F00012D7A2 /* DVTCompletingTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTCompletingTextView.h; sourceTree = "<group>"; };
20+
0144D9971B06F9F00012D7A2 /* DVTSourceCodeSymbolKind.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTSourceCodeSymbolKind.h; sourceTree = "<group>"; };
21+
0144D9991B06F9F00012D7A2 /* DVTSourceTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTSourceTextView.h; sourceTree = "<group>"; };
22+
0144D99A1B06F9F00012D7A2 /* DVTTextCompletionController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTTextCompletionController.h; sourceTree = "<group>"; };
23+
0144D99B1B06F9F00012D7A2 /* DVTTextCompletionListWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTTextCompletionListWindowController.h; sourceTree = "<group>"; };
24+
0144D99C1B06F9F00012D7A2 /* DVTTextCompletionSession.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTTextCompletionSession.h; sourceTree = "<group>"; };
25+
0144D99F1B06F9F00012D7A2 /* IDEEditor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEEditor.h; sourceTree = "<group>"; };
26+
0144D9A01B06F9F00012D7A2 /* IDEIndex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndex.h; sourceTree = "<group>"; };
27+
0144D9A11B06F9F00012D7A2 /* IDEIndexCollection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndexCollection.h; sourceTree = "<group>"; };
28+
0144D9A21B06F9F00012D7A2 /* IDEIndexCompletionItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndexCompletionItem.h; sourceTree = "<group>"; };
29+
0144D9A31B06F9F00012D7A2 /* IDEIndexContainerSymbol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndexContainerSymbol.h; sourceTree = "<group>"; };
30+
0144D9A61B06F9F00012D7A2 /* IDEIndexSymbol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEIndexSymbol.h; sourceTree = "<group>"; };
31+
0144D9B11B06FBD30012D7A2 /* IDEFileTextSettings.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IDEFileTextSettings.h; sourceTree = "<group>"; };
3032
1821147E18DDABF8005F1C26 /* IDEKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IDEKit.framework; path = ../../../../Applications/Xcode.app/Contents/Frameworks/IDEKit.framework; sourceTree = "<group>"; };
3133
1830652718D268E50001DC8E /* DVTFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DVTFoundation.framework; path = ../../../../Applications/Xcode.app/Contents/SharedFrameworks/DVTFoundation.framework; sourceTree = "<group>"; };
3234
1830652918D268FE0001DC8E /* DVTKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DVTKit.framework; path = ../../../../Applications/Xcode.app/Contents/SharedFrameworks/DVTKit.framework; sourceTree = "<group>"; };
35+
185293151B085C4200A2E2C7 /* DVTViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DVTViewController.h; sourceTree = "<group>"; };
3336
18990B1F18D25239007A8756 /* SCXcodeSwitchExpander.xcplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SCXcodeSwitchExpander.xcplugin; sourceTree = BUILT_PRODUCTS_DIR; };
3437
18990B2A18D25239007A8756 /* SCXcodeSwitchExpander-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "SCXcodeSwitchExpander-Info.plist"; sourceTree = "<group>"; };
3538
18990B2E18D25239007A8756 /* SCXcodeSwitchExpander-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SCXcodeSwitchExpander-Prefix.pch"; sourceTree = "<group>"; };
@@ -58,17 +61,20 @@
5861
18142E1D18E89497006BD329 /* Xcode Headers */ = {
5962
isa = PBXGroup;
6063
children = (
61-
18142E1E18E89497006BD329 /* DVTCompletingTextView.h */,
62-
18142E1F18E89497006BD329 /* DVTSourceCodeSymbolKind.h */,
63-
18142E2018E89497006BD329 /* DVTSourceTextView.h */,
64-
18142E2118E89497006BD329 /* DVTTextCompletionController.h */,
65-
18142E2218E89497006BD329 /* DVTTextCompletionListWindowController.h */,
66-
18142E2318E89497006BD329 /* DVTTextCompletionSession.h */,
67-
18142E2418E89497006BD329 /* IDEIndex.h */,
68-
18142E2518E89497006BD329 /* IDEIndexCollection.h */,
69-
18142E2618E89497006BD329 /* IDEIndexCompletionItem.h */,
70-
18142E2718E89497006BD329 /* IDEIndexContainerSymbol.h */,
71-
18142E2818E89497006BD329 /* IDEIndexSymbol.h */,
64+
0144D9921B06F9F00012D7A2 /* DVTCompletingTextView.h */,
65+
0144D9971B06F9F00012D7A2 /* DVTSourceCodeSymbolKind.h */,
66+
0144D9991B06F9F00012D7A2 /* DVTSourceTextView.h */,
67+
0144D99A1B06F9F00012D7A2 /* DVTTextCompletionController.h */,
68+
0144D99B1B06F9F00012D7A2 /* DVTTextCompletionListWindowController.h */,
69+
0144D99C1B06F9F00012D7A2 /* DVTTextCompletionSession.h */,
70+
185293151B085C4200A2E2C7 /* DVTViewController.h */,
71+
0144D99F1B06F9F00012D7A2 /* IDEEditor.h */,
72+
0144D9B11B06FBD30012D7A2 /* IDEFileTextSettings.h */,
73+
0144D9A01B06F9F00012D7A2 /* IDEIndex.h */,
74+
0144D9A11B06F9F00012D7A2 /* IDEIndexCollection.h */,
75+
0144D9A21B06F9F00012D7A2 /* IDEIndexCompletionItem.h */,
76+
0144D9A31B06F9F00012D7A2 /* IDEIndexContainerSymbol.h */,
77+
0144D9A61B06F9F00012D7A2 /* IDEIndexSymbol.h */,
7278
);
7379
path = "Xcode Headers";
7480
sourceTree = "<group>";

SCXcodeSwitchExpander/DVTTextCompletionController+SCXcodeSwitchExpander.m

100644100755
Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ - (BOOL)tryExpandingSwitchStatement
7474
for(IDEIndexSymbol *symbol in collection.allObjects) {
7575

7676
DVTSourceCodeSymbolKind *symbolKind = symbol.symbolKind;
77+
78+
BOOL isSymbolKindEnum = NO;
79+
for(DVTSourceCodeSymbolKind *conformingSymbol in symbolKind.allConformingSymbolKinds) {
80+
isSymbolKindEnum = [self isSymbolKindEnum:conformingSymbol];
81+
}
82+
83+
if (!isSymbolKindEnum) {
84+
return NO;
85+
}
7786

7887
if(symbolKind.isContainer) {
7988

@@ -112,8 +121,14 @@ - (BOOL)tryExpandingSwitchStatement
112121
return NO;
113122
}
114123

124+
NSRange defaultAutocompletionRange;
115125
// Get rid of the default autocompletion if necessary
116-
NSRange defaultAutocompletionRange = [textView.string rangeOfString:@"\\s*case <#constant#>:\\s*<#statements#>\\s*break;\\s*default:\\s*break;\\s*" options:NSRegularExpressionSearch range:NSMakeRange(openingBracketLocation, closingBracketLocation - openingBracketLocation)];
126+
if ([[SCXcodeSwitchExpander sharedSwitchExpander] isSwift]) {
127+
defaultAutocompletionRange = [textView.string rangeOfString:@"\\s*case .<#constant#>:\\s*<#statements#>\\s*break\\s*default:\\s*break\\s*" options:NSRegularExpressionSearch range:NSMakeRange(openingBracketLocation, closingBracketLocation - openingBracketLocation)];
128+
} else {
129+
defaultAutocompletionRange = [textView.string rangeOfString:@"\\s*case <#constant#>:\\s*<#statements#>\\s*break;\\s*default:\\s*break;\\s*" options:NSRegularExpressionSearch range:NSMakeRange(openingBracketLocation, closingBracketLocation - openingBracketLocation)];
130+
}
131+
117132
if(defaultAutocompletionRange.location != NSNotFound) {
118133
[textView insertText:@"" replacementRange:defaultAutocompletionRange];
119134
closingBracketLocation -= defaultAutocompletionRange.length;
@@ -131,7 +146,12 @@ - (BOOL)tryExpandingSwitchStatement
131146

132147
for(IDEIndexSymbol *child in [((IDEIndexContainerSymbol*)symbol).children allObjects]) {
133148
if([switchContent rangeOfString:child.displayName].location == NSNotFound) {
134-
[replacementString appendString:[NSString stringWithFormat:@"case %@: {\n<#statement#>\nbreak;\n}\n", child.displayName]];
149+
if ([[SCXcodeSwitchExpander sharedSwitchExpander] isSwift]) {
150+
NSString *childDisplayName = [self correctEnumConstantIfFromCocoa:[NSString stringWithFormat:@"%@",symbol] symbolName:symbolName cocoaEnumName:child.displayName];
151+
[replacementString appendString:[NSString stringWithFormat:@"case .%@: \n<#statement#>\nbreak\n\n", childDisplayName]];
152+
} else {
153+
[replacementString appendString:[NSString stringWithFormat:@"case %@: {\n<#statement#>\nbreak;\n}\n", child.displayName]];
154+
}
135155
}
136156
}
137157

@@ -141,13 +161,16 @@ - (BOOL)tryExpandingSwitchStatement
141161
switchContentRange = NSMakeRange(openingBracketLocation + 1, closingBracketLocation - openingBracketLocation - 1);
142162
switchContent = [textView.string substringWithRange:switchContentRange];
143163

144-
// Insert the default case if necessary
145-
if([switchContent rangeOfString:@"default"].location == NSNotFound) {
146-
replacementString = [NSMutableString stringWithString:@"default: {\nbreak;\n}\n"];
147-
[textView insertText:replacementString replacementRange:NSMakeRange(switchContentRange.location + switchContentRange.length, 0)];
148-
closingBracketLocation += replacementString.length;
149-
}
150-
164+
// Insert the default case if necessary
165+
if([switchContent rangeOfString:@"default"].location == NSNotFound) {
166+
if ([[SCXcodeSwitchExpander sharedSwitchExpander] isSwift]) {
167+
replacementString = [NSMutableString stringWithString:@"default: \nbreak\n\n"];
168+
} else {
169+
replacementString = [NSMutableString stringWithString:@"default: {\nbreak;\n}\n"];
170+
}
171+
[textView insertText:replacementString replacementRange:NSMakeRange(switchContentRange.location + switchContentRange.length, 0)];
172+
closingBracketLocation += replacementString.length;
173+
}
151174
// Re-indent everything
152175
NSRange reindentRange = NSMakeRange(openingBracketLocation, closingBracketLocation - openingBracketLocation + 2);
153176
[textView _indentInsertedTextIfNecessaryAtRange:reindentRange];
@@ -164,6 +187,20 @@ - (BOOL)tryExpandingSwitchStatement
164187
return NO;
165188
}
166189

190+
- (NSString *)correctEnumConstantIfFromCocoa:(NSString *)symbol symbolName:(NSString *)symbolName cocoaEnumName:(NSString *)enumName
191+
{
192+
if ([symbol rangeOfString:@"c:@E@"].location != NSNotFound) {
193+
return [enumName stringByReplacingOccurrencesOfString:symbolName withString:@""];
194+
}
195+
196+
return enumName;
197+
}
198+
199+
- (BOOL)isSymbolKindEnum:(DVTSourceCodeSymbolKind *)symbol
200+
{
201+
return [symbol.identifier isEqualToString:@"Xcode.SourceCodeSymbolKind.Enum"];
202+
}
203+
167204
- (NSUInteger)matchingBracketLocationForOpeningBracketLocation:(NSUInteger)location inString:(NSString *)string
168205
{
169206
if(string.length == 0) {

SCXcodeSwitchExpander/SCXcodeSwitchExpander.h

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
+ (instancetype)sharedSwitchExpander;
1414

1515
@property (nonatomic, weak, readonly) IDEIndex *index;
16+
@property (nonatomic, assign) BOOL isSwift;
1617

1718
@end

SCXcodeSwitchExpander/SCXcodeSwitchExpander.m

100644100755
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#import "SCXcodeSwitchExpander.h"
1010
#import "IDEIndex.h"
11+
#import "IDEEditor.h"
12+
#import "IDEFileTextSettings.h"
1113

1214
static SCXcodeSwitchExpander *sharedExpander = nil;
1315

@@ -29,26 +31,42 @@ + (void)pluginDidLoad:(NSBundle *)plugin
2931

3032
+ (instancetype)sharedSwitchExpander
3133
{
32-
static dispatch_once_t onceToken;
34+
static dispatch_once_t onceToken;
3335
dispatch_once(&onceToken, ^{
3436
sharedExpander = [[self alloc] init];
3537
});
36-
37-
return sharedExpander;
38+
39+
return sharedExpander;
3840
}
3941

4042
- (id)init
4143
{
4244
if (self = [super init]) {
43-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(indexDidChange:) name:@"IDEIndexDidChangeNotification" object:nil];
44-
}
45-
45+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(indexDidChange:) name:@"IDEIndexDidChangeNotification" object:nil];
46+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editorDidDidFinishSetup:) name:@"IDESourceCodeEditorDidFinishSetup" object:nil];
47+
}
48+
4649
return self;
4750
}
4851

52+
- (void)editorDidDidFinishSetup:(NSNotification *)sender
53+
{
54+
IDEEditor * editor = sender.object;
55+
IDEFileTextSettings *fileSettings = editor.fileTextSettings;
56+
IDEFileReference *fileReference = fileSettings.fileReference;
57+
58+
/*
59+
Since I've got this on Console:<Xcode3FileReference, 0x7fcde7977580 (Represents: <PBXFileReference:0x7fcdeb1018e0:18990B3518D2529C007A8756:name='SCXcodeSwitchExpander.m'>)>
60+
Yes, lazy, and I'm sorry but its 2am and importing XCode Header files is a pain in the ass :P
61+
*/
62+
NSString *fileReferenceStringBulk = [NSString stringWithFormat:@"%@",fileReference];
63+
64+
self.isSwift = [fileReferenceStringBulk rangeOfString:@".swift"].location != NSNotFound;
65+
}
66+
4967
- (void)indexDidChange:(NSNotification *)sender
5068
{
51-
self.index = sender.object;
69+
self.index = sender.object;
5270
}
5371

5472
@end

SCXcodeSwitchExpander/Xcode Headers/DVTSourceCodeSymbolKind.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
// Copyright (c) 2014 Stefan Ceriu. All rights reserved.
77
//
88

9-
@interface DVTSourceCodeSymbolKind : NSObject
9+
@class NSArray, NSString;
1010

11-
@property(readonly, getter=isContainer) BOOL container;
11+
@interface DVTSourceCodeSymbolKind : NSObject <NSCopying>
1212

13-
@end
13+
@property(readonly) NSString *identifier;
14+
@property(readonly, getter=isContainer) BOOL container;
15+
@property(readonly) NSArray *allConformingSymbolKinds;
1416

17+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// DVTViewController.h
3+
// SCXcodeSwitchExpander
4+
//
5+
// Created by Stefan Ceriu on 16/05/2015.
6+
// Copyright (c) 2015 Stefan Ceriu. All rights reserved.
7+
//
8+
9+
#import <Cocoa/Cocoa.h>
10+
11+
@interface DVTViewController : NSViewController
12+
13+
@end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// IDEEditor.h
3+
// SCXcodeSwitchExpander
4+
//
5+
// Created by Stefan Ceriu on 16/05/2015.
6+
// Copyright (c) 2015 Stefan Ceriu. All rights reserved.
7+
//
8+
9+
#import "DVTViewController.h"
10+
11+
@class IDEFileTextSettings;
12+
13+
@interface IDEEditor : DVTViewController
14+
15+
@property(retain, nonatomic) IDEFileTextSettings *fileTextSettings;
16+
17+
@end
18+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// IDEFileTextSettings.h
3+
// SCXcodeSwitchExpander
4+
//
5+
// Created by Stefan Ceriu on 16/05/2015.
6+
// Copyright (c) 2015 Stefan Ceriu. All rights reserved.
7+
//
8+
9+
@class IDEFileReference;
10+
11+
@interface IDEFileTextSettings : NSObject
12+
13+
@property(retain, nonatomic) IDEFileReference *fileReference;
14+
15+
@end
16+

0 commit comments

Comments
 (0)