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
69 lines (65 loc) · 2.55 KB
/
CodeBlock.swift
File metadata and controls
69 lines (65 loc) · 2.55 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
import SwiftUI
struct CodeBlock: View {
let code: String
let language: String
let startLineIndex: Int
let colorScheme: ColorScheme
let commonPrecedingSpaceCount: Int
let highlightedCode: [NSAttributedString]
init(code: String, language: String, startLineIndex: Int, colorScheme: ColorScheme) {
self.code = code
self.language = language
self.startLineIndex = startLineIndex
self.colorScheme = colorScheme
let result = Self.highlight(
code: code,
language: language,
colorScheme: colorScheme
)
self.commonPrecedingSpaceCount = result.commonLeadingSpaceCount
self.highlightedCode = result.code
}
var body: some View {
VStack(spacing: 4) {
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: 12, design: .monospaced))
.padding(.leading, 4)
.padding([.trailing, .top, .bottom])
}
static func highlight(
code: String,
language: String,
colorScheme: ColorScheme
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
return highlighted(
code: code,
language: language,
brightMode: colorScheme != .dark,
droppingLeadingSpaces: UserDefaults.shared
.value(for: \.hideCommonPrecedingSpacesInSuggestion)
)
}
}