Skip to content

Commit 1cf519e

Browse files
author
oil3
committed
added swift, MVP
1 parent 66280cb commit 1cf519e

15 files changed

+463
-148
lines changed

CodeColors.xcodeproj/project.pbxproj

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
340B3CB42CBE47D6009C20B5 /* SyntaxSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 340B3CB32CBE47D6009C20B5 /* SyntaxSettings.swift */; };
11+
340B3CB62CBE4823009C20B5 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 340B3CB52CBE4823009C20B5 /* SettingsView.swift */; };
12+
340B3CB72CBE49FA009C20B5 /* SyntaxSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 340B3CB32CBE47D6009C20B5 /* SyntaxSettings.swift */; };
1013
34B4DB152CBCAD7E001F31C6 /* CodeColorsApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34B4DB142CBCAD7E001F31C6 /* CodeColorsApp.swift */; };
11-
34B4DB172CBCAD7E001F31C6 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34B4DB162CBCAD7E001F31C6 /* ContentView.swift */; };
1214
34B4DB192CBCAD7F001F31C6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 34B4DB182CBCAD7F001F31C6 /* Assets.xcassets */; };
1315
34B4DB1C2CBCAD7F001F31C6 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 34B4DB1B2CBCAD7F001F31C6 /* Preview Assets.xcassets */; };
1416
34B4DB2A2CBCADFD001F31C6 /* Quartz.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 34B4DB292CBCADFD001F31C6 /* Quartz.framework */; };
@@ -17,6 +19,8 @@
1719
34B4DB3E2CBCC5F3001F31C6 /* PreviewProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34B4DB2E2CBCADFD001F31C6 /* PreviewProvider.swift */; };
1820
34E745012CBE141900EB6061 /* SyntaxRules.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34E745002CBE141900EB6061 /* SyntaxRules.swift */; };
1921
34E745032CBE15D200EB6061 /* SyntaxHighlighter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34E745022CBE15D200EB6061 /* SyntaxHighlighter.swift */; };
22+
34E745052CBE2C4700EB6061 /* CodeContentLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34E745042CBE2C4700EB6061 /* CodeContentLoader.swift */; };
23+
34E745072CBE2C6D00EB6061 /* LineReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34E745062CBE2C6D00EB6061 /* LineReader.swift */; };
2024
/* End PBXBuildFile section */
2125

2226
/* Begin PBXContainerItemProxy section */
@@ -44,9 +48,10 @@
4448
/* End PBXCopyFilesBuildPhase section */
4549

4650
/* Begin PBXFileReference section */
51+
340B3CB32CBE47D6009C20B5 /* SyntaxSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyntaxSettings.swift; sourceTree = "<group>"; };
52+
340B3CB52CBE4823009C20B5 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
4753
34B4DB112CBCAD7E001F31C6 /* CodeColors.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CodeColors.app; sourceTree = BUILT_PRODUCTS_DIR; };
4854
34B4DB142CBCAD7E001F31C6 /* CodeColorsApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeColorsApp.swift; sourceTree = "<group>"; };
49-
34B4DB162CBCAD7E001F31C6 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
5055
34B4DB182CBCAD7F001F31C6 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
5156
34B4DB1B2CBCAD7F001F31C6 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
5257
34B4DB1D2CBCAD7F001F31C6 /* CodeColors.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CodeColors.entitlements; sourceTree = "<group>"; };
@@ -58,6 +63,8 @@
5863
34B4DB342CBCADFD001F31C6 /* QuickCodeColorLook.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = QuickCodeColorLook.entitlements; sourceTree = "<group>"; };
5964
34E745002CBE141900EB6061 /* SyntaxRules.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyntaxRules.swift; sourceTree = "<group>"; };
6065
34E745022CBE15D200EB6061 /* SyntaxHighlighter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyntaxHighlighter.swift; sourceTree = "<group>"; };
66+
34E745042CBE2C4700EB6061 /* CodeContentLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeContentLoader.swift; sourceTree = "<group>"; };
67+
34E745062CBE2C6D00EB6061 /* LineReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LineReader.swift; sourceTree = "<group>"; };
6168
/* End PBXFileReference section */
6269

6370
/* Begin PBXFrameworksBuildPhase section */
@@ -102,7 +109,8 @@
102109
isa = PBXGroup;
103110
children = (
104111
34B4DB142CBCAD7E001F31C6 /* CodeColorsApp.swift */,
105-
34B4DB162CBCAD7E001F31C6 /* ContentView.swift */,
112+
340B3CB32CBE47D6009C20B5 /* SyntaxSettings.swift */,
113+
340B3CB52CBE4823009C20B5 /* SettingsView.swift */,
106114
34B4DB182CBCAD7F001F31C6 /* Assets.xcassets */,
107115
34B4DB1D2CBCAD7F001F31C6 /* CodeColors.entitlements */,
108116
34B4DB1A2CBCAD7F001F31C6 /* Preview Content */,
@@ -129,6 +137,8 @@
129137
34B4DB2B2CBCADFD001F31C6 /* QuickCodeColorLook */ = {
130138
isa = PBXGroup;
131139
children = (
140+
34E745042CBE2C4700EB6061 /* CodeContentLoader.swift */,
141+
34E745062CBE2C6D00EB6061 /* LineReader.swift */,
132142
34B4DB2C2CBCADFD001F31C6 /* PreviewViewController.swift */,
133143
34B4DB2E2CBCADFD001F31C6 /* PreviewProvider.swift */,
134144
34E745002CBE141900EB6061 /* SyntaxRules.swift */,
@@ -239,7 +249,8 @@
239249
isa = PBXSourcesBuildPhase;
240250
buildActionMask = 2147483647;
241251
files = (
242-
34B4DB172CBCAD7E001F31C6 /* ContentView.swift in Sources */,
252+
340B3CB42CBE47D6009C20B5 /* SyntaxSettings.swift in Sources */,
253+
340B3CB62CBE4823009C20B5 /* SettingsView.swift in Sources */,
243254
34B4DB152CBCAD7E001F31C6 /* CodeColorsApp.swift in Sources */,
244255
);
245256
runOnlyForDeploymentPostprocessing = 0;
@@ -252,6 +263,9 @@
252263
34E745012CBE141900EB6061 /* SyntaxRules.swift in Sources */,
253264
34B4DB3D2CBCC51D001F31C6 /* PreviewViewController.swift in Sources */,
254265
34B4DB3E2CBCC5F3001F31C6 /* PreviewProvider.swift in Sources */,
266+
34E745072CBE2C6D00EB6061 /* LineReader.swift in Sources */,
267+
34E745052CBE2C4700EB6061 /* CodeContentLoader.swift in Sources */,
268+
340B3CB72CBE49FA009C20B5 /* SyntaxSettings.swift in Sources */,
255269
);
256270
runOnlyForDeploymentPostprocessing = 0;
257271
};
@@ -399,6 +413,7 @@
399413
ENABLE_HARDENED_RUNTIME = YES;
400414
ENABLE_PREVIEWS = YES;
401415
GENERATE_INFOPLIST_FILE = YES;
416+
INFOPLIST_KEY_CFBundleDisplayName = "Code Colors";
402417
INFOPLIST_KEY_NSHumanReadableCopyright = "";
403418
LD_RUNPATH_SEARCH_PATHS = (
404419
"$(inherited)",
@@ -427,6 +442,7 @@
427442
ENABLE_HARDENED_RUNTIME = YES;
428443
ENABLE_PREVIEWS = YES;
429444
GENERATE_INFOPLIST_FILE = YES;
445+
INFOPLIST_KEY_CFBundleDisplayName = "Code Colors";
430446
INFOPLIST_KEY_NSHumanReadableCopyright = "";
431447
LD_RUNPATH_SEARCH_PATHS = (
432448
"$(inherited)",

CodeColors.xcodeproj/xcuserdata/ssz.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,66 @@
33
uuid = "60919A6F-13ED-4CF9-8607-2F6AA9EC1D1D"
44
type = "1"
55
version = "2.0">
6+
<Breakpoints>
7+
<BreakpointProxy
8+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
9+
<BreakpointContent
10+
uuid = "3C42B69D-4F10-4EFA-962E-AE6A10F5F046"
11+
shouldBeEnabled = "Yes"
12+
ignoreCount = "0"
13+
continueAfterRunningActions = "No"
14+
filePath = "QuickCodeColorLook/PreviewProvider.swift"
15+
startingColumnNumber = "9223372036854775807"
16+
endingColumnNumber = "9223372036854775807"
17+
startingLineNumber = "6"
18+
endingLineNumber = "6"
19+
landmarkName = "PreviewProvider"
20+
landmarkType = "3">
21+
<Locations>
22+
<Location
23+
uuid = "3C42B69D-4F10-4EFA-962E-AE6A10F5F046 - 3c2fcbce1918509b"
24+
shouldBeEnabled = "Yes"
25+
ignoreCount = "0"
26+
continueAfterRunningActions = "No"
27+
symbolName = "QuickCodeColorLook.PreviewProvider.__allocating_init() -&gt; QuickCodeColorLook.PreviewProvider"
28+
moduleName = "QuickCodeColorLook"
29+
usesParentBreakpointCondition = "Yes"
30+
urlString = "file:///Users/ssz/Documents/CodeColors/QuickCodeColorLook/PreviewProvider.swift"
31+
startingColumnNumber = "9223372036854775807"
32+
endingColumnNumber = "9223372036854775807"
33+
startingLineNumber = "0"
34+
endingLineNumber = "0">
35+
</Location>
36+
<Location
37+
uuid = "3C42B69D-4F10-4EFA-962E-AE6A10F5F046 - 61e72b3e7660e087"
38+
shouldBeEnabled = "Yes"
39+
ignoreCount = "0"
40+
continueAfterRunningActions = "No"
41+
symbolName = "QuickCodeColorLook.PreviewProvider.init() -&gt; QuickCodeColorLook.PreviewProvider"
42+
moduleName = "QuickCodeColorLook"
43+
usesParentBreakpointCondition = "Yes"
44+
urlString = "file:///Users/ssz/Documents/CodeColors/%3Ccompiler-generated%3E"
45+
startingColumnNumber = "9223372036854775807"
46+
endingColumnNumber = "9223372036854775807"
47+
startingLineNumber = "0"
48+
endingLineNumber = "0">
49+
</Location>
50+
<Location
51+
uuid = "3C42B69D-4F10-4EFA-962E-AE6A10F5F046 - 41f0be72b60e8e96"
52+
shouldBeEnabled = "Yes"
53+
ignoreCount = "0"
54+
continueAfterRunningActions = "No"
55+
symbolName = "QuickCodeColorLook.PreviewProvider.__deallocating_deinit"
56+
moduleName = "QuickCodeColorLook"
57+
usesParentBreakpointCondition = "Yes"
58+
urlString = "file:///Users/ssz/Documents/CodeColors/QuickCodeColorLook/PreviewProvider.swift"
59+
startingColumnNumber = "9223372036854775807"
60+
endingColumnNumber = "9223372036854775807"
61+
startingLineNumber = "0"
62+
endingLineNumber = "0">
63+
</Location>
64+
</Locations>
65+
</BreakpointContent>
66+
</BreakpointProxy>
67+
</Breakpoints>
668
</Bucket>

CodeColors/CodeColors.entitlements

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
66
<true/>
7-
<key>com.apple.security.files.user-selected.read-only</key>
7+
<key>com.apple.security.application-groups</key>
8+
<array>
9+
<string>$(TeamIdentifierPrefix)</string>
10+
</array>
11+
<key>com.apple.security.files.user-selected.read-write</key>
812
<true/>
913
</dict>
1014
</plist>

CodeColors/CodeColorsApp.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//
22
// CodeColorsApp.swift
3-
// CodeColors
3+
// CodeColors: Syntax Highlighting
44
//
55
// Created by Almahdi Morris on 13/10/24.
66
//
@@ -11,7 +11,7 @@ import SwiftUI
1111
struct CodeColorsApp: App {
1212
var body: some Scene {
1313
WindowGroup {
14-
ContentView()
14+
SettingsView()
1515
}
1616
}
1717
}

CodeColors/ContentView.swift

Lines changed: 0 additions & 20 deletions
This file was deleted.

CodeColors/SettingsView.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import SwiftUI
2+
3+
struct SettingsView: View {
4+
@ObservedObject var settings = SyntaxSettings.shared
5+
6+
var body: some View {
7+
Form {
8+
ForEach(settings.syntaxColors.keys.sorted(), id: \.self) { key in
9+
HStack {
10+
Text(key.capitalized)
11+
Spacer()
12+
ColorPicker("", selection: Binding(
13+
get: { settings.syntaxColors[key] ?? .black },
14+
set: { settings.syntaxColors[key] = $0 }
15+
))
16+
.labelsHidden()
17+
}
18+
}
19+
HStack {
20+
Button("Reset to Default") {
21+
settings.resetToDefault()
22+
}
23+
Spacer()
24+
Button("Save") {
25+
settings.saveSettings()
26+
}
27+
}
28+
}
29+
.padding()
30+
.frame(width: 400)
31+
}
32+
}

CodeColors/SyntaxSettings.swift

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import SwiftUI
2+
import Combine
3+
4+
class SyntaxSettings: ObservableObject {
5+
static let shared = SyntaxSettings()
6+
private let userDefaults = UserDefaults(suiteName: "com.oil3.codecolors")
7+
8+
@Published var syntaxColors: [String: Color] = [:]
9+
10+
private init() {
11+
loadSettings()
12+
}
13+
14+
func loadSettings() {
15+
if let data = userDefaults?.data(forKey: "syntaxColors"),
16+
let savedColors = try? JSONDecoder().decode([String: CodableColor].self, from: data) {
17+
self.syntaxColors = savedColors.mapValues { $0.color }
18+
} else {
19+
// Load default colors
20+
syntaxColors = SyntaxSettings.defaultColors
21+
}
22+
}
23+
24+
func saveSettings() {
25+
let codableColors = syntaxColors.mapValues { CodableColor(color: $0) }
26+
if let data = try? JSONEncoder().encode(codableColors) {
27+
userDefaults?.set(data, forKey: "syntaxColors")
28+
}
29+
}
30+
31+
func resetToDefault() {
32+
syntaxColors = SyntaxSettings.defaultColors
33+
saveSettings()
34+
}
35+
36+
static let defaultColors: [String: Color] = [
37+
"string": .teal,
38+
"number": .orange,
39+
"boolean": .blue,
40+
"keyword": .purple,
41+
"comment": .green,
42+
// need add more default colors
43+
]
44+
}
45+
46+
struct CodableColor: Codable {
47+
let color: Color
48+
49+
enum CodingKeys: CodingKey {
50+
case red, green, blue, opacity
51+
}
52+
53+
init(color: Color) {
54+
let components = color.components
55+
self.color = Color(red: components.red, green: components.green, blue: components.blue, opacity: components.opacity)
56+
}
57+
58+
init(from decoder: Decoder) throws {
59+
let container = try decoder.container(keyedBy: CodingKeys.self)
60+
let red = try container.decode(Double.self, forKey: .red)
61+
let green = try container.decode(Double.self, forKey: .green)
62+
let blue = try container.decode(Double.self, forKey: .blue)
63+
let opacity = try container.decode(Double.self, forKey: .opacity)
64+
color = Color(red: red, green: green, blue: blue, opacity: opacity)
65+
}
66+
67+
func encode(to encoder: Encoder) throws {
68+
var container = encoder.container(keyedBy: CodingKeys.self)
69+
let components = color.components
70+
try container.encode(components.red, forKey: .red)
71+
try container.encode(components.green, forKey: .green)
72+
try container.encode(components.blue, forKey: .blue)
73+
try container.encode(components.opacity, forKey: .opacity)
74+
}
75+
}
76+
77+
extension Color {
78+
var components: (red: Double, green: Double, blue: Double, opacity: Double) {
79+
#if os(macOS)
80+
let nsColor = NSColor(self)
81+
var red: CGFloat = 0
82+
var green: CGFloat = 0
83+
var blue: CGFloat = 0
84+
var opacity: CGFloat = 0
85+
nsColor.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
86+
return (Double(red), Double(green), Double(blue), Double(opacity))
87+
#else
88+
// iOS placeholder
89+
#endif
90+
}
91+
}

0 commit comments

Comments
 (0)