Skip to content

Commit aacb4b1

Browse files
committed
Fix highlighting in CodeBlock
1 parent 95d1680 commit aacb4b1

2 files changed

Lines changed: 72 additions & 9 deletions

File tree

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,69 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Shangxin Guo on 2023/4/12.
6-
//
7-
8-
import Foundation
1+
import SwiftUI
2+
3+
struct CodeBlock: View {
4+
let code: String
5+
let language: String
6+
let startLineIndex: Int
7+
let colorScheme: ColorScheme
8+
let commonPrecedingSpaceCount: Int
9+
let highlightedCode: [NSAttributedString]
10+
11+
init(code: String, language: String, startLineIndex: Int, colorScheme: ColorScheme) {
12+
self.code = code
13+
self.language = language
14+
self.startLineIndex = startLineIndex
15+
self.colorScheme = colorScheme
16+
let result = Self.highlight(
17+
code: code,
18+
language: language,
19+
colorScheme: colorScheme
20+
)
21+
self.commonPrecedingSpaceCount = result.commonLeadingSpaceCount
22+
self.highlightedCode = result.code
23+
}
24+
25+
var body: some View {
26+
VStack(spacing: 4) {
27+
ForEach(0..<highlightedCode.endIndex, id: \.self) { index in
28+
HStack(alignment: .firstTextBaseline, spacing: 4) {
29+
Text("\(index + startLineIndex + 1)")
30+
.multilineTextAlignment(.trailing)
31+
.foregroundColor(.secondary)
32+
.frame(minWidth: 40)
33+
Text(AttributedString(highlightedCode[index]))
34+
.foregroundColor(.white.opacity(0.1))
35+
.frame(maxWidth: .infinity, alignment: .leading)
36+
.multilineTextAlignment(.leading)
37+
.lineSpacing(4)
38+
.overlay(alignment: .topLeading) {
39+
if index == 0, commonPrecedingSpaceCount > 0 {
40+
Text("\(commonPrecedingSpaceCount + 1)")
41+
.padding(.top, -12)
42+
.font(.footnote)
43+
.foregroundStyle(colorScheme == .dark ? .white : .black)
44+
.opacity(0.3)
45+
}
46+
}
47+
}
48+
}
49+
}
50+
.foregroundColor(.white)
51+
.font(.system(size: 12, design: .monospaced))
52+
.padding(.leading, 4)
53+
.padding([.trailing, .top, .bottom])
54+
}
55+
56+
static func highlight(
57+
code: String,
58+
language: String,
59+
colorScheme: ColorScheme
60+
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
61+
return highlighted(
62+
code: code,
63+
language: language,
64+
brightMode: colorScheme != .dark,
65+
droppingLeadingSpaces: UserDefaults.shared
66+
.value(for: \.hideCommonPrecedingSpacesInSuggestion)
67+
)
68+
}
69+
}

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import SwiftUI
22

33
struct CodeBlockSuggestionPanel: View {
44
@ObservedObject var suggestion: SuggestionProvider
5+
@Environment(\.colorScheme) var colorScheme
56

67
struct ToolBar: View {
78
@ObservedObject var suggestion: SuggestionProvider
@@ -51,7 +52,8 @@ struct CodeBlockSuggestionPanel: View {
5152
CodeBlock(
5253
code: suggestion.code,
5354
language: suggestion.language,
54-
startLineIndex: suggestion.startLineIndex
55+
startLineIndex: suggestion.startLineIndex,
56+
colorScheme: colorScheme
5557
)
5658
.frame(maxWidth: .infinity)
5759
}

0 commit comments

Comments
 (0)