forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXcodeThemeParser.swift
More file actions
358 lines (336 loc) · 14.2 KB
/
XcodeThemeParser.swift
File metadata and controls
358 lines (336 loc) · 14.2 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
import Foundation
import Preferences
public struct XcodeTheme: Codable {
public struct ThemeColor: Codable {
public var red: Double
public var green: Double
public var blue: Double
public var alpha: Double
public var hexString: String {
let red = Int(self.red * 255)
let green = Int(self.green * 255)
let blue = Int(self.blue * 255)
let alpha = Int(self.alpha * 255)
return String(format: "#%02X%02X%02X%02X", red, green, blue, alpha)
}
var storable: StorableColor {
.init(red: red, green: green, blue: blue, alpha: alpha)
}
}
public struct ThemeFont: Codable {
public var name: String
public var size: Double
var storable: StorableFont {
.init(name: name, size: size)
}
}
public var plainTextColor: ThemeColor
public var plainTextFont: ThemeFont
public var commentColor: ThemeColor
public var documentationMarkupColor: ThemeColor
public var documentationMarkupKeywordColor: ThemeColor
public var marksColor: ThemeColor
public var stringsColor: ThemeColor
public var charactersColor: ThemeColor
public var numbersColor: ThemeColor
public var regexLiteralsColor: ThemeColor
public var regexLiteralNumbersColor: ThemeColor
public var regexLiteralCaptureNamesColor: ThemeColor
public var regexLiteralCharacterClassNamesColor: ThemeColor
public var regexLiteralOperatorsColor: ThemeColor
public var keywordsColor: ThemeColor
public var preprocessorStatementsColor: ThemeColor
public var urlsColor: ThemeColor
public var attributesColor: ThemeColor
public var typeDeclarationsColor: ThemeColor
public var otherDeclarationsColor: ThemeColor
public var projectClassNamesColor: ThemeColor
public var projectFunctionAndMethodNamesColor: ThemeColor
public var projectConstantsColor: ThemeColor
public var projectTypeNamesColor: ThemeColor
public var projectPropertiesAndGlobalsColor: ThemeColor
public var projectPreprocessorMacrosColor: ThemeColor
public var otherClassNamesColor: ThemeColor
public var otherFunctionAndMethodNamesColor: ThemeColor
public var otherConstantsColor: ThemeColor
public var otherTypeNamesColor: ThemeColor
public var otherPropertiesAndGlobalsColor: ThemeColor
public var otherPreprocessorMacrosColor: ThemeColor
public var headingColor: ThemeColor
public var backgroundColor: ThemeColor
public var selectionColor: ThemeColor
public var cursorColor: ThemeColor
public var currentLineColor: ThemeColor
public var invisibleCharactersColor: ThemeColor
public var debuggerConsolePromptColor: ThemeColor
public var debuggerConsoleOutputColor: ThemeColor
public var debuggerConsoleInputColor: ThemeColor
public var executableConsoleOutputColor: ThemeColor
public var executableConsoleInputColor: ThemeColor
public func asHighlightJSTheme() -> String {
buildHighlightJSTheme(self)
.replacingOccurrences(of: "\n", with: "")
.replacingOccurrences(of: ": ", with: ":")
.replacingOccurrences(of: "} ", with: "}")
.replacingOccurrences(of: " {", with: "{")
.replacingOccurrences(of: ";}", with: "}")
.replacingOccurrences(of: " ", with: "")
}
}
public extension XcodeTheme {
/// Color scheme locations:
/// ~/Library/Developer/Xcode/UserData/FontAndColorThemes/
/// Xcode.app/Contents/SharedFrameworks/DVTUserInterfaceKit.framework/Versions/A/Resources/FontAndColorThemes
init(fileURL: URL) throws {
let parser = XcodeThemeParser()
self = try parser.parse(fileURL: fileURL)
}
}
struct XcodeThemeParser {
enum Error: Swift.Error {
case fileNotFound
case invalidData
}
func parse(fileURL: URL) throws -> XcodeTheme {
guard let data = try? Data(contentsOf: fileURL) else {
throw Error.fileNotFound
}
if fileURL.pathExtension == "xccolortheme" {
return try parseXCColorTheme(data)
} else {
throw Error.invalidData
}
}
func parseXCColorTheme(_ data: Data) throws -> XcodeTheme {
let plist = try? PropertyListSerialization.propertyList(
from: data,
options: .mutableContainers,
format: nil
) as? [String: Any]
guard let theme = plist else { throw Error.invalidData }
func getRawThemeValue(at path: [String]) -> String? {
guard !path.isEmpty else { return nil }
let keys = path.dropLast(1)
var currentDict = theme
for key in keys {
guard let value = currentDict[key] as? [String: Any] else {
return nil
}
currentDict = value
}
return currentDict[path.last!] as? String
}
/// The source value is an `r g b a` string, for example: `0.5 0.5 0.2 1`
func convertColor(source: String) -> XcodeTheme.ThemeColor {
let components = source.split(separator: " ")
let red = (components[0] as NSString).doubleValue
let green = (components[1] as NSString).doubleValue
let blue = (components[2] as NSString).doubleValue
let alpha = (components[3] as NSString).doubleValue
return .init(red: red, green: green, blue: blue, alpha: alpha)
}
func getThemeValue(
at path: [String],
defaultValue: XcodeTheme.ThemeColor = .init(red: 0, green: 0, blue: 0, alpha: 1)
) -> XcodeTheme.ThemeColor {
if let value = getRawThemeValue(at: path) {
return convertColor(source: value)
}
return defaultValue
}
/// The source value is an `FontName - size` string, for example: `SFMono-Medium - 12.0`
func convertFont(source: String) -> XcodeTheme.ThemeFont? {
if let separator = source.range(of: " - ") {
let name = String(source.prefix(upTo: separator.lowerBound))
let size = Double(source.suffix(from: separator.upperBound)) ?? 0.0
return .init(name: name, size: size)
}
return nil
}
func getThemeFont(
at path: [String],
defaultValue: XcodeTheme.ThemeFont = .init(name: "SFMono-Medium", size: 12.0)
) -> XcodeTheme.ThemeFont {
if let value = getRawThemeValue(at: path) {
return convertFont(source: value) ?? defaultValue
}
return defaultValue
}
let black = XcodeTheme.ThemeColor(red: 0, green: 0, blue: 0, alpha: 1)
let white = XcodeTheme.ThemeColor(red: 1, green: 1, blue: 1, alpha: 1)
let xcodeTheme = XcodeTheme(
plainTextColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.plain"],
defaultValue: black
),
plainTextFont: getThemeFont(
at: ["DVTSourceTextSyntaxFonts", "xcode.syntax.plain"]
),
commentColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.comment"],
defaultValue: black
),
documentationMarkupColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.comment.doc"],
defaultValue: black
),
documentationMarkupKeywordColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.comment.doc.keyword"],
defaultValue: black
),
marksColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.mark"],
defaultValue: black
),
stringsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.string"],
defaultValue: black
),
charactersColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.character"],
defaultValue: black
),
numbersColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.number"],
defaultValue: black
),
regexLiteralsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.plain"],
defaultValue: black
),
regexLiteralNumbersColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.number"],
defaultValue: black
),
regexLiteralCaptureNamesColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.plain"],
defaultValue: black
),
regexLiteralCharacterClassNamesColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.plain"],
defaultValue: black
),
regexLiteralOperatorsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.plain"],
defaultValue: black
),
keywordsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.keyword"],
defaultValue: black
),
preprocessorStatementsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.preprocessor"],
defaultValue: black
),
urlsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.url"],
defaultValue: black
),
attributesColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.attribute"],
defaultValue: black
),
typeDeclarationsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.declaration.type"],
defaultValue: black
),
otherDeclarationsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.declaration.other"],
defaultValue: black
),
projectClassNamesColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.class"],
defaultValue: black
),
projectFunctionAndMethodNamesColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.function"],
defaultValue: black
),
projectConstantsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.constant"],
defaultValue: black
),
projectTypeNamesColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.type"],
defaultValue: black
),
projectPropertiesAndGlobalsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.variable"],
defaultValue: black
),
projectPreprocessorMacrosColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.macro"],
defaultValue: black
),
otherClassNamesColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.class.system"],
defaultValue: black
),
otherFunctionAndMethodNamesColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.function.system"],
defaultValue: black
),
otherConstantsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.constant.system"],
defaultValue: black
),
otherTypeNamesColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.type.system"],
defaultValue: black
),
otherPropertiesAndGlobalsColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.variable.system"],
defaultValue: black
),
otherPreprocessorMacrosColor: getThemeValue(
at: ["DVTSourceTextSyntaxColors", "xcode.syntax.identifier.macro.system"],
defaultValue: black
),
headingColor: getThemeValue(
at: ["DVTMarkupTextPrimaryHeadingColor"],
defaultValue: black
),
backgroundColor: getThemeValue(
at: ["DVTSourceTextBackground"],
defaultValue: white
),
selectionColor: getThemeValue(
at: ["DVTSourceTextSelectionColor"],
defaultValue: black
),
cursorColor: getThemeValue(
at: ["DVTSourceTextInsertionPointColor"],
defaultValue: black
),
currentLineColor: getThemeValue(
at: ["DVTSourceTextCurrentLineHighlightColor"],
defaultValue: black
),
invisibleCharactersColor: getThemeValue(
at: ["DVTSourceTextInvisiblesColor"],
defaultValue: black
),
debuggerConsolePromptColor: getThemeValue(
at: ["DVTConsoleDebuggerPromptTextColor"],
defaultValue: black
),
debuggerConsoleOutputColor: getThemeValue(
at: ["DVTConsoleDebuggerOutputTextColor"],
defaultValue: black
),
debuggerConsoleInputColor: getThemeValue(
at: ["DVTConsoleDebuggerInputTextColor"],
defaultValue: black
),
executableConsoleOutputColor: getThemeValue(
at: ["DVTConsoleExectuableOutputTextColor"],
defaultValue: black
),
executableConsoleInputColor: getThemeValue(
at: ["DVTConsoleExectuableInputTextColor"],
defaultValue: black
)
)
return xcodeTheme
}
}