Skip to content

Commit 1c9384e

Browse files
committed
Add Splash back because there is no other quick and easy solution
1 parent 4201df1 commit 1c9384e

4 files changed

Lines changed: 94 additions & 27 deletions

File tree

Copilot for Xcode.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let package = Package(
3535
.package(url: "https://github.com/ChimeHQ/LanguageClient", from: "0.3.1"),
3636
.package(url: "https://github.com/apple/swift-async-algorithms", from: "0.1.0"),
3737
.package(url: "https://github.com/raspu/Highlightr", from: "2.1.0"),
38-
.package(url: "https://github.com/JohnSundell/Splash", from: "0.1.0"),
38+
.package(url: "https://github.com/JohnSundell/Splash", branch: "master"),
3939
.package(url: "https://github.com/nmdias/FeedKit", from: "9.1.2"),
4040
.package(url: "https://github.com/intitni/swift-markdown-ui", branch: "main"),
4141
.package(url: "https://github.com/sparkle-project/Sparkle", from: "2.0.0"),

Core/Sources/SuggestionWidget/SyntaxHighlighting.swift

Lines changed: 90 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import AppKit
22
import Foundation
33
import Highlightr
4+
import Splash
45
import SwiftUI
56
import XPCShared
67

@@ -10,31 +11,97 @@ func highlightedCodeBlock(
1011
brightMode: Bool,
1112
fontSize: Double
1213
) -> NSAttributedString {
13-
var language = language
14-
if language == "objective-c" {
15-
language = "objectivec"
16-
}
17-
func unhighlightedCode() -> NSAttributedString {
18-
return NSAttributedString(
19-
string: code,
20-
attributes: [
21-
.foregroundColor: brightMode ? NSColor.black : NSColor.white,
22-
.font: NSFont.monospacedSystemFont(ofSize: fontSize, weight: .regular),
23-
]
14+
switch language {
15+
case "swift":
16+
let plainTextColor = brightMode
17+
? .black
18+
: #colorLiteral(red: 0.6509803922, green: 0.6980392157, blue: 0.7529411765, alpha: 1)
19+
let highlighter =
20+
SyntaxHighlighter(
21+
format: AttributedStringOutputFormat(theme: .init(
22+
font: .init(size: 14),
23+
plainTextColor: plainTextColor,
24+
tokenColors: brightMode
25+
? [
26+
.keyword: #colorLiteral(red: 0.6078431373, green: 0.137254902, blue: 0.5764705882, alpha: 1),
27+
.string: #colorLiteral(red: 0.1371159852, green: 0.3430536985, blue: 0.362406373, alpha: 1),
28+
.type: #colorLiteral(red: 0.2456904352, green: 0.5002114773, blue: 0.5297455192, alpha: 1),
29+
.call: #colorLiteral(red: 0.1960784314, green: 0.4274509804, blue: 0.4549019608, alpha: 1),
30+
.number: #colorLiteral(red: 0.4385872483, green: 0.4995297194, blue: 0.5483990908, alpha: 1),
31+
.comment: #colorLiteral(red: 0.3647058824, green: 0.4235294118, blue: 0.4745098039, alpha: 1),
32+
.property: #colorLiteral(red: 0.1960784314, green: 0.4274509804, blue: 0.4549019608, alpha: 1),
33+
.dotAccess: #colorLiteral(red: 0.1960784314, green: 0.4274509804, blue: 0.4549019608, alpha: 1),
34+
.preprocessing: #colorLiteral(red: 0.3921568627, green: 0.2196078431, blue: 0.1254901961, alpha: 1),
35+
] : [
36+
.keyword: #colorLiteral(red: 0.8258609176, green: 0.5708742738, blue: 0.8922662139, alpha: 1),
37+
.string: #colorLiteral(red: 0.6253595352, green: 0.7963448763, blue: 0.5427476764, alpha: 1),
38+
.type: #colorLiteral(red: 0.9221783876, green: 0.7978314757, blue: 0.5575165749, alpha: 1),
39+
.call: #colorLiteral(red: 0.4466812611, green: 0.742190659, blue: 0.9515134692, alpha: 1),
40+
.number: #colorLiteral(red: 0.8620631099, green: 0.6468816996, blue: 0.4395158887, alpha: 1),
41+
.comment: #colorLiteral(red: 0.4233166873, green: 0.4612616301, blue: 0.5093258619, alpha: 1),
42+
.property: #colorLiteral(red: 0.906378448, green: 0.5044228435, blue: 0.5263597369, alpha: 1),
43+
.dotAccess: #colorLiteral(red: 0.906378448, green: 0.5044228435, blue: 0.5263597369, alpha: 1),
44+
.preprocessing: #colorLiteral(red: 0.3776347041, green: 0.8792117238, blue: 0.4709561467, alpha: 1),
45+
]
46+
))
47+
)
48+
let formatted = NSMutableAttributedString(attributedString: highlighter.highlight(code))
49+
formatted.addAttributes(
50+
[.font: NSFont.monospacedSystemFont(ofSize: fontSize, weight: .regular)],
51+
range: NSRange(location: 0, length: formatted.length)
2452
)
53+
func leadingSpacesInCode(_ code: String) -> Int {
54+
var leadingSpaces = 0
55+
for char in code {
56+
if char == " " {
57+
leadingSpaces += 1
58+
} else {
59+
break
60+
}
61+
}
62+
return leadingSpaces
63+
}
64+
65+
// Workaround: Splash has a bug that will insert an extra space at the beginning.
66+
let leadingSpaces = leadingSpacesInCode(code)
67+
let leadingSpacesFormatted = leadingSpacesInCode(formatted.string)
68+
let diff = leadingSpacesFormatted - leadingSpaces
69+
if diff > 0 {
70+
formatted.mutableString.replaceCharacters(
71+
in: .init(location: 0, length: diff),
72+
with: ""
73+
)
74+
}
75+
// End of workaround.
76+
77+
return formatted
78+
default:
79+
var language = language
80+
if language == "objective-c" {
81+
language = "objectivec"
82+
}
83+
func unhighlightedCode() -> NSAttributedString {
84+
return NSAttributedString(
85+
string: code,
86+
attributes: [
87+
.foregroundColor: brightMode ? NSColor.black : NSColor.white,
88+
.font: NSFont.monospacedSystemFont(ofSize: fontSize, weight: .regular),
89+
]
90+
)
91+
}
92+
guard let highlighter = Highlightr() else {
93+
return unhighlightedCode()
94+
}
95+
highlighter.setTheme(to: brightMode ? "xcode" : "atom-one-dark")
96+
highlighter.theme.setCodeFont(.monospacedSystemFont(ofSize: fontSize, weight: .regular))
97+
guard let formatted = highlighter.highlight(code, as: language) else {
98+
return unhighlightedCode()
99+
}
100+
if formatted.string == "undefined" {
101+
return unhighlightedCode()
102+
}
103+
return formatted
25104
}
26-
guard let highlighter = Highlightr() else {
27-
return unhighlightedCode()
28-
}
29-
highlighter.setTheme(to: brightMode ? "xcode" : "atom-one-dark")
30-
highlighter.theme.setCodeFont(.monospacedSystemFont(ofSize: fontSize, weight: .regular))
31-
guard let formatted = highlighter.highlight(code, as: language) else {
32-
return unhighlightedCode()
33-
}
34-
if formatted.string == "undefined" {
35-
return unhighlightedCode()
36-
}
37-
return formatted
38105
}
39106

40107
func highlighted(

Core/Tests/SuggestionWidgetTests/ConvertToCodeLinesTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class ConvertToCodeLinesTests: XCTestCase {
1010
"""
1111
let (result, spaceCount) = highlighted(
1212
code: code,
13-
language: "md",
13+
language: "swift",
1414
brightMode: true,
1515
droppingLeadingSpaces: false
1616
)

0 commit comments

Comments
 (0)