|
| 1 | +import Foundation |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +public final class SuggestionProvider: ObservableObject { |
| 5 | + @Published public var code: String = "" { |
| 6 | + didSet { highlightedCode = nil } |
| 7 | + } |
| 8 | + @Published public var language: String = "" { |
| 9 | + didSet { highlightedCode = nil } |
| 10 | + } |
| 11 | + @Published public var startLineIndex: Int = 0 |
| 12 | + @Published public var suggestionCount: Int = 0 |
| 13 | + @Published public var currentSuggestionIndex: Int = 0 |
| 14 | + |
| 15 | + private var colorScheme: ColorScheme = .light |
| 16 | + private var highlightedCode: [NSAttributedString]? = nil |
| 17 | + func highlightedCode(colorScheme: ColorScheme) -> [NSAttributedString] { |
| 18 | + if colorScheme != self.colorScheme { highlightedCode = nil } |
| 19 | + self.colorScheme = colorScheme |
| 20 | + if let highlightedCode { return highlightedCode } |
| 21 | + let new = highlighted( |
| 22 | + code: code, |
| 23 | + language: language, |
| 24 | + brightMode: colorScheme != .dark |
| 25 | + ) |
| 26 | + highlightedCode = new |
| 27 | + return new |
| 28 | + } |
| 29 | + |
| 30 | + public var onSelectPreviousSuggestionTapped: () -> Void |
| 31 | + public var onSelectNextSuggestionTapped: () -> Void |
| 32 | + public var onRejectSuggestionTapped: () -> Void |
| 33 | + public var onAcceptSuggestionTapped: () -> Void |
| 34 | + |
| 35 | + public init( |
| 36 | + code: String = "", |
| 37 | + language: String = "", |
| 38 | + startLineIndex: Int = 0, |
| 39 | + suggestionCount: Int = 0, |
| 40 | + currentSuggestionIndex: Int = 0, |
| 41 | + onSelectPreviousSuggestionTapped: @escaping () -> Void = {}, |
| 42 | + onSelectNextSuggestionTapped: @escaping () -> Void = {}, |
| 43 | + onRejectSuggestionTapped: @escaping () -> Void = {}, |
| 44 | + onAcceptSuggestionTapped: @escaping () -> Void = {} |
| 45 | + ) { |
| 46 | + self.code = code |
| 47 | + self.language = language |
| 48 | + self.startLineIndex = startLineIndex |
| 49 | + self.suggestionCount = suggestionCount |
| 50 | + self.currentSuggestionIndex = currentSuggestionIndex |
| 51 | + self.onSelectPreviousSuggestionTapped = onSelectPreviousSuggestionTapped |
| 52 | + self.onSelectNextSuggestionTapped = onSelectNextSuggestionTapped |
| 53 | + self.onRejectSuggestionTapped = onRejectSuggestionTapped |
| 54 | + self.onAcceptSuggestionTapped = onAcceptSuggestionTapped |
| 55 | + } |
| 56 | + |
| 57 | + func selectPreviousSuggestion() { onSelectPreviousSuggestionTapped() } |
| 58 | + func selectNextSuggestion() { onSelectNextSuggestionTapped() } |
| 59 | + func rejectSuggestion() { onRejectSuggestionTapped() } |
| 60 | + func acceptSuggestion() { onAcceptSuggestionTapped() } |
| 61 | +} |
0 commit comments