forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBlock.swift
More file actions
105 lines (98 loc) · 3.53 KB
/
CodeBlock.swift
File metadata and controls
105 lines (98 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import SwiftUI
public struct CodeBlock: View {
public let code: String
public let language: String
public let startLineIndex: Int
public let colorScheme: ColorScheme
public let commonPrecedingSpaceCount: Int
public let highlightedCode: [NSAttributedString]
public let firstLinePrecedingSpaceCount: Int
public let fontSize: Double
public init(
code: String,
language: String,
startLineIndex: Int,
colorScheme: ColorScheme,
firstLinePrecedingSpaceCount: Int = 0,
fontSize: Double
) {
self.code = code
self.language = language
self.startLineIndex = startLineIndex
self.colorScheme = colorScheme
self.firstLinePrecedingSpaceCount = firstLinePrecedingSpaceCount
self.fontSize = fontSize
let padding = firstLinePrecedingSpaceCount > 0
? String(repeating: " ", count: firstLinePrecedingSpaceCount)
: ""
let result = Self.highlight(
code: padding + code,
language: language,
colorScheme: colorScheme,
fontSize: fontSize
)
commonPrecedingSpaceCount = result.commonLeadingSpaceCount
highlightedCode = result.code
}
public var body: some View {
VStack(spacing: 2) {
ForEach(0..<highlightedCode.endIndex, id: \.self) { index in
HStack(alignment: .firstTextBaseline, spacing: 4) {
Text("\(index + startLineIndex + 1)")
.multilineTextAlignment(.trailing)
.foregroundColor(.secondary)
.frame(minWidth: 40)
Text(AttributedString(highlightedCode[index]))
.foregroundColor(.white.opacity(0.1))
.frame(maxWidth: .infinity, alignment: .leading)
.multilineTextAlignment(.leading)
.lineSpacing(4)
.overlay(alignment: .topLeading) {
if index == 0, commonPrecedingSpaceCount > 0 {
Text("\(commonPrecedingSpaceCount + 1)")
.padding(.top, -12)
.font(.footnote)
.foregroundStyle(colorScheme == .dark ? .white : .black)
.opacity(0.3)
}
}
}
}
}
.foregroundColor(.white)
.font(.system(size: fontSize, design: .monospaced))
.padding(.leading, 4)
.padding([.trailing, .top, .bottom])
}
static func highlight(
code: String,
language: String,
colorScheme: ColorScheme,
fontSize: Double
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
return highlighted(
code: code,
language: language,
brightMode: colorScheme != .dark,
droppingLeadingSpaces: UserDefaults.shared
.value(for: \.hideCommonPrecedingSpacesInSuggestion),
fontSize: fontSize
)
}
}
// MARK: - Preview
struct CodeBlock_Previews: PreviewProvider {
static var previews: some View {
CodeBlock(
code: """
let foo = Foo()
let bar = Bar()
""",
language: "swift",
startLineIndex: 0,
colorScheme: .dark,
firstLinePrecedingSpaceCount: 0,
fontSize: 12
)
}
}