Skip to content

Commit

Permalink
Update CodeBlock to accept droppingLeadingSpaces in init
Browse files Browse the repository at this point in the history
  • Loading branch information
intitni committed Mar 27, 2024
1 parent c4a6c02 commit 5dd8ee0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct CodeBlockSuggestionPanel: View {
@AppStorage(\.suggestionCodeFontSize) var fontSize
@AppStorage(\.suggestionDisplayCompactMode) var suggestionDisplayCompactMode
@AppStorage(\.suggestionPresentationMode) var suggestionPresentationMode
@AppStorage(\.hideCommonPrecedingSpacesInSuggestion) var hideCommonPrecedingSpaces

struct ToolBar: View {
@ObservedObject var suggestion: CodeSuggestionProvider
Expand Down Expand Up @@ -101,7 +102,8 @@ struct CodeBlockSuggestionPanel: View {
language: suggestion.language,
startLineIndex: suggestion.startLineIndex,
colorScheme: colorScheme,
fontSize: fontSize
fontSize: fontSize,
droppingLeadingSpaces: hideCommonPrecedingSpaces
)
.frame(maxWidth: .infinity)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ extension PromptToCodePanel {
struct Content: View {
let store: StoreOf<PromptToCode>
@Environment(\.colorScheme) var colorScheme
@AppStorage(\.suggestionCodeFontSize) var fontSize
@AppStorage(\.promptToCodeCodeFontSize) var fontSize
@AppStorage(\.hideCommonPrecedingSpacesInPromptToCode) var hideCommonPrecedingSpaces

struct CodeContent: Equatable {
var code: String
Expand Down Expand Up @@ -278,7 +279,8 @@ extension PromptToCodePanel {
colorScheme: colorScheme,
firstLinePrecedingSpaceCount: viewStore.state
.firstLinePrecedingSpaceCount,
fontSize: fontSize
fontSize: fontSize,
droppingLeadingSpaces: hideCommonPrecedingSpaces
)
.frame(maxWidth: .infinity)
.scaleEffect(x: 1, y: -1, anchor: .center)
Expand Down
17 changes: 11 additions & 6 deletions Tool/Sources/SharedUIComponents/CodeBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ public struct CodeBlock: View {
public let highlightedCode: [NSAttributedString]
public let firstLinePrecedingSpaceCount: Int
public let fontSize: Double
public let droppingLeadingSpaces: Bool

public init(
code: String,
language: String,
startLineIndex: Int,
colorScheme: ColorScheme,
firstLinePrecedingSpaceCount: Int = 0,
fontSize: Double
fontSize: Double,
droppingLeadingSpaces: Bool
) {
self.code = code
self.language = language
self.startLineIndex = startLineIndex
self.colorScheme = colorScheme
self.droppingLeadingSpaces = droppingLeadingSpaces
self.firstLinePrecedingSpaceCount = firstLinePrecedingSpaceCount
self.fontSize = fontSize
let padding = firstLinePrecedingSpaceCount > 0
Expand All @@ -31,7 +34,8 @@ public struct CodeBlock: View {
code: padding + code,
language: language,
colorScheme: colorScheme,
fontSize: fontSize
fontSize: fontSize,
droppingLeadingSpaces: droppingLeadingSpaces
)
commonPrecedingSpaceCount = result.commonLeadingSpaceCount
highlightedCode = result.code
Expand Down Expand Up @@ -72,14 +76,14 @@ public struct CodeBlock: View {
code: String,
language: String,
colorScheme: ColorScheme,
fontSize: Double
fontSize: Double,
droppingLeadingSpaces: Bool
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
return highlighted(
code: code,
language: language,
brightMode: colorScheme != .dark,
droppingLeadingSpaces: UserDefaults.shared
.value(for: \.hideCommonPrecedingSpacesInSuggestion),
droppingLeadingSpaces: droppingLeadingSpaces,
fontSize: fontSize
)
}
Expand All @@ -98,7 +102,8 @@ struct CodeBlock_Previews: PreviewProvider {
startLineIndex: 0,
colorScheme: .dark,
firstLinePrecedingSpaceCount: 0,
fontSize: 12
fontSize: 12,
droppingLeadingSpaces: true
)
}
}
Expand Down
20 changes: 14 additions & 6 deletions Tool/Sources/SharedUIComponents/Experiment/NewCodeBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct _CodeBlock: View {
let commonPrecedingSpaceCount: Int
let highlightedCode: AttributedString
let colorScheme: ColorScheme
let droppingLeadingSpaces: Bool

/// Create a text edit view with a certain text that uses a certain options.
/// - Parameters:
Expand All @@ -24,11 +25,13 @@ struct _CodeBlock: View {
firstLinePrecedingSpaceCount: Int,
colorScheme: ColorScheme,
fontSize: Double,
droppingLeadingSpaces: Bool,
selection: Binding<NSRange?> = .constant(nil)
) {
_selection = selection
self.fontSize = fontSize
self.colorScheme = colorScheme
self.droppingLeadingSpaces = droppingLeadingSpaces

let padding = firstLinePrecedingSpaceCount > 0
? String(repeating: " ", count: firstLinePrecedingSpaceCount)
Expand All @@ -37,7 +40,8 @@ struct _CodeBlock: View {
code: padding + code,
language: language,
colorScheme: colorScheme,
fontSize: fontSize
fontSize: fontSize,
droppingLeadingSpaces: droppingLeadingSpaces
)
commonPrecedingSpaceCount = result.commonLeadingSpaceCount
highlightedCode = result.code
Expand Down Expand Up @@ -65,14 +69,14 @@ struct _CodeBlock: View {
code: String,
language: String,
colorScheme: ColorScheme,
fontSize: Double
fontSize: Double,
droppingLeadingSpaces: Bool
) -> (code: AttributedString, commonLeadingSpaceCount: Int) {
let (lines, commonLeadingSpaceCount) = highlighted(
code: code,
language: language,
brightMode: colorScheme != .dark,
droppingLeadingSpaces: UserDefaults.shared
.value(for: \.hideCommonPrecedingSpacesInSuggestion),
droppingLeadingSpaces: droppingLeadingSpaces,
fontSize: fontSize,
replaceSpacesWithMiddleDots: false
)
Expand Down Expand Up @@ -142,7 +146,7 @@ private struct _CodeBlockRepresentable: NSViewRepresentable {
context.coordinator.parent = self

let textView = scrollView.documentView as! STTextViewFrameObservable

textView.onHeightChange = onHeightChange
textView.showsInvisibleCharacters = true
textView.textContainer.lineBreakMode = .byCharWrapping
Expand Down Expand Up @@ -241,7 +245,10 @@ private class STTextViewFrameObservable: STTextView {
var onHeightChange: ((Double) -> Void)?
func recalculateSize() {
var maxY = 0 as Double
textLayoutManager.enumerateTextLayoutFragments(in: textLayoutManager.documentRange, options: [.ensuresLayout]) { fragment in
textLayoutManager.enumerateTextLayoutFragments(
in: textLayoutManager.documentRange,
options: [.ensuresLayout]
) { fragment in
print(fragment.layoutFragmentFrame)
maxY = max(maxY, fragment.layoutFragmentFrame.maxY)
return true
Expand Down Expand Up @@ -287,3 +294,4 @@ private final class ColumnRuler: NSRulerView {
])
}
}

0 comments on commit 5dd8ee0

Please sign in to comment.