Skip to content

Commit d16d1c7

Browse files
committed
Pass scenario to highlighter
1 parent 5b88887 commit d16d1c7

8 files changed

Lines changed: 32 additions & 7 deletions

File tree

Core/Sources/ChatGPTChatTab/ChatPanel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ struct ChatCodeSyntaxHighlighter: CodeSyntaxHighlighter {
544544
let content = highlightedCodeBlock(
545545
code: content,
546546
language: language ?? "",
547+
scenario: "chat",
547548
brightMode: brightMode,
548549
fontSize: fontSize
549550
)

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ struct CodeBlockSuggestionPanel: View {
100100
CodeBlock(
101101
code: suggestion.code,
102102
language: suggestion.language,
103-
startLineIndex: suggestion.startLineIndex,
103+
startLineIndex: suggestion.startLineIndex,
104+
scenario: "suggestion",
104105
colorScheme: colorScheme,
105106
fontSize: fontSize,
106107
droppingLeadingSpaces: hideCommonPrecedingSpaces

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ extension PromptToCodePanel {
275275
CodeBlock(
276276
code: viewStore.state.code,
277277
language: viewStore.state.language,
278-
startLineIndex: viewStore.state.startLineIndex,
278+
startLineIndex: viewStore.state.startLineIndex,
279+
scenario: "promptToCode",
279280
colorScheme: colorScheme,
280281
firstLinePrecedingSpaceCount: viewStore.state
281282
.firstLinePrecedingSpaceCount,

Pro

Submodule Pro updated from dd5f6ce to 7c3e41f

Tool/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ let package = Package(
5757
.package(url: "https://github.com/ChimeHQ/JSONRPC", exact: "0.6.0"),
5858
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.6.0"),
5959
.package(url: "https://github.com/unum-cloud/usearch", from: "0.19.1"),
60-
.package(url: "https://github.com/intitni/Highlightr", branch: "bump-highlight-js-version"),
60+
.package(url: "https://github.com/intitni/Highlightr", branch: "master"),
6161
.package(
6262
url: "https://github.com/pointfreeco/swift-composable-architecture",
6363
from: "0.55.0"

Tool/Sources/SharedUIComponents/CodeBlock.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public struct CodeBlock: View {
44
public let code: String
55
public let language: String
66
public let startLineIndex: Int
7+
public let scenario: String
78
public let colorScheme: ColorScheme
89
public let commonPrecedingSpaceCount: Int
910
public let highlightedCode: [NSAttributedString]
@@ -15,6 +16,7 @@ public struct CodeBlock: View {
1516
code: String,
1617
language: String,
1718
startLineIndex: Int,
19+
scenario: String,
1820
colorScheme: ColorScheme,
1921
firstLinePrecedingSpaceCount: Int = 0,
2022
fontSize: Double,
@@ -23,6 +25,7 @@ public struct CodeBlock: View {
2325
self.code = code
2426
self.language = language
2527
self.startLineIndex = startLineIndex
28+
self.scenario = scenario
2629
self.colorScheme = colorScheme
2730
self.droppingLeadingSpaces = droppingLeadingSpaces
2831
self.firstLinePrecedingSpaceCount = firstLinePrecedingSpaceCount
@@ -33,6 +36,7 @@ public struct CodeBlock: View {
3336
let result = Self.highlight(
3437
code: padding + code,
3538
language: language,
39+
scenario: scenario,
3640
colorScheme: colorScheme,
3741
fontSize: fontSize,
3842
droppingLeadingSpaces: droppingLeadingSpaces
@@ -75,13 +79,15 @@ public struct CodeBlock: View {
7579
static func highlight(
7680
code: String,
7781
language: String,
82+
scenario: String,
7883
colorScheme: ColorScheme,
7984
fontSize: Double,
8085
droppingLeadingSpaces: Bool
8186
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
8287
return highlighted(
8388
code: code,
8489
language: language,
90+
scenario: scenario,
8591
brightMode: colorScheme != .dark,
8692
droppingLeadingSpaces: droppingLeadingSpaces,
8793
fontSize: fontSize
@@ -100,6 +106,7 @@ struct CodeBlock_Previews: PreviewProvider {
100106
""",
101107
language: "swift",
102108
startLineIndex: 0,
109+
scenario: "",
103110
colorScheme: .dark,
104111
firstLinePrecedingSpaceCount: 0,
105112
fontSize: 12,

Tool/Sources/SharedUIComponents/Experiment/NewCodeBlock.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct _CodeBlock: View {
1313
let highlightedCode: AttributedString
1414
let colorScheme: ColorScheme
1515
let droppingLeadingSpaces: Bool
16+
let scenario: String
1617

1718
/// Create a text edit view with a certain text that uses a certain options.
1819
/// - Parameters:
@@ -23,6 +24,7 @@ struct _CodeBlock: View {
2324
code: String,
2425
language: String,
2526
firstLinePrecedingSpaceCount: Int,
27+
scenario: String,
2628
colorScheme: ColorScheme,
2729
fontSize: Double,
2830
droppingLeadingSpaces: Bool,
@@ -32,13 +34,15 @@ struct _CodeBlock: View {
3234
self.fontSize = fontSize
3335
self.colorScheme = colorScheme
3436
self.droppingLeadingSpaces = droppingLeadingSpaces
37+
self.scenario = scenario
3538

3639
let padding = firstLinePrecedingSpaceCount > 0
3740
? String(repeating: " ", count: firstLinePrecedingSpaceCount)
3841
: ""
3942
let result = Self.highlight(
4043
code: padding + code,
4144
language: language,
45+
scenario: scenario,
4246
colorScheme: colorScheme,
4347
fontSize: fontSize,
4448
droppingLeadingSpaces: droppingLeadingSpaces
@@ -68,13 +72,15 @@ struct _CodeBlock: View {
6872
static func highlight(
6973
code: String,
7074
language: String,
75+
scenario: String,
7176
colorScheme: ColorScheme,
7277
fontSize: Double,
7378
droppingLeadingSpaces: Bool
7479
) -> (code: AttributedString, commonLeadingSpaceCount: Int) {
7580
let (lines, commonLeadingSpaceCount) = highlighted(
7681
code: code,
77-
language: language,
82+
language: language,
83+
scenario: scenario,
7884
brightMode: colorScheme != .dark,
7985
droppingLeadingSpaces: droppingLeadingSpaces,
8086
fontSize: fontSize,

Tool/Sources/SharedUIComponents/SyntaxHighlighting.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import SwiftUI
77
public func highlightedCodeBlock(
88
code: String,
99
language: String,
10+
scenario: String,
1011
brightMode: Bool,
1112
fontSize: Double
1213
) -> NSAttributedString {
@@ -27,7 +28,13 @@ public func highlightedCodeBlock(
2728
guard let highlighter = Highlightr() else {
2829
return unhighlightedCode()
2930
}
30-
highlighter.setTheme(to: brightMode ? "xcode" : "atom-one-dark")
31+
highlighter.setTheme(to: {
32+
let mode = brightMode ? "light" : "dark"
33+
if scenario.isEmpty {
34+
return mode
35+
}
36+
return "\(scenario)-\(mode)"
37+
}())
3138
highlighter.theme.setCodeFont(.monospacedSystemFont(ofSize: fontSize, weight: .regular))
3239
guard let formatted = highlighter.highlight(code, as: language) else {
3340
return unhighlightedCode()
@@ -41,14 +48,16 @@ public func highlightedCodeBlock(
4148
public func highlighted(
4249
code: String,
4350
language: String,
51+
scenario: String,
4452
brightMode: Bool,
4553
droppingLeadingSpaces: Bool,
4654
fontSize: Double,
4755
replaceSpacesWithMiddleDots: Bool = true
4856
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
4957
let formatted = highlightedCodeBlock(
5058
code: code,
51-
language: language,
59+
language: language,
60+
scenario: scenario,
5261
brightMode: brightMode,
5362
fontSize: fontSize
5463
)

0 commit comments

Comments
 (0)