|
1 | 1 | import SwiftUI |
2 | 2 |
|
3 | | -struct CodeBlock: View { |
4 | | - @Environment(\.colorScheme) var colorScheme |
5 | | - |
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 | | - |
13 | | - var body: some View { |
14 | | - VStack(spacing: 4) { |
15 | | - ForEach(0..<highlightedCode.endIndex, id: \.self) { index in |
16 | | - HStack(alignment: .firstTextBaseline, spacing: 4) { |
17 | | - Text("\(index + startLineIndex + 1)") |
18 | | - .multilineTextAlignment(.trailing) |
19 | | - .foregroundColor(.secondary) |
20 | | - .frame(minWidth: 40) |
21 | | - Text(AttributedString(highlightedCode[index])) |
22 | | - .foregroundColor(.white.opacity(0.1)) |
23 | | - .frame(maxWidth: .infinity, alignment: .leading) |
24 | | - .multilineTextAlignment(.leading) |
25 | | - .lineSpacing(4) |
26 | | - .overlay(alignment: .topLeading) { |
27 | | - if index == 0, commonPrecedingSpaceCount > 0 { |
28 | | - Text("\(commonPrecedingSpaceCount + 1)") |
29 | | - .padding(.top, -12) |
30 | | - .font(.footnote) |
31 | | - .foregroundStyle(colorScheme == .dark ? .white : .black) |
32 | | - .opacity(0.3) |
33 | | - } |
34 | | - } |
35 | | - } |
36 | | - } |
37 | | - } |
38 | | - .foregroundColor(.white) |
39 | | - .font(.system(size: 12, design: .monospaced)) |
40 | | - .padding(.leading, 4) |
41 | | - .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 |
66 | | - } |
67 | | -} |
68 | | - |
69 | 3 | struct CodeBlockSuggestionPanel: View { |
70 | 4 | @ObservedObject var suggestion: SuggestionProvider |
71 | 5 |
|
|
0 commit comments