|
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 | +} |
0 commit comments