Skip to content

Commit d763796

Browse files
committed
Allow unwrapping code in code block
1 parent e5b3d8f commit d763796

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

Core/Sources/ChatGPTChatTab/Styles.swift

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,20 @@ extension NSAppearance {
3333
}
3434
}
3535

36-
extension MarkdownUI.Theme {
37-
static func custom(fontSize: Double) -> MarkdownUI.Theme {
38-
.gitHub.text {
39-
ForegroundColor(.primary)
40-
BackgroundColor(Color.clear)
41-
FontSize(fontSize)
42-
}
43-
.codeBlock { configuration in
44-
ScrollView(.horizontal) {
45-
configuration.label
46-
.relativeLineSpacing(.em(0.225))
47-
.markdownTextStyle {
48-
FontFamilyVariant(.monospaced)
49-
FontSize(.em(0.85))
50-
}
51-
.padding(16)
52-
.padding(.top, 14)
36+
extension View {
37+
func codeBlockLabelStyle() -> some View {
38+
self
39+
.relativeLineSpacing(.em(0.225))
40+
.markdownTextStyle {
41+
FontFamilyVariant(.monospaced)
42+
FontSize(.em(0.85))
5343
}
54-
.workaroundForVerticalScrollingBugInMacOS()
44+
.padding(16)
45+
.padding(.top, 14)
46+
}
47+
48+
func codeBlockStyle(_ configuration: CodeBlockConfiguration) -> some View {
49+
self
5550
.background(Color(nsColor: .textBackgroundColor).opacity(0.7))
5651
.clipShape(RoundedRectangle(cornerRadius: 6))
5752
.overlay(alignment: .top) {
@@ -69,6 +64,31 @@ extension MarkdownUI.Theme {
6964
}
7065
}
7166
.markdownMargin(top: 4, bottom: 16)
67+
}
68+
}
69+
70+
extension MarkdownUI.Theme {
71+
static func custom(fontSize: Double) -> MarkdownUI.Theme {
72+
.gitHub.text {
73+
ForegroundColor(.primary)
74+
BackgroundColor(Color.clear)
75+
FontSize(fontSize)
76+
}
77+
.codeBlock { configuration in
78+
let wrapCode = UserDefaults.shared.value(for: \.wrapCodeInChatCodeBlock)
79+
80+
if wrapCode {
81+
configuration.label
82+
.codeBlockLabelStyle()
83+
.codeBlockStyle(configuration)
84+
} else {
85+
ScrollView(.horizontal) {
86+
configuration.label
87+
.codeBlockLabelStyle()
88+
}
89+
.workaroundForVerticalScrollingBugInMacOS()
90+
.codeBlockStyle(configuration)
91+
}
7292
}
7393
}
7494

Core/Sources/HostApp/FeatureSettings/ChatSettingsView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct ChatSettingsView: View {
1919
@AppStorage(\.defaultChatFeatureEmbeddingModelId) var defaultChatFeatureEmbeddingModelId
2020
@AppStorage(\.chatModels) var chatModels
2121
@AppStorage(\.embeddingModels) var embeddingModels
22+
@AppStorage(\.wrapCodeInChatCodeBlock) var wrapCodeInCodeBlock
2223

2324
init() {}
2425
}
@@ -159,6 +160,10 @@ struct ChatSettingsView: View {
159160

160161
Text("pt")
161162
}
163+
164+
Toggle(isOn: $settings.wrapCodeInCodeBlock) {
165+
Text("Wrap code in code block")
166+
}
162167
}
163168
}
164169

Tool/Sources/Preferences/Keys.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ public extension UserDefaultPreferenceKeys {
384384
var chatSearchPluginMaxIterations: PreferenceKey<Int> {
385385
.init(defaultValue: 3, key: "ChatSearchPluginMaxIterations")
386386
}
387+
388+
var wrapCodeInChatCodeBlock: PreferenceKey<Bool> {
389+
.init(defaultValue: true, key: "WrapCodeInChatCodeBlock")
390+
}
387391
}
388392

389393
// MARK: - Bing Search

0 commit comments

Comments
 (0)