Skip to content

Commit 7e2ab5b

Browse files
committed
Move highlighting to CodeBlock
1 parent 5c4b432 commit 7e2ab5b

2 files changed

Lines changed: 44 additions & 38 deletions

File tree

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import SwiftUI
22

33
struct CodeBlock: View {
4-
@ObservedObject var suggestion: SuggestionProvider
54
@Environment(\.colorScheme) var colorScheme
65

6+
let code: String
7+
let language: String
8+
let startLineIndex: Int
9+
10+
@State var commonPrecedingSpaceCount: Int = 0
11+
@State var highlightedCode: [NSAttributedString] = []
12+
713
var body: some View {
814
VStack(spacing: 4) {
9-
let code = suggestion.highlightedCode(colorScheme: colorScheme)
10-
ForEach(0..<code.endIndex, id: \.self) { index in
15+
ForEach(0..<highlightedCode.endIndex, id: \.self) { index in
1116
HStack(alignment: .firstTextBaseline, spacing: 4) {
12-
Text("\(index + suggestion.startLineIndex + 1)")
17+
Text("\(index + startLineIndex + 1)")
1318
.multilineTextAlignment(.trailing)
1419
.foregroundColor(.secondary)
1520
.frame(minWidth: 40)
16-
Text(AttributedString(code[index]))
21+
Text(AttributedString(highlightedCode[index]))
1722
.foregroundColor(.white.opacity(0.1))
1823
.frame(maxWidth: .infinity, alignment: .leading)
1924
.multilineTextAlignment(.leading)
2025
.lineSpacing(4)
2126
.overlay(alignment: .topLeading) {
22-
if index == 0, suggestion.commonPrecedingSpaceCount > 0 {
23-
Text("\(suggestion.commonPrecedingSpaceCount + 1)")
27+
if index == 0, commonPrecedingSpaceCount > 0 {
28+
Text("\(commonPrecedingSpaceCount + 1)")
2429
.padding(.top, -12)
2530
.font(.footnote)
2631
.foregroundStyle(colorScheme == .dark ? .white : .black)
@@ -34,6 +39,30 @@ struct CodeBlock: View {
3439
.font(.system(size: 12, design: .monospaced))
3540
.padding(.leading, 4)
3641
.padding([.trailing, .top, .bottom])
42+
.onChange(of: code) { _ in
43+
highlightCode()
44+
}
45+
.onChange(of: colorScheme) { _ in
46+
highlightCode()
47+
}
48+
.onChange(of: language) { _ in
49+
highlightCode()
50+
}
51+
.onAppear {
52+
highlightCode()
53+
}
54+
}
55+
56+
func highlightCode() {
57+
let (new, spaceCount) = highlighted(
58+
code: code,
59+
language: language,
60+
brightMode: colorScheme != .dark,
61+
droppingLeadingSpaces: UserDefaults.shared
62+
.value(for: \.hideCommonPrecedingSpacesInSuggestion)
63+
)
64+
highlightedCode = new
65+
commonPrecedingSpaceCount = spaceCount
3766
}
3867
}
3968

@@ -85,8 +114,12 @@ struct CodeBlockSuggestionPanel: View {
85114
var body: some View {
86115
VStack(spacing: 0) {
87116
ScrollView {
88-
CodeBlock(suggestion: suggestion)
89-
.frame(maxWidth: .infinity)
117+
CodeBlock(
118+
code: suggestion.code,
119+
language: suggestion.language,
120+
startLineIndex: suggestion.startLineIndex
121+
)
122+
.frame(maxWidth: .infinity)
90123
}
91124
.background(Color.contentBackground)
92125

Core/Sources/SuggestionWidget/SuggestionProvider.swift

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,13 @@ import Foundation
22
import SwiftUI
33

44
public final class SuggestionProvider: ObservableObject {
5-
@Published public var code: String = "" {
6-
didSet { highlightedCode = nil }
7-
}
8-
9-
@Published public var language: String = "" {
10-
didSet { highlightedCode = nil }
11-
}
12-
5+
@Published public var code: String = ""
6+
@Published public var language: String = ""
137
@Published public var startLineIndex: Int = 0
148
@Published public var suggestionCount: Int = 0
159
@Published public var currentSuggestionIndex: Int = 0
1610
@Published public var commonPrecedingSpaceCount = 0
1711

18-
private var colorScheme: ColorScheme = .light
19-
private var highlightedCode: [NSAttributedString]?
20-
21-
func highlightedCode(colorScheme: ColorScheme) -> [NSAttributedString] {
22-
if colorScheme != self.colorScheme { highlightedCode = nil }
23-
self.colorScheme = colorScheme
24-
if let highlightedCode { return highlightedCode }
25-
let (new, spaceCount) = highlighted(
26-
code: code,
27-
language: language,
28-
brightMode: colorScheme != .dark,
29-
droppingLeadingSpaces: UserDefaults.shared
30-
.value(for: \.hideCommonPrecedingSpacesInSuggestion)
31-
)
32-
highlightedCode = new
33-
Task { @MainActor in
34-
commonPrecedingSpaceCount = spaceCount
35-
}
36-
return new
37-
}
38-
3912
public var onSelectPreviousSuggestionTapped: () -> Void
4013
public var onSelectNextSuggestionTapped: () -> Void
4114
public var onRejectSuggestionTapped: () -> Void

0 commit comments

Comments
 (0)