forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBlock.swift
More file actions
129 lines (121 loc) · 4.31 KB
/
CodeBlock.swift
File metadata and controls
129 lines (121 loc) · 4.31 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import Preferences
import SwiftUI
public struct CodeBlock: View {
public let code: String
public let language: String
public let startLineIndex: Int
public let scenario: String
public let colorScheme: ColorScheme
public let commonPrecedingSpaceCount: Int
public let highlightedCode: [NSAttributedString]
public let firstLinePrecedingSpaceCount: Int
public let font: NSFont
public let droppingLeadingSpaces: Bool
public let proposedForegroundColor: Color?
public let wrapCode: Bool
public init(
code: String,
language: String,
startLineIndex: Int,
scenario: String,
colorScheme: ColorScheme,
firstLinePrecedingSpaceCount: Int = 0,
font: NSFont,
droppingLeadingSpaces: Bool,
proposedForegroundColor: Color?,
wrapCode: Bool = true
) {
self.code = code
self.language = language
self.startLineIndex = startLineIndex
self.scenario = scenario
self.colorScheme = colorScheme
self.droppingLeadingSpaces = droppingLeadingSpaces
self.firstLinePrecedingSpaceCount = firstLinePrecedingSpaceCount
self.font = font
self.proposedForegroundColor = proposedForegroundColor
self.wrapCode = wrapCode
let padding = firstLinePrecedingSpaceCount > 0
? String(repeating: " ", count: firstLinePrecedingSpaceCount)
: ""
let result = Self.highlight(
code: padding + code,
language: language,
scenario: scenario,
colorScheme: colorScheme,
font: font,
droppingLeadingSpaces: droppingLeadingSpaces
)
commonPrecedingSpaceCount = result.commonLeadingSpaceCount
highlightedCode = result.code
}
var foregroundColor: Color {
proposedForegroundColor ?? (colorScheme == .dark ? .white : .black)
}
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(.gray)
.frame(minWidth: 40)
Text(AttributedString(highlightedCode[index]))
.foregroundColor(.gray)
.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(foregroundColor)
.opacity(0.3)
}
}
}
}
}
.foregroundColor(.white)
.font(.init(font))
.padding(.leading, 4)
.padding([.trailing, .top, .bottom])
}
static func highlight(
code: String,
language: String,
scenario: String,
colorScheme: ColorScheme,
font: NSFont,
droppingLeadingSpaces: Bool
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
return CodeHighlighting.highlighted(
code: code,
language: language,
scenario: scenario,
brightMode: colorScheme != .dark,
droppingLeadingSpaces: droppingLeadingSpaces,
font: font
)
}
}
// MARK: - Preview
struct CodeBlock_Previews: PreviewProvider {
static var previews: some View {
CodeBlock(
code: """
let foo = Foo()
let bar = Bar()
""",
language: "swift",
startLineIndex: 0,
scenario: "",
colorScheme: .dark,
firstLinePrecedingSpaceCount: 0,
font: .monospacedSystemFont(ofSize: 12, weight: .regular),
droppingLeadingSpaces: true,
proposedForegroundColor: nil
)
}
}