/
JPObjcIndex.m
180 lines (167 loc) · 5.61 KB
/
JPObjcIndex.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// JPObjcIndex.m
// JSPatchX
//
// Created by bang on 4/16/16.
// Copyright © 2016 bang. All rights reserved.
//
#import "JPObjcIndex.h"
#import "IDEWorkspace+JSPatchX.h"
#import "IDEWorkspaceArena.h"
#import "DVTFilePath.h"
#import "JPCompletionItem.h"
#import "DVTSourceCodeSymbolKind.h"
#import "JPObjcFile.h"
#import "JPObjcMethod.h"
#import "JPObjcClass.h"
#import "JPObjcProtocol.h"
@implementation JPObjcIndex {
/*
{
"$clsname" : {
"super": "$superClassName"j,
"methods": [ $JPObjcMethod, ...]
}, ...
}
*/
NSMutableDictionary *_parsedClassCache;
NSMutableArray *_protocolCompletionItems;
__weak IDEWorkspace *_workspace;
}
- (instancetype)initWithWorkspace:(IDEWorkspace *)workspace
{
self = [super init];
if (self) {
_workspace = workspace;
}
return self;
}
- (NSArray *)_superClassesOfClass:(NSString *)clsName
{
NSMutableArray *classes = [[NSMutableArray alloc] init];
while (![clsName isEqualToString:@"NSObject"] && clsName) {
if (_parsedClassCache[clsName]) {
NSString *superClsName = _parsedClassCache[clsName][@"super"];
if (superClsName.length) {
[classes addObject:superClsName];
}
clsName = superClsName;
} else {
break;
}
}
return classes;
}
- (NSArray *)_allClassesWithClasses:(NSArray *)classes
{
NSMutableArray *allClasses = [[NSMutableArray alloc] init];
for (NSString *clsName in classes) {
NSArray *superClasses = [self _superClassesOfClass:clsName];
for (NSString *superClsName in superClasses) {
if (![allClasses containsObject:superClasses]) {
[allClasses addObject:superClsName];
}
}
if (![allClasses containsObject:clsName]) {
[allClasses addObject:clsName];
}
}
if (![allClasses containsObject:@"NSObject"]) {
[allClasses addObject:@"NSObject"];
}
return allClasses;
}
- (NSArray *)_completionItemsWithClass:(NSString *)className
{
@synchronized(self) {
if (_parsedClassCache[className] && _parsedClassCache[className][@"methods"]) {
NSArray *methods = _parsedClassCache[className][@"methods"];
NSMutableArray *completionItems = [[NSMutableArray alloc] initWithCapacity:methods.count];
for (JPObjcMethod *method in methods) {
if (method.methodName.length == 0) {
continue;
}
[completionItems addObject:[method completionItem]];
}
return completionItems;
}
}
return nil;
}
- (NSArray *)methodCompletionItemsWithClasses:(NSArray *)classes
{
@synchronized(self) {
if (!_parsedClassCache) {
_parsedClassCache = [[NSMutableDictionary alloc] init];
_protocolCompletionItems = [[NSMutableArray alloc] init];
[self _scanProjectHeaders];
[self _scanDefaultFramework];
}
}
NSMutableArray *ret = [[NSMutableArray alloc] init];
NSArray *allClasses = [self _allClassesWithClasses:classes];
for (NSString *clsName in allClasses) {
[ret addObjectsFromArray:[self _completionItemsWithClass:clsName]];
}
return ret;
}
- (NSArray *)protocolCompletionItems
{
@synchronized (self) {
return _protocolCompletionItems;
}
}
- (void)_scanProjectHeaders
{
NSString *folder = [_workspace currentProjectFolder];
NSArray *fileList = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:folder error:nil];
for (NSString *file in fileList) {
if ([file hasSuffix:@".h"]) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@", folder, file];
[self _parseFile:filePath];
}
}
}
-(void)_scanDefaultFramework
{
NSArray *arrDirs = [_workspace defaultScanHeaderDirs];
for (NSString *dir in arrDirs) {
if (![[NSFileManager defaultManager] fileExistsAtPath:dir]) {
continue;
}
NSArray *headers = [[NSFileManager defaultManager] subpathsAtPath:dir];
for (NSString *header in headers) {
if ([header hasSuffix:@".h"] || [header hasSuffix:@".H"]) {
NSString *filePath = [NSString stringWithFormat:@"%@/%@", dir, header];
[self _parseFile:filePath];
}
}
}
}
- (void)_parseFile:(NSString *)filePath
{
JPObjcFile *objcFile = [JPObjcFile parseFile:filePath];
if (objcFile && objcFile.classes) {
@synchronized(self) {
for (JPObjcClass *objcCls in objcFile.classes) {
if (!objcCls.methods.count) continue;
if (!_parsedClassCache[objcCls.clsName]) {
_parsedClassCache[objcCls.clsName] = [[NSMutableDictionary alloc] init];
}
NSMutableArray *methods = _parsedClassCache[objcCls.clsName][@"methods"];
if (methods && [methods isKindOfClass:[NSMutableArray class]]) {
[methods addObjectsFromArray:objcCls.methods];
} else {
[_parsedClassCache[objcCls.clsName] setObject:[objcCls.methods mutableCopy] forKey:@"methods"];
}
if (!_parsedClassCache[objcCls.clsName][@"super"] && objcCls.superClsName.length) {
[_parsedClassCache[objcCls.clsName] setObject:objcCls.superClsName forKey:@"super"];
}
}
for (JPObjcProtocol *prop in objcFile.protocols) {
[_protocolCompletionItems addObjectsFromArray:prop.methodCompletionItems];
}
}
}
}
@end