-
-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathFilespace.swift
More file actions
139 lines (117 loc) · 4.21 KB
/
Filespace.swift
File metadata and controls
139 lines (117 loc) · 4.21 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import Environment
import Foundation
import SuggestionModel
public protocol FilespacePropertyKey {
associatedtype Value
}
public struct FilespacePropertyValues {
var storage: [ObjectIdentifier: Any] = [:]
public subscript<K: WorkspacePropertyKey>(key: K.Type) -> K.Value? {
get {
storage[ObjectIdentifier(key)] as? K.Value
}
set {
storage[ObjectIdentifier(key)] = newValue
}
}
}
public final class Filespace {
public struct Snapshot: Equatable {
public var linesHash: Int
public var cursorPosition: CursorPosition
}
public struct CodeMetadata: Equatable {
public var uti: String?
public var tabSize: Int?
public var indentSize: Int?
public var usesTabsForIndentation: Bool?
}
public let fileURL: URL
public private(set) lazy var language: String = languageIdentifierFromFileURL(fileURL).rawValue
public var suggestions: [CodeSuggestion] = [] {
didSet { refreshUpdateTime() }
}
/// stored for pseudo command handler
public var codeMetadata: CodeMetadata = .init()
public var suggestionIndex: Int = 0
public var suggestionSourceSnapshot: Snapshot = .init(linesHash: -1, cursorPosition: .outOfScope)
public var presentingSuggestion: CodeSuggestion? {
guard suggestions.endIndex > suggestionIndex, suggestionIndex >= 0 else { return nil }
return suggestions[suggestionIndex]
}
private(set) var lastSuggestionUpdateTime: Date = Environment.now()
public var isExpired: Bool {
Environment.now().timeIntervalSince(lastSuggestionUpdateTime) > 60 * 3
}
let fileSaveWatcher: FileSaveWatcher
let onClose: (URL) -> Void
deinit {
onClose(fileURL)
}
init(
fileURL: URL,
onSave: @escaping (Filespace) -> Void,
onClose: @escaping (URL) -> Void
) {
self.fileURL = fileURL
self.onClose = onClose
fileSaveWatcher = .init(fileURL: fileURL)
fileSaveWatcher.changeHandler = { [weak self] in
guard let self else { return }
onSave(self)
}
}
public func reset(resetSnapshot: Bool = true) {
suggestions = []
suggestionIndex = 0
if resetSnapshot {
suggestionSourceSnapshot = .init(linesHash: -1, cursorPosition: .outOfScope)
}
}
public func refreshUpdateTime() {
lastSuggestionUpdateTime = Environment.now()
}
/// Validate the suggestion is still valid.
/// - Parameters:
/// - lines: lines of the file
/// - cursorPosition: cursor position
/// - Returns: `true` if the suggestion is still valid
public func validateSuggestions(lines: [String], cursorPosition: CursorPosition) -> Bool {
guard let presentingSuggestion else { return false }
// cursor has moved to another line
if cursorPosition.line != presentingSuggestion.position.line {
reset()
return false
}
// the cursor position is valid
guard cursorPosition.line >= 0, cursorPosition.line < lines.count else {
reset()
return false
}
let editingLine = lines[cursorPosition.line].dropLast(1) // dropping \n
let suggestionLines = presentingSuggestion.text.split(separator: "\n")
let suggestionFirstLine = suggestionLines.first ?? ""
// the line content doesn't match the suggestion
if cursorPosition.character > 0,
!suggestionFirstLine.hasPrefix(editingLine[..<(editingLine.index(
editingLine.startIndex,
offsetBy: cursorPosition.character,
limitedBy: editingLine.endIndex
) ?? editingLine.endIndex)])
{
reset()
return false
}
// finished typing the whole suggestion when the suggestion has only one line
if editingLine.hasPrefix(suggestionFirstLine), suggestionLines.count <= 1 {
reset()
return false
}
// undo to a state before the suggestion was generated
if editingLine.count < presentingSuggestion.position.character {
reset()
return false
}
return true
}
}