Skip to content

Commit 5dd8ee0

Browse files
committed
Update CodeBlock to accept droppingLeadingSpaces in init
1 parent c4a6c02 commit 5dd8ee0

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct CodeBlockSuggestionPanel: View {
77
@AppStorage(\.suggestionCodeFontSize) var fontSize
88
@AppStorage(\.suggestionDisplayCompactMode) var suggestionDisplayCompactMode
99
@AppStorage(\.suggestionPresentationMode) var suggestionPresentationMode
10+
@AppStorage(\.hideCommonPrecedingSpacesInSuggestion) var hideCommonPrecedingSpaces
1011

1112
struct ToolBar: View {
1213
@ObservedObject var suggestion: CodeSuggestionProvider
@@ -101,7 +102,8 @@ struct CodeBlockSuggestionPanel: View {
101102
language: suggestion.language,
102103
startLineIndex: suggestion.startLineIndex,
103104
colorScheme: colorScheme,
104-
fontSize: fontSize
105+
fontSize: fontSize,
106+
droppingLeadingSpaces: hideCommonPrecedingSpaces
105107
)
106108
.frame(maxWidth: .infinity)
107109
}

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ extension PromptToCodePanel {
202202
struct Content: View {
203203
let store: StoreOf<PromptToCode>
204204
@Environment(\.colorScheme) var colorScheme
205-
@AppStorage(\.suggestionCodeFontSize) var fontSize
205+
@AppStorage(\.promptToCodeCodeFontSize) var fontSize
206+
@AppStorage(\.hideCommonPrecedingSpacesInPromptToCode) var hideCommonPrecedingSpaces
206207

207208
struct CodeContent: Equatable {
208209
var code: String
@@ -278,7 +279,8 @@ extension PromptToCodePanel {
278279
colorScheme: colorScheme,
279280
firstLinePrecedingSpaceCount: viewStore.state
280281
.firstLinePrecedingSpaceCount,
281-
fontSize: fontSize
282+
fontSize: fontSize,
283+
droppingLeadingSpaces: hideCommonPrecedingSpaces
282284
)
283285
.frame(maxWidth: .infinity)
284286
.scaleEffect(x: 1, y: -1, anchor: .center)

Tool/Sources/SharedUIComponents/CodeBlock.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ public struct CodeBlock: View {
99
public let highlightedCode: [NSAttributedString]
1010
public let firstLinePrecedingSpaceCount: Int
1111
public let fontSize: Double
12+
public let droppingLeadingSpaces: Bool
1213

1314
public init(
1415
code: String,
1516
language: String,
1617
startLineIndex: Int,
1718
colorScheme: ColorScheme,
1819
firstLinePrecedingSpaceCount: Int = 0,
19-
fontSize: Double
20+
fontSize: Double,
21+
droppingLeadingSpaces: Bool
2022
) {
2123
self.code = code
2224
self.language = language
2325
self.startLineIndex = startLineIndex
2426
self.colorScheme = colorScheme
27+
self.droppingLeadingSpaces = droppingLeadingSpaces
2528
self.firstLinePrecedingSpaceCount = firstLinePrecedingSpaceCount
2629
self.fontSize = fontSize
2730
let padding = firstLinePrecedingSpaceCount > 0
@@ -31,7 +34,8 @@ public struct CodeBlock: View {
3134
code: padding + code,
3235
language: language,
3336
colorScheme: colorScheme,
34-
fontSize: fontSize
37+
fontSize: fontSize,
38+
droppingLeadingSpaces: droppingLeadingSpaces
3539
)
3640
commonPrecedingSpaceCount = result.commonLeadingSpaceCount
3741
highlightedCode = result.code
@@ -72,14 +76,14 @@ public struct CodeBlock: View {
7276
code: String,
7377
language: String,
7478
colorScheme: ColorScheme,
75-
fontSize: Double
79+
fontSize: Double,
80+
droppingLeadingSpaces: Bool
7681
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
7782
return highlighted(
7883
code: code,
7984
language: language,
8085
brightMode: colorScheme != .dark,
81-
droppingLeadingSpaces: UserDefaults.shared
82-
.value(for: \.hideCommonPrecedingSpacesInSuggestion),
86+
droppingLeadingSpaces: droppingLeadingSpaces,
8387
fontSize: fontSize
8488
)
8589
}
@@ -98,7 +102,8 @@ struct CodeBlock_Previews: PreviewProvider {
98102
startLineIndex: 0,
99103
colorScheme: .dark,
100104
firstLinePrecedingSpaceCount: 0,
101-
fontSize: 12
105+
fontSize: 12,
106+
droppingLeadingSpaces: true
102107
)
103108
}
104109
}

Tool/Sources/SharedUIComponents/Experiment/NewCodeBlock.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct _CodeBlock: View {
1212
let commonPrecedingSpaceCount: Int
1313
let highlightedCode: AttributedString
1414
let colorScheme: ColorScheme
15+
let droppingLeadingSpaces: Bool
1516

1617
/// Create a text edit view with a certain text that uses a certain options.
1718
/// - Parameters:
@@ -24,11 +25,13 @@ struct _CodeBlock: View {
2425
firstLinePrecedingSpaceCount: Int,
2526
colorScheme: ColorScheme,
2627
fontSize: Double,
28+
droppingLeadingSpaces: Bool,
2729
selection: Binding<NSRange?> = .constant(nil)
2830
) {
2931
_selection = selection
3032
self.fontSize = fontSize
3133
self.colorScheme = colorScheme
34+
self.droppingLeadingSpaces = droppingLeadingSpaces
3235

3336
let padding = firstLinePrecedingSpaceCount > 0
3437
? String(repeating: " ", count: firstLinePrecedingSpaceCount)
@@ -37,7 +40,8 @@ struct _CodeBlock: View {
3740
code: padding + code,
3841
language: language,
3942
colorScheme: colorScheme,
40-
fontSize: fontSize
43+
fontSize: fontSize,
44+
droppingLeadingSpaces: droppingLeadingSpaces
4145
)
4246
commonPrecedingSpaceCount = result.commonLeadingSpaceCount
4347
highlightedCode = result.code
@@ -65,14 +69,14 @@ struct _CodeBlock: View {
6569
code: String,
6670
language: String,
6771
colorScheme: ColorScheme,
68-
fontSize: Double
72+
fontSize: Double,
73+
droppingLeadingSpaces: Bool
6974
) -> (code: AttributedString, commonLeadingSpaceCount: Int) {
7075
let (lines, commonLeadingSpaceCount) = highlighted(
7176
code: code,
7277
language: language,
7378
brightMode: colorScheme != .dark,
74-
droppingLeadingSpaces: UserDefaults.shared
75-
.value(for: \.hideCommonPrecedingSpacesInSuggestion),
79+
droppingLeadingSpaces: droppingLeadingSpaces,
7680
fontSize: fontSize,
7781
replaceSpacesWithMiddleDots: false
7882
)
@@ -142,7 +146,7 @@ private struct _CodeBlockRepresentable: NSViewRepresentable {
142146
context.coordinator.parent = self
143147

144148
let textView = scrollView.documentView as! STTextViewFrameObservable
145-
149+
146150
textView.onHeightChange = onHeightChange
147151
textView.showsInvisibleCharacters = true
148152
textView.textContainer.lineBreakMode = .byCharWrapping
@@ -241,7 +245,10 @@ private class STTextViewFrameObservable: STTextView {
241245
var onHeightChange: ((Double) -> Void)?
242246
func recalculateSize() {
243247
var maxY = 0 as Double
244-
textLayoutManager.enumerateTextLayoutFragments(in: textLayoutManager.documentRange, options: [.ensuresLayout]) { fragment in
248+
textLayoutManager.enumerateTextLayoutFragments(
249+
in: textLayoutManager.documentRange,
250+
options: [.ensuresLayout]
251+
) { fragment in
245252
print(fragment.layoutFragmentFrame)
246253
maxY = max(maxY, fragment.layoutFragmentFrame.maxY)
247254
return true
@@ -287,3 +294,4 @@ private final class ColumnRuler: NSRulerView {
287294
])
288295
}
289296
}
297+

0 commit comments

Comments
 (0)