- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathXcode.m
More file actions
Latest commit
375 lines (300 loc) · 10.3 KB
/
Copy pathXcode.m
File metadata and controls
375 lines (300 loc) · 10.3 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
@interfaceFakeDocumentController:NSObject
@end
@implementationFakeDocumentController
-(id)workspaceDocuments
{
returnnil;
}
@end
BOOL mightNeedFakeDocumentController=true;
NSObject* hackDocumentController()
{
if(@available(macOS 27,*))
{
if(mightNeedFakeDocumentController&&[NSThread.callStackSymbols[1] containsString:@"$s6IDEKit35IDEWorkspaceThemeOverrideDataSourceC15installIfNeeded33_EC2B2934FA8B952E633945C4050A3FA5LLyyFZ"])
{
return FakeDocumentController.alloc.init.autorelease;
}
mightNeedFakeDocumentController=false;
}
returnnil;
}
NSMenu* hackContextMenu()
{
returncontextMenuHook();
}
@implementationXcode
+(NSString*)replacePath:(NSString*)template
{
if(!xcodePath)
{
xcodePath=[NSWorkspace.sharedWorkspace URLForApplicationWithBundleIdentifier:@"com.apple.dt.Xcode"].path;
if(!xcodePath)
{
alertAbort(@"xcode missing");
}
}
return [template stringByReplacingOccurrencesOfString:@"%"withString:xcodePath];
}
+(XcodeDocument*)documentWithURL:(NSURL*)urltype:(NSString*)type
{
return [(XcodeDocument*)SoftDocument.alloc initWithContentsOfURL:url ofType:type error:nil].autorelease;
}
+(void)destroyDocument:(XcodeDocument*)document
{
document.retain;
dispatch_async(dispatch_get_main_queue(),^()
{
document.close;
document.release;
});
}
+(XcodeViewController*)viewControllerWithDocument:(XcodeDocument*)document
{
XcodeViewController* controller=[(XcodeViewController*)SoftViewController.alloc initWithNibName:nilbundle:nildocument:document].autorelease;
// TODO: needed for trimming whitespace, weird
controller.fileTextSettings=((NSObject*)SoftSettings2.alloc).init.autorelease;
return controller;
}
+(NSRange)selectionWithViewController:(XcodeViewController*)controller
{
XcodeDocumentLocation* location=controller.currentSelectedDocumentLocations.firstObject;
return location?location.characterRange:NSMakeRange(0,0);
}
+(void)focusViewController:(XcodeViewController*)controllerwithSelection:(NSRange)selection
{
NSURL* fakeURL=[NSURL.alloc initWithString:@""].autorelease;
XcodeDocumentLocation* location=[(XcodeDocumentLocation*)SoftDocumentLocation.alloc initWithDocumentURL:fakeURL timestamp:nilcharacterRange:selection].autorelease;
[controller selectDocumentLocations:@[location]];
// TODO: hack to load views in time for takeFocus
controller.view.window.display;
controller.takeFocus;
}
+(void)destroyViewController:(XcodeViewController*)controller
{
// TODO: fixes a memory leak closing tabs (not windows)
assert(controller.view.window);
controller.view.display;
controller.view.removeFromSuperview;
controller.invalidate;
}
+(XcodeSettings*)settings
{
return [SoftSettings sharedPreferences];
}
+(NSArray<XcodeTheme2*>*)themes
{
return [SoftTheme2 preferenceSetsManager].availablePreferenceSets;
}
+(NSArray<NSString*>*)themeNames
{
NSArray<NSString*>* names=[Xcode.themes valueForKey:@"localizedName"];
return [names sortedArrayUsingSelector:@selector(compare:)];
}
+(XcodeTheme2*)theme
{
return [SoftTheme2 preferenceSetsManager].currentPreferenceSet;
}
+(NSString*)themeName
{
return Xcode.theme.localizedName;
}
+(BOOL)themeIsLight
{
return Xcode.theme.hasLightBackground;
}
+(void)setTheme:(XcodeTheme2*)theme
{
[SoftTheme2 preferenceSetsManager].currentPreferenceSet=theme;
[NSUserDefaults.standardUserDefaults setObject:theme.name forKey:XcodeLightThemeKey];
[NSUserDefaults.standardUserDefaults setObject:theme.name forKey:XcodeDarkThemeKey];
}
+(void)setThemeName:(NSString*)name
{
XcodeTheme2* matched=nil;
for(XcodeTheme2* theme in Xcode.themes)
{
if([theme.localizedName isEqual:name])
{
matched=theme;
break;
}
}
if(!matched)
{
alert(@"theme missing");
return;
}
Xcode.theme=matched;
}
+(NSString*)systemThemesPath
{
for(NSString* template in @[@"%/Contents/SharedFrameworks/DVTUserInterfaceKit.framework/Versions/A/Resources/FontAndColorThemes",@"%/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/FontAndColorThemes"])
{
NSString* path=[Xcode replacePath:template];
if([NSFileManager.defaultManager fileExistsAtPath:path])
{
return path;
}
}
alertAbort(@"system themes folder missing");
}
+(NSString*)userThemesPath
{
return@"~/Library/Developer/Xcode/UserData/FontAndColorThemes".stringByExpandingTildeInPath;
}
+(void)saveThemeWithName:(NSString*)namebackgroundColor:(NSString*)backgroundColorhighlightColor:(NSString*)highlightColorselectionColor:(NSString*)selectionColordefaultFont:(NSString*)defaultFontdefaultColor:(NSString*)defaultColorcommentFont:(NSString*)commentFontcommentColor:(NSString*)commentColorpreprocessorFont:(NSString*)preprocessorFontpreprocessorColor:(NSString*)preprocessorColorclassFont:(NSString*)classFontclassColor:(NSString*)classColorfunctionFont:(NSString*)functionFontfunctionColor:(NSString*)functionColorkeywordFont:(NSString*)keywordFontkeywordColor:(NSString*)keywordColorstringFont:(NSString*)stringFontstringColor:(NSString*)stringColornumberFont:(NSString*)numberFontnumberColor:(NSString*)numberColor
{
NSString* basePath=[Xcode.systemThemesPath stringByAppendingPathComponent:@"Default (Light).xccolortheme"];
NSData* baseData=[NSDatadataWithContentsOfFile:basePath];
if(!baseData)
{
alertAbort(@"base theme missing");
}
NSMutableDictionary* custom=[NSPropertyListSerializationpropertyListWithData:baseData options:NSPropertyListMutableContainers format:nilerror:nil];
if(!custom)
{
alertAbort(@"base theme broken");
}
custom[XcodeThemeBackgroundKey]=backgroundColor;
custom[XcodeThemeHighlightKey]=highlightColor;
custom[XcodeThemeSelectionKey]=selectionColor;
custom[XcodeThemeCursorKey]=defaultColor;
custom[XcodeThemeInvisiblesKey]=commentColor;
custom[XcodeThemeMarkdownCodeKey]=stringColor;
custom[XcodeThemeLineHeightKey]=@1;
NSMutableDictionary* innerFonts=custom[XcodeThemeFontsKey];
NSMutableDictionary* innerColors=custom[XcodeThemeColorsKey];
for(NSString* key in innerColors.allKeys)
{
NSString* font=defaultFont;
NSString* color=defaultColor;
if([XcodeThemeCommentKeys containsObject:key])
{
font=commentFont;
color=commentColor;
}
elseif([XcodeThemePreprocessorKeys containsObject:key])
{
font=preprocessorFont;
color=preprocessorColor;
}
elseif([XcodeThemeClassKeys containsObject:key])
{
font=classFont;
color=classColor;
}
elseif([XcodeThemeFunctionKeys containsObject:key])
{
font=functionFont;
color=functionColor;
}
elseif([XcodeThemeKeywordKeys containsObject:key])
{
font=keywordFont;
color=keywordColor;
}
elseif([XcodeThemeStringKeys containsObject:key])
{
font=stringFont;
color=stringColor;
}
elseif([XcodeThemeNumberKeys containsObject:key])
{
font=numberFont;
color=numberColor;
}
innerFonts[key]=font;
innerColors[key]=color;
}
NSString* customPath=[Xcode.userThemesPath stringByAppendingPathComponent:[name stringByAppendingString:@".xccolortheme"]];
[NSFileManager.defaultManager createDirectoryAtPath:customPath.stringByDeletingLastPathComponent withIntermediateDirectories:trueattributes:nilerror:nil];
NSData* customData=[NSPropertyListSerializationdataWithPropertyList:custom format:NSPropertyListXMLFormat_v1_0 options:0error:nil];
if(![customData writeToFile:customPath atomically:true])
{
alertAbort(@"theme write failed");
}
}
+(void)addThemeChangeHandler:(void (^)())handler
{
void (^leakedHandler)()=[handler copy];
[NSNotificationCenter.defaultCenter addObserverForName:XcodeThemeChangedKey object:nilqueue:nilusingBlock:^(NSNotification* note)
{
leakedHandler();
}];
}
+(void)setContextMenuHook:(NSMenu* (^)())block
{
contextMenuHook=[block copy];
}
+(void)linkLibrary:(NSString*)path
{
if(!dlopen(path.UTF8String,RTLD_LAZY))
{
alertAbort([NSStringstringWithFormat:@"dlopen failed: %s",dlerror()]);
}
}
+(void*)linkSymbol:(NSString*)name
{
void* symbol=dlsym(RTLD_DEFAULT,name.UTF8String);
if(!symbol)
{
alertAbort([NSStringstringWithFormat:@"dlsym failed: %s",dlerror()]);
}
return symbol;
}
+(Class)linkClass:(NSString*)name
{
return [Xcode linkSymbol:[NSStringstringWithFormat:@"OBJC_CLASS_$_%@",name]];
}
+(IMP)swizzleWithClass:(NSString*)classNameselector:(NSString*)selNameisInstance:(BOOL)isInstanceimplementation:(IMP)newImp
{
Class class=NSClassFromString(className);
if(!class)
{
alertAbort(@"swizzle class missing");
}
SEL sel=NSSelectorFromString(selName);
Method method=(isInstance?class_getInstanceMethod:class_getClassMethod)(class,sel);
if(!method)
{
alertAbort(@"swizzle method missing");
}
returnmethod_setImplementation(method,newImp);
}
+(void)setupWithArgv:(char**)argv
{
// TODO: case where it's already set? maybe we should instead check if IDESourceEditor fails to load?
if(!getenv("DYLD_FRAMEWORK_PATH"))
{
NSString* dylibPaths=[Xcode replacePath:@"%/Contents/Frameworks:%/Contents/SharedFrameworks:%/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks:%/Contents/Developer/Library/Frameworks"];
setenv("DYLD_FRAMEWORK_PATH",dylibPaths.UTF8String,true);
setenv("DYLD_LIBRARY_PATH",dylibPaths.UTF8String,true);
execv(argv[0],argv);
alertAbort(@"re-exec failed");
}
[Xcode linkLibrary:[Xcode replacePath:@"%/Contents/PlugIns/IDESourceEditor.framework/Versions/A/IDESourceEditor"]];
SoftInitialize=[Xcode linkSymbol:@"IDEInitialize"];
SoftDocument=[Xcode linkClass:@"_TtC15IDESourceEditor18SourceCodeDocument"];
SoftViewController=[Xcode linkClass:@"_TtC15IDESourceEditor16SourceCodeEditor"];
SoftTheme=[Xcode linkClass:@"DVTTheme"];
SoftTheme2=[Xcode linkClass:@"DVTFontAndColorTheme"];
SoftSettings=[Xcode linkClass:@"DVTTextPreferences"];
SoftSettings2=[Xcode linkClass:@"IDEFileTextSettings"];
SoftDocumentLocation=[Xcode linkClass:@"DVTTextDocumentLocation"];
// TODO: stupid
[Xcode swizzleWithClass:@"IDEDocumentController"selector:@"sharedDocumentController"isInstance:falseimplementation:(IMP)hackDocumentController];
[Xcode swizzleWithClass:@"_TtC12SourceEditor16SourceEditorView"selector:@"menuForEvent:"isInstance:trueimplementation:(IMP)hackContextMenu];
// TODO: aborts if Xcode present but never opened
NSError* error=nil;
SoftInitialize(0,&error);
if(error)
{
alertAbort([NSStringstringWithFormat:@"xcode init failed: %@",error]);
}
// TODO: is there a more normal way to call this?
[SoftTheme initialize];
// TODO: still missing xcode indexing-based colors
// TODO: source control sidebar?
}
@end