Skip to content

Commit 95d1680

Browse files
committed
Move CodeBlock to its own file
1 parent 7e2ab5b commit 95d1680

2 files changed

Lines changed: 8 additions & 66 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Shangxin Guo on 2023/4/12.
6+
//
7+
8+
import Foundation

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,5 @@
11
import SwiftUI
22

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-
693
struct CodeBlockSuggestionPanel: View {
704
@ObservedObject var suggestion: SuggestionProvider
715

0 commit comments

Comments
 (0)