|
| 1 | +import Environment |
| 2 | +import Foundation |
| 3 | +import SuggestionModel |
| 4 | + |
| 5 | +public protocol FilespacePropertyKey { |
| 6 | + associatedtype Value |
| 7 | +} |
| 8 | + |
| 9 | +public struct FilespacePropertyValues { |
| 10 | + var storage: [ObjectIdentifier: Any] = [:] |
| 11 | + |
| 12 | + public subscript<K: WorkspacePropertyKey>(key: K.Type) -> K.Value? { |
| 13 | + get { |
| 14 | + storage[ObjectIdentifier(key)] as? K.Value |
| 15 | + } |
| 16 | + set { |
| 17 | + storage[ObjectIdentifier(key)] = newValue |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +public final class Filespace { |
| 23 | + public struct Snapshot: Equatable { |
| 24 | + public var linesHash: Int |
| 25 | + public var cursorPosition: CursorPosition |
| 26 | + } |
| 27 | + |
| 28 | + public struct CodeMetadata: Equatable { |
| 29 | + public var uti: String? |
| 30 | + public var tabSize: Int? |
| 31 | + public var indentSize: Int? |
| 32 | + public var usesTabsForIndentation: Bool? |
| 33 | + } |
| 34 | + |
| 35 | + public let fileURL: URL |
| 36 | + public private(set) lazy var language: String = languageIdentifierFromFileURL(fileURL).rawValue |
| 37 | + public var suggestions: [CodeSuggestion] = [] { |
| 38 | + didSet { refreshUpdateTime() } |
| 39 | + } |
| 40 | + |
| 41 | + /// stored for pseudo command handler |
| 42 | + public var codeMetadata: CodeMetadata = .init() |
| 43 | + public var suggestionIndex: Int = 0 |
| 44 | + public var suggestionSourceSnapshot: Snapshot = .init(linesHash: -1, cursorPosition: .outOfScope) |
| 45 | + public var presentingSuggestion: CodeSuggestion? { |
| 46 | + guard suggestions.endIndex > suggestionIndex, suggestionIndex >= 0 else { return nil } |
| 47 | + return suggestions[suggestionIndex] |
| 48 | + } |
| 49 | + |
| 50 | + private(set) var lastSuggestionUpdateTime: Date = Environment.now() |
| 51 | + public var isExpired: Bool { |
| 52 | + Environment.now().timeIntervalSince(lastSuggestionUpdateTime) > 60 * 3 |
| 53 | + } |
| 54 | + |
| 55 | + let fileSaveWatcher: FileSaveWatcher |
| 56 | + let onClose: (URL) -> Void |
| 57 | + |
| 58 | + deinit { |
| 59 | + onClose(fileURL) |
| 60 | + } |
| 61 | + |
| 62 | + init( |
| 63 | + fileURL: URL, |
| 64 | + onSave: @escaping (Filespace) -> Void, |
| 65 | + onClose: @escaping (URL) -> Void |
| 66 | + ) { |
| 67 | + self.fileURL = fileURL |
| 68 | + self.onClose = onClose |
| 69 | + fileSaveWatcher = .init(fileURL: fileURL) |
| 70 | + fileSaveWatcher.changeHandler = { [weak self] in |
| 71 | + guard let self else { return } |
| 72 | + onSave(self) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public func reset(resetSnapshot: Bool = true) { |
| 77 | + suggestions = [] |
| 78 | + suggestionIndex = 0 |
| 79 | + if resetSnapshot { |
| 80 | + suggestionSourceSnapshot = .init(linesHash: -1, cursorPosition: .outOfScope) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public func refreshUpdateTime() { |
| 85 | + lastSuggestionUpdateTime = Environment.now() |
| 86 | + } |
| 87 | + |
| 88 | + /// Validate the suggestion is still valid. |
| 89 | + /// - Parameters: |
| 90 | + /// - lines: lines of the file |
| 91 | + /// - cursorPosition: cursor position |
| 92 | + /// - Returns: `true` if the suggestion is still valid |
| 93 | + public func validateSuggestions(lines: [String], cursorPosition: CursorPosition) -> Bool { |
| 94 | + guard let presentingSuggestion else { return false } |
| 95 | + |
| 96 | + // cursor has moved to another line |
| 97 | + if cursorPosition.line != presentingSuggestion.position.line { |
| 98 | + reset() |
| 99 | + return false |
| 100 | + } |
| 101 | + |
| 102 | + // the cursor position is valid |
| 103 | + guard cursorPosition.line >= 0, cursorPosition.line < lines.count else { |
| 104 | + reset() |
| 105 | + return false |
| 106 | + } |
| 107 | + |
| 108 | + let editingLine = lines[cursorPosition.line].dropLast(1) // dropping \n |
| 109 | + let suggestionLines = presentingSuggestion.text.split(separator: "\n") |
| 110 | + let suggestionFirstLine = suggestionLines.first ?? "" |
| 111 | + |
| 112 | + // the line content doesn't match the suggestion |
| 113 | + if cursorPosition.character > 0, |
| 114 | + !suggestionFirstLine.hasPrefix(editingLine[..<(editingLine.index( |
| 115 | + editingLine.startIndex, |
| 116 | + offsetBy: cursorPosition.character, |
| 117 | + limitedBy: editingLine.endIndex |
| 118 | + ) ?? editingLine.endIndex)]) |
| 119 | + { |
| 120 | + reset() |
| 121 | + return false |
| 122 | + } |
| 123 | + |
| 124 | + // finished typing the whole suggestion when the suggestion has only one line |
| 125 | + if editingLine.hasPrefix(suggestionFirstLine), suggestionLines.count <= 1 { |
| 126 | + reset() |
| 127 | + return false |
| 128 | + } |
| 129 | + |
| 130 | + // undo to a state before the suggestion was generated |
| 131 | + if editingLine.count < presentingSuggestion.position.character { |
| 132 | + reset() |
| 133 | + return false |
| 134 | + } |
| 135 | + |
| 136 | + return true |
| 137 | + } |
| 138 | +} |
| 139 | + |
0 commit comments