-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathSettings.swift
1903 lines (1664 loc) · 63.4 KB
/
Settings.swift
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Settings.swift
// SourceCodeSyntaxHighlight
//
// Created by Sbarex on 05/03/21.
// Copyright © 2021 sbarex. All rights reserved.
//
import Cocoa
protocol SettingsDelegate: AnyObject {
func settingsIsChanged(_ settings: SettingsBase)
}
typealias ThemeBaseColor = (name: String, background: String, foreground: String)
// MARK: -
class SettingsBase: NSObject {
/// Output format.
enum Format: String {
case html
case rtf
}
public struct Key {
static let format = "format"
static let lightTheme = "theme-light"
static let lightBackgroundColor = "theme-light-color"
static let lightForegroundColor = "theme-light-fg-color"
static let darkTheme = "theme-dark"
static let darkBackgroundColor = "theme-dark-color"
static let darkForegroundColor = "theme-dark-fg-color"
static let theme = "theme"
static let backgroundColor = "theme-color"
static let foregroundColor = "theme-fg-color"
static let themeLua = "theme-lua"
static let lineNumbers = "line-numbers"
static let lineNumbersFillToZeroes = "line-fill-zero"
static let wordWrap = "word-wrap"
static let wordWrapHard = "word-wrap-hard"
static let wordWrapOneLineFiles = "word-wrap-one-line-file"
static let lineLength = "line-length"
static let tabSpaces = "tab-spaces"
static let extraArguments = "extra"
static let fontFamily = "font-family"
static let fontSize = "font-size"
static let customCSS = "css"
static let interactive = "interactive"
static let maxData = "max-data"
static let convertEOL = "convert-EOL"
static let version = "version"
static let about = "about"
static let debug = "debug"
static let dumpPlain = "dump"
static let vcs = "vcs"
static let vcsDiff = "vcs-diff"
static let vcs_add_light = "vcs_add_light"
static let vcs_add_dark = "vcs_add_dark"
static let vcs_edit_light = "vcs_edit_light"
static let vcs_edit_dark = "vcs_edit_dark"
static let vcs_del_light = "vcs_del_light"
static let vcs_del_dark = "vcs_del_dark"
static let git_path = "git_path"
static let hg_path = "hg_path"
static let svn_path = "svn_path"
static let customizedUTISettings = "uti-settings"
static let plainSettings = "plain-settings"
static let specialSettingsGlobal = "global-special-settings"
static let connectedUTI = "uti"
static let specialSettingsFormat = "specialSettings"
static let preprocessor = "preprocessor"
static let syntax = "syntax"
static let appendedExtraArguments = "uti-extra"
static let lsp = "LSP"
static let lspExecutable = "LSP-executable"
static let lspDelay = "LSP-delay"
static let lspSyntax = "LSP-syntax"
static let lspHover = "LSP-hover"
static let lspSemantic = "LSP-semantic"
static let lspSyntaxError = "LSP-syntax-error"
static let lspOptions = "LSP-options"
static let qlWidth = "ql-window-width"
static let qlHeight = "ql-window-height"
}
fileprivate var refreshLock = 0
@objc dynamic fileprivate(set) var needRefresh: Bool = false
weak var delegate: SettingsDelegate?
func lockRefresh() {
refreshLock += 1
}
func unlockRefresh() {
refreshLock -= 1
if refreshLock == 0 {
setNeedRefresh()
}
}
func setNeedRefresh() {
if refreshLock == 0 {
needRefresh = true
delegate?.settingsIsChanged(self)
}
}
dynamic var format: Format {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: format)
}
}
dynamic var isFormatDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isFormatDefined)
}
}
// MARK: Themes
dynamic var isLightThemeNameDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isLightThemeNameDefined)
}
}
/// Name of theme for light visualization.
dynamic var lightThemeName: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: lightThemeName)
}
}
/// Background color for the rgb view in light theme.
var lightBackgroundColor: String
var lightForegroundColor: String
/// Background color for the rgb view in dark theme.
dynamic var isDarkThemeNameDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isDarkThemeNameDefined)
}
}
/// Name of theme for dark visualization.
dynamic var darkThemeName: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: darkThemeName)
}
}
var darkBackgroundColor: String
var darkForegroundColor: String
// MARK: Line numbers.
dynamic var isLineNumbersDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isLineNumbersDefined)
}
}
/// Show line numbers.
dynamic var isLineNumbersVisible: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isLineNumbersVisible)
}
}
dynamic var isLineNumbersFillToZeroes: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isLineNumbersFillToZeroes)
}
}
// MARK: Word wrap
dynamic var isWordWrapDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isWordWrapDefined)
}
}
/// Word wrap mode.
dynamic var isWordWrapped: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isWordWrapped)
}
}
dynamic var isWordWrappedHard: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isWordWrappedHard)
}
}
var isWordWrappedSoft: Bool {
return !isWordWrappedHard
}
dynamic var isWordWrappedSoftForOneLineFiles: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isWordWrappedSoftForOneLineFiles)
}
}
dynamic var isWordWrappedIndented: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isWordWrappedIndented)
}
}
dynamic var isLineLengthDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isLineLengthDefined)
}
}
/// Line length for word wrap.
dynamic var lineLength: Int {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: lineLength)
}
}
// MARK: Tabs
dynamic var isTabSpacesDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isTabSpacesDefined)
}
}
/// Number of spaces use for a tab. Set to 0 to disable converting tab to spaces.
dynamic var tabSpaces: Int {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: tabSpaces)
}
}
// MARK: CSS
dynamic var isCSSDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isCSSDefined)
}
}
/// Custom style sheet.
/// When the settings are stored the value is written to a file.
/// When nil use the css stored on file, if exists.
dynamic var css: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: css)
}
}
// MARK: Font
dynamic var isFontNameDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isFormatDefined)
}
}
dynamic var fontName: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: fontName)
}
}
dynamic var isFontSizeDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isFontSizeDefined)
}
}
dynamic var fontSize: CGFloat {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: fontSize)
}
}
// MARK: Interactive preview
dynamic var isAllowInteractiveActionsDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isAllowInteractiveActionsDefined)
}
}
/// If true enable js action on the Quick Look preview but disable dblclick and click and drag on window.
dynamic var allowInteractiveActions: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: allowInteractiveActions)
}
}
// MARK: Highlight arguments
dynamic var isArgumentsDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isArgumentsDefined)
}
}
/// Extra arguments for highlight.
dynamic var arguments: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: arguments)
}
}
dynamic var isVCSDefined: Bool {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: isVCSDefined)
}
}
dynamic var vcsAddLightColor: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: vcsAddLightColor)
}
}
dynamic var vcsAddDarkColor: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: vcsAddDarkColor)
}
}
dynamic var vcsEditLightColor: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: vcsEditLightColor)
}
}
dynamic var vcsEditDarkColor: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: vcsEditDarkColor)
}
}
dynamic var vcsDelLightColor: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: vcsDelLightColor)
}
}
dynamic var vcsDelDarkColor: String {
didSet {
requestRefreshOnChanged(oldValue: oldValue, newValue: vcsDelDarkColor)
}
}
// MARK:
internal var mustNotifyDirtyStatus = false
internal var lockDirty = 0 {
didSet {
if oldValue != lockDirty && lockDirty == 0 && mustNotifyDirtyStatus {
mustNotifyDirtyStatus = false
NotificationCenter.default.post(name: .SettingsIsDirty, object: self)
}
}
}
@objc dynamic var isDirty = false {
didSet {
if oldValue != isDirty {
if lockDirty == 0 {
mustNotifyDirtyStatus = false
NotificationCenter.default.post(name: .SettingsIsDirty, object: self)
} else {
mustNotifyDirtyStatus = true
}
}
}
}
var isCustomized: Bool {
get {
let state = isFormatDefined || isLightThemeNameDefined || isDarkThemeNameDefined || isLineNumbersDefined || isWordWrapDefined || isLineLengthDefined || isTabSpacesDefined || isArgumentsDefined || isCSSDefined || isFontSizeDefined || isVCSDefined
guard !state else {
return true
}
if #available(macOS 12, *) {
return false
} else {
return isAllowInteractiveActionsDefined
}
}
}
@discardableResult
internal func requestRefreshOnChanged<T: Equatable>(oldValue: T, newValue: T) ->Bool {
guard oldValue != newValue else {
return false
}
setNeedRefresh()
isDirty = true
return true
}
// MARK: - Initializers
internal required init(settings: [String: AnyHashable]) {
self.format = .rtf
isFormatDefined = false
self.lightThemeName = "edit-xcode"
isLightThemeNameDefined = false
self.lightBackgroundColor = "#ffffff"
self.lightForegroundColor = "#000000"
self.darkThemeName = "neon"
isDarkThemeNameDefined = false
self.darkBackgroundColor = "#303030"
self.darkForegroundColor = "#f0f0f0"
self.isLineNumbersVisible = false
self.isLineNumbersDefined = false
self.isLineNumbersFillToZeroes = false
self.isWordWrapped = false
self.isWordWrappedHard = false
self.isWordWrappedIndented = false
self.isWordWrappedSoftForOneLineFiles = true
self.isWordWrapDefined = false
self.lineLength = 80
self.isLineLengthDefined = false
self.tabSpaces = 4
self.isTabSpacesDefined = false
self.css = ""
self.isCSSDefined = false
self.fontName = "-" // Use the system font
self.isFontNameDefined = false
self.fontSize = NSFont.systemFontSize
self.isFontSizeDefined = false
self.allowInteractiveActions = false
self.isAllowInteractiveActionsDefined = false
self.arguments = ""
self.isArgumentsDefined = false
self.vcsAddLightColor = "#C9DEC1"
self.vcsAddDarkColor = "#009924"
self.vcsEditLightColor = "#C3D6E8"
self.vcsEditDarkColor = "#1AABFF"
self.vcsDelLightColor = "#edc5c5"
self.vcsDelDarkColor = "#fd8888"
self.isVCSDefined = false
super.init()
lockDirty += 1
self.override(fromDictionary: settings)
self.isDirty = false
self.mustNotifyDirtyStatus = false
lockDirty -= 1
}
/// Updating values from a dictionary. Settings not defined on dictionary are not updated.
/// - parameters:
/// - data: NSDictionary [String: AnyHashable]
func override(fromDictionary dict: [String: AnyHashable]?) {
guard let settings = dict else {
return
}
if let format = Settings.Format(rawValue: settings[SettingsBase.Key.format] as? String ?? "") {
self.format = format
self.isFormatDefined = true
}
// Light theme.
if let theme = settings[SettingsBase.Key.lightTheme] as? String {
self.lightThemeName = theme
isLightThemeNameDefined = true
}
// Light background color.
if let color = settings[SettingsBase.Key.lightBackgroundColor] as? String {
self.lightBackgroundColor = color
}
if let color = settings[SettingsBase.Key.lightForegroundColor] as? String {
self.lightForegroundColor = color
}
// Dark theme.
if let theme = settings[SettingsBase.Key.darkTheme] as? String {
self.darkThemeName = theme
isDarkThemeNameDefined = true
}
// Dark background color.
if let color = settings[SettingsBase.Key.darkBackgroundColor] as? String {
self.darkBackgroundColor = color
}
if let color = settings[SettingsBase.Key.darkForegroundColor] as? String {
self.darkForegroundColor = color
}
// Show line numbers.
if let ln = settings[SettingsBase.Key.lineNumbers] as? Bool {
self.isLineNumbersVisible = ln
self.isLineNumbersDefined = true
self.isLineNumbersFillToZeroes = settings[SettingsBase.Key.lineNumbersFillToZeroes] as? Bool ?? false
}
if let wrap = settings[SettingsBase.Key.wordWrap] as? Int {
self.isWordWrapped = wrap != 0
self.isWordWrappedIndented = wrap > 1
isWordWrapDefined = true
}
if let v = settings[SettingsBase.Key.wordWrapHard] as? Bool {
self.isWordWrappedHard = v
}
if let v = settings[SettingsBase.Key.wordWrapOneLineFiles] as? Bool {
self.isWordWrappedSoftForOneLineFiles = v
}
if let n = settings[SettingsBase.Key.lineLength] as? Int {
self.lineLength = n
self.isLineLengthDefined = true
}
// Convert tab to spaces.
if let n = settings[SettingsBase.Key.tabSpaces] as? Int {
self.tabSpaces = n
self.isTabSpacesDefined = true
}
if let css = settings[SettingsBase.Key.customCSS] as? String {
self.css = css
self.isCSSDefined = !css.isEmpty
}
// Font name.
if let font = settings[SettingsBase.Key.fontFamily] as? String {
self.fontName = font.isEmpty ? "-" : font
self.isFontNameDefined = true
}
// Font size.
if let pt = settings[SettingsBase.Key.fontSize] as? CGFloat {
self.fontSize = pt
self.isFontSizeDefined = true
}
if let state = settings[SettingsBase.Key.interactive] as? Bool {
self.allowInteractiveActions = state
self.isAllowInteractiveActionsDefined = true
}
// Extra arguments for _highlight_.
if let args = settings[SettingsBase.Key.extraArguments] as? String, !args.trimmingCharacters(in: .whitespaces).isEmpty {
self.arguments = args
self.isArgumentsDefined = true
}
if let color = settings[SettingsBase.Key.vcs_add_light] as? String {
self.vcsAddLightColor = color
self.isVCSDefined = true
}
if let color = settings[SettingsBase.Key.vcs_add_dark] as? String {
self.vcsAddDarkColor = color
self.isVCSDefined = true
}
if let color = settings[SettingsBase.Key.vcs_edit_light] as? String {
self.vcsEditLightColor = color
self.isVCSDefined = true
}
if let color = settings[SettingsBase.Key.vcs_edit_dark] as? String {
self.vcsEditDarkColor = color
self.isVCSDefined = true
}
if let color = settings[SettingsBase.Key.vcs_del_light] as? String {
self.vcsDelLightColor = color
self.isVCSDefined = true
}
if let color = settings[SettingsBase.Key.vcs_del_dark] as? String {
self.vcsDelDarkColor = color
self.isVCSDefined = true
}
}
// MARK:
func isEqual(to object: SettingsBase) -> Bool {
let a = self.toDictionary()
let b = self.toDictionary()
return a == b
}
func duplicate() -> Self {
return type(of: self).init(settings: self.toDictionary())
}
// MARK: -
/// Output the settings to a dictionary.
/// Only customized options are exported.
func toDictionary(forSaving: Bool = false) -> [String: AnyHashable] {
var r: [String: AnyHashable] = [:]
if isFormatDefined {
r[SettingsBase.Key.format] = format == .html ? "html" : "rtf"
}
if isLightThemeNameDefined {
r[SettingsBase.Key.lightTheme] = lightThemeName
r[SettingsBase.Key.lightBackgroundColor] = lightBackgroundColor
r[SettingsBase.Key.lightForegroundColor] = lightForegroundColor
}
if isDarkThemeNameDefined {
r[SettingsBase.Key.darkTheme] = darkThemeName
r[SettingsBase.Key.darkBackgroundColor] = darkBackgroundColor
r[SettingsBase.Key.darkForegroundColor] = darkForegroundColor
}
if isWordWrapDefined {
r[SettingsBase.Key.wordWrap] = isWordWrapped ? (isWordWrappedIndented ? 2 : 1) : 0
r[SettingsBase.Key.wordWrapHard] = isWordWrappedHard
r[SettingsBase.Key.wordWrapOneLineFiles] = isWordWrappedSoftForOneLineFiles
}
if isLineLengthDefined {
r[SettingsBase.Key.lineLength] = lineLength
}
if isTabSpacesDefined {
r[SettingsBase.Key.tabSpaces] = tabSpaces
}
if isArgumentsDefined {
r[SettingsBase.Key.extraArguments] = arguments
}
if isVCSDefined {
r[SettingsBase.Key.vcs_add_light] = vcsAddLightColor
r[SettingsBase.Key.vcs_add_dark] = vcsAddDarkColor
r[SettingsBase.Key.vcs_edit_light] = vcsEditLightColor
r[SettingsBase.Key.vcs_edit_dark] = vcsEditDarkColor
r[SettingsBase.Key.vcs_del_light] = vcsDelLightColor
r[SettingsBase.Key.vcs_del_dark] = vcsDelDarkColor
}
if isFontNameDefined {
r[SettingsBase.Key.fontFamily] = fontName
}
if isFontSizeDefined {
r[SettingsBase.Key.fontSize] = fontSize
}
if isCSSDefined {
r[SettingsBase.Key.customCSS] = css
}
if isLineNumbersDefined {
r[SettingsBase.Key.lineNumbers] = isLineNumbersVisible
r[SettingsBase.Key.lineNumbersFillToZeroes] = isLineNumbersFillToZeroes
}
if isAllowInteractiveActionsDefined {
r[SettingsBase.Key.interactive] = allowInteractiveActions
}
return r
}
func isOSThemeLight() -> Bool {
/*if #available(macOS 11.0, *) {
// Fixme: nell'estensione non sempre restituisce il valore aggiornato.
return NSAppearance.currentDrawing().bestMatch(from: [.aqua, .darkAqua]) ?? .aqua == .aqua
} else {
return (UserDefaults.standard.string(forKey: "AppleInterfaceStyle") ?? "Light") == "Light"
}*/
return (UserDefaults.standard.string(forKey: "AppleInterfaceStyle") ?? "Light") == "Light"
}
func getTheme() -> ThemeBaseColor {
let isOSThemeLight = self.isOSThemeLight()
let theme: String
let themeBackground: String
let themeForeground: String
if isOSThemeLight {
theme = self.isLightThemeNameDefined ? self.lightThemeName : "edit-xcode"
themeBackground = self.isLightThemeNameDefined ? self.lightBackgroundColor : "#ffffff"
themeForeground = self.isLightThemeNameDefined ? self.lightForegroundColor : "#000000"
} else {
theme = self.isDarkThemeNameDefined ? self.darkThemeName : "neon"
themeBackground = self.isDarkThemeNameDefined ? self.darkBackgroundColor : "#303030"
themeForeground = self.isDarkThemeNameDefined ? self.darkForegroundColor : "#f0f0f0"
}
return (name: theme, background: themeBackground, foreground: themeForeground)
}
}
protocol SettingsLSP: SettingsBase {
var useLSP: Bool { get set }
var lspExecutable: String { get set }
var lspSyntax: String { get set }
var lspDelay: Int { get set }
var lspHover: Bool { get set }
var lspSemantic: Bool { get set }
var lspSyntaxError: Bool { get set }
var lspOptions: [String] { get set }
var isUsingLSP: Bool { get }
var isLSPCustomized: Bool { get }
}
extension SettingsLSP {
var isUsingLSP: Bool {
return useLSP && !lspExecutable.isEmpty
}
var isLSPCustomized: Bool {
return useLSP && (!lspExecutable.isEmpty || !lspSyntax.isEmpty || lspDelay>0 || lspHover || lspSemantic || lspSyntaxError || !lspOptions.isEmpty)
}
func overrideLSP(fromDictionary dict: [String: AnyHashable]?) {
guard let settings = dict else {
return
}
if let v = settings[SettingsBase.Key.lsp] as? Bool {
self.useLSP = v
}
if let v = settings[SettingsBase.Key.lspExecutable] as? String {
self.lspExecutable = v
}
if let v = settings[SettingsBase.Key.lspDelay] as? Int {
self.lspDelay = v
}
if let v = settings[SettingsBase.Key.lspSyntax] as? String {
self.lspSyntax = v
}
if let v = settings[SettingsBase.Key.lspHover] as? Bool {
self.lspHover = v
}
if let v = settings[SettingsBase.Key.lspSemantic] as? Bool {
self.lspSemantic = v
}
if let v = settings[SettingsBase.Key.lspSyntaxError] as? Bool {
self.lspSyntaxError = v
}
if let v = settings[SettingsBase.Key.lspOptions] as? [String] {
self.lspOptions = v
}
}
func lspToDictionary(dict: inout [String: AnyHashable], forSaving: Bool) {
if !forSaving || self.useLSP {
dict[SettingsBase.Key.lsp] = self.useLSP
}
if !forSaving || !self.lspExecutable.isEmpty {
dict[SettingsBase.Key.lspExecutable] = self.lspExecutable
}
if !forSaving || self.lspDelay > 0 {
dict[SettingsBase.Key.lspDelay] = self.lspDelay
}
if !forSaving || !self.lspSyntax.isEmpty {
dict[SettingsBase.Key.lspSyntax] = self.lspSyntax
}
if !forSaving || self.lspHover {
dict[SettingsBase.Key.lspHover] = self.lspHover
}
if !forSaving || self.lspSemantic {
dict[SettingsBase.Key.lspSemantic] = self.lspSemantic
}
if !forSaving || self.lspSyntaxError {
dict[SettingsBase.Key.lspSyntaxError] = self.lspSyntaxError
}
if !forSaving || !self.lspOptions.isEmpty {
dict[SettingsBase.Key.lspOptions] = self.lspOptions
}
}
}
protocol SettingsFormatProtocol: SettingsBase {
dynamic var isAppendArgumentsDefined: Bool { get set }
dynamic var appendArguments: String { get set }
dynamic var isPreprocessorDefined: Bool { get set }
dynamic var preprocessor: String { get set }
dynamic var isSyntaxDefined: Bool { get set }
dynamic var syntax: String { get set }
}
// MARK: -
class SettingsFormat: SettingsBase, SettingsFormatProtocol, SettingsLSP {
var uti: String
var isCSSPopulated: Bool = false
dynamic var isAppendArgumentsDefined: Bool {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: isAppendArgumentsDefined)
}
}
dynamic var appendArguments: String {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: appendArguments)
}
}
dynamic var isPreprocessorDefined: Bool {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: isPreprocessorDefined)
}
}
dynamic var preprocessor: String {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: preprocessor)
}
}
dynamic var isSyntaxDefined: Bool {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: isSyntaxDefined)
}
}
dynamic var syntax: String {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: syntax)
}
}
var specialSyntax: String?
var specialPreprocessor: String?
var specialAppendArguments: String?
var useLSP: Bool {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: useLSP)
}
}
var lspExecutable: String {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: lspExecutable)
}
}
var lspSyntax: String {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: lspSyntax)
}
}
var lspDelay: Int {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: lspDelay)
}
}
var lspHover: Bool {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: lspHover)
}
}
var lspSemantic: Bool {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: lspSemantic)
}
}
var lspSyntaxError: Bool {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: lspSyntaxError)
}
}
var lspOptions: [String] {
didSet {
self.requestRefreshOnChanged(oldValue: oldValue, newValue: lspOptions)
}
}
override var isCustomized: Bool {
get {
return super.isCustomized || isPreprocessorDefined || isAppendArgumentsDefined || isSyntaxDefined || isLSPCustomized
}
}
convenience init (uti: String, settings: [String: AnyHashable]) {
var s = settings
s[SettingsBase.Key.connectedUTI] = uti
self.init(settings: s)
}
required internal init(settings: [String: AnyHashable]) {
self.uti = (settings[SettingsBase.Key.connectedUTI] as? String) ?? ""
self.appendArguments = ""
self.isAppendArgumentsDefined = false
self.syntax = ""
self.isSyntaxDefined = false
self.preprocessor = ""
self.isPreprocessorDefined = false
self.specialPreprocessor = nil
self.specialSyntax = nil
self.specialAppendArguments = nil
self.useLSP = false
self.lspExecutable = ""
self.lspSyntax = ""
self.lspDelay = 0
self.lspHover = false
self.lspSemantic = false
self.lspSyntaxError = false
self.lspOptions = []
super.init(settings: settings)
}
/// Updating values from a dictionary. Settings not defined on dictionary are not updated.
/// - parameters:
/// - data: NSDictionary [String: AnyHashable]
override func override(fromDictionary dict: [String: AnyHashable]?) {
guard let settings = dict else {
return
}
super.override(fromDictionary: dict)
if let args = settings[SettingsBase.Key.appendedExtraArguments] as? String, !args.trimmingCharacters(in: .whitespaces).isEmpty {
self.appendArguments = args
self.isAppendArgumentsDefined = true
}
if let syntax = settings[SettingsBase.Key.syntax] as? String {
self.syntax = syntax
self.isSyntaxDefined = true
}
if let preprocessor = settings[SettingsBase.Key.preprocessor] as? String {
self.preprocessor = preprocessor
self.isPreprocessorDefined = true
}
overrideLSP(fromDictionary: dict)
if let specials = settings[SettingsBase.Key.specialSettingsFormat] as? [String: String] {
if let v = specials[SettingsBase.Key.preprocessor] {
self.specialPreprocessor = v
}
if let v = specials[SettingsBase.Key.syntax] {
self.specialSyntax = v
}
if let v = specials[SettingsBase.Key.appendedExtraArguments], !v.trimmingCharacters(in: .whitespaces).isEmpty {
self.specialAppendArguments = v
}
}
}
override func toDictionary(forSaving: Bool = false) -> [String: AnyHashable] {
var r = super.toDictionary(forSaving: forSaving)
if isAppendArgumentsDefined {
r[SettingsBase.Key.appendedExtraArguments] = appendArguments
}
if isSyntaxDefined {
r[SettingsBase.Key.syntax] = syntax
}
if isPreprocessorDefined {
r[SettingsBase.Key.preprocessor] = preprocessor
}
lspToDictionary(dict: &r, forSaving: forSaving)
if !forSaving {
var special: [String: AnyHashable] = [:]
if let preprocessor = self.specialPreprocessor {
special[SettingsBase.Key.preprocessor] = preprocessor
}
if let syntax = self.specialSyntax {
special[SettingsBase.Key.syntax] = syntax
}
if let appendArguments = self.specialAppendArguments, !appendArguments.trimmingCharacters(in: .whitespaces).isEmpty {
special[SettingsBase.Key.appendedExtraArguments] = appendArguments
}
if !special.isEmpty {
r[SettingsBase.Key.specialSettingsFormat] = special
}
}
r[SettingsBase.Key.connectedUTI] = self.uti
return r
}
override func duplicate() -> Self {
return type(of: self).init(settings: self.toDictionary())
}
}
// MARK: -
class Settings: SettingsBase {
/// Current settings version handled by the applications.
static let version: Float = 2.4
static let plainUTIs = ["public.unix-executable", "public.data", "public.content", "public.item"]
/// Version of the settings.
var version: Float = 0
var isAllSpecialSettingsPopulated: Bool = false
var isAllCSSPopulated: Bool = false
var specialSettings: [String: [String: [String: String]]] = [:]
internal var plainSettings: [PlainSettings] = []
var app_version: String {
var title: String = "<a href='https://github.com/sbarex/SourceCodeSyntaxHighlight'>";
if let info = Bundle.main.infoDictionary {
title += (info["CFBundleExecutable"] as? String ?? "Syntax Highlight") + "</a>"
if let version = info["CFBundleShortVersionString"] as? String,
let build = info["CFBundleVersion"] as? String {
title += ", version \(version) (\(build))"
}