Skip to content

Commit f94cf3e

Browse files
committed
Support disabling code wrapping in prompt to code
1 parent 848dd24 commit f94cf3e

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

Core/Sources/HostApp/FeatureSettings/PromptToCodeSettingsView.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct PromptToCodeSettingsView: View {
2020
var promptToCodeChatModelId
2121
@AppStorage(\.promptToCodeEmbeddingModelId)
2222
var promptToCodeEmbeddingModelId
23-
23+
@AppStorage(\.wrapCodeInPromptToCode) var wrapCode
2424
@AppStorage(\.chatModels) var chatModels
2525
@AppStorage(\.embeddingModels) var embeddingModels
2626
init() {}
@@ -90,6 +90,10 @@ struct PromptToCodeSettingsView: View {
9090
Toggle(isOn: $settings.hideCommonPrecedingSpaces) {
9191
Text("Hide Common Preceding Spaces")
9292
}
93+
94+
Toggle(isOn: $settings.wrapCode) {
95+
Text("Wrap code")
96+
}
9397

9498
#if canImport(ProHostApp)
9599

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ extension PromptToCodePanel {
209209
@AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark
210210
@AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight
211211
@AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark
212+
@AppStorage(\.wrapCodeInPromptToCode) var wrapCode
212213

213214
struct CodeContent: Equatable {
214215
var code: String
@@ -241,11 +242,11 @@ extension PromptToCodePanel {
241242
return color
242243
}
243244
}
244-
return Color.clear
245+
return Color.contentBackground
245246
}
246247

247248
var body: some View {
248-
CustomScrollView {
249+
ScrollView {
249250
VStack(spacing: 0) {
250251
Spacer(minLength: 60)
251252

@@ -304,25 +305,41 @@ extension PromptToCodePanel {
304305
.frame(maxWidth: .infinity)
305306
.scaleEffect(x: 1, y: -1, anchor: .center)
306307
} else {
307-
CodeBlock(
308-
code: viewStore.state.code,
309-
language: viewStore.state.language,
310-
startLineIndex: viewStore.state.startLineIndex,
311-
scenario: "promptToCode",
312-
colorScheme: colorScheme,
313-
firstLinePrecedingSpaceCount: viewStore.state
314-
.firstLinePrecedingSpaceCount,
315-
font: codeFont.value.nsFont,
316-
droppingLeadingSpaces: hideCommonPrecedingSpaces,
317-
proposedForegroundColor:codeForegroundColor
318-
)
319-
.frame(maxWidth: .infinity)
320-
.scaleEffect(x: 1, y: -1, anchor: .center)
308+
if wrapCode {
309+
codeBlock(viewStore.state)
310+
} else {
311+
ScrollView(.horizontal) {
312+
codeBlock(viewStore.state)
313+
}
314+
.modify {
315+
if #available(macOS 13.0, *) {
316+
$0.scrollIndicators(.hidden)
317+
} else {
318+
$0
319+
}
320+
}
321+
}
321322
}
322323
}
323324
}
324-
.background(codeBackgroundColor)
325325
}
326+
.background(codeBackgroundColor)
327+
.scaleEffect(x: 1, y: -1, anchor: .center)
328+
}
329+
330+
func codeBlock(_ state: CodeContent) -> some View {
331+
CodeBlock(
332+
code: state.code,
333+
language: state.language,
334+
startLineIndex: state.startLineIndex,
335+
scenario: "promptToCode",
336+
colorScheme: colorScheme,
337+
firstLinePrecedingSpaceCount: state.firstLinePrecedingSpaceCount,
338+
font: codeFont.value.nsFont,
339+
droppingLeadingSpaces: hideCommonPrecedingSpaces,
340+
proposedForegroundColor:codeForegroundColor
341+
)
342+
.frame(maxWidth: .infinity)
326343
.scaleEffect(x: 1, y: -1, anchor: .center)
327344
}
328345
}

Tool/Sources/Preferences/Keys.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ public extension UserDefaultPreferenceKeys {
302302
var hideCommonPrecedingSpacesInPromptToCode: PreferenceKey<Bool> {
303303
.init(defaultValue: true, key: "HideCommonPrecedingSpacesInPromptToCode")
304304
}
305+
306+
var wrapCodeInPromptToCode: PreferenceKey<Bool> {
307+
.init(defaultValue: false, key: "WrapCodeInPromptToCode")
308+
}
305309
}
306310

307311
// MARK: - Suggestion

Tool/Sources/SharedUIComponents/CodeBlock.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public struct CodeBlock: View {
1313
public let font: NSFont
1414
public let droppingLeadingSpaces: Bool
1515
public let proposedForegroundColor: Color?
16+
public let wrapCode: Bool
1617

1718
public init(
1819
code: String,
@@ -23,7 +24,8 @@ public struct CodeBlock: View {
2324
firstLinePrecedingSpaceCount: Int = 0,
2425
font: NSFont,
2526
droppingLeadingSpaces: Bool,
26-
proposedForegroundColor: Color?
27+
proposedForegroundColor: Color?,
28+
wrapCode: Bool = true
2729
) {
2830
self.code = code
2931
self.language = language
@@ -34,6 +36,7 @@ public struct CodeBlock: View {
3436
self.firstLinePrecedingSpaceCount = firstLinePrecedingSpaceCount
3537
self.font = font
3638
self.proposedForegroundColor = proposedForegroundColor
39+
self.wrapCode = wrapCode
3740
let padding = firstLinePrecedingSpaceCount > 0
3841
? String(repeating: " ", count: firstLinePrecedingSpaceCount)
3942
: ""

0 commit comments

Comments
 (0)