-
-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathFilespace.swift
More file actions
198 lines (168 loc) · 5.21 KB
/
Filespace.swift
File metadata and controls
198 lines (168 loc) · 5.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import Dependencies
import Foundation
import GitIgnoreCheck
import SuggestionBasic
public protocol FilespacePropertyKey {
associatedtype Value
static func createDefaultValue() -> Value
}
public final class FilespacePropertyValues {
private var storage: [ObjectIdentifier: Any] = [:]
@WorkspaceActor
public subscript<K: FilespacePropertyKey>(_ key: K.Type) -> K.Value {
get {
if let value = storage[ObjectIdentifier(key)] as? K.Value {
return value
}
let value = key.createDefaultValue()
storage[ObjectIdentifier(key)] = value
return value
}
set {
storage[ObjectIdentifier(key)] = newValue
}
}
}
public struct FilespaceCodeMetadata: Equatable {
public var uti: String?
public var tabSize: Int?
public var indentSize: Int?
public var usesTabsForIndentation: Bool?
public var lineEnding: String = "\n"
init(
uti: String? = nil,
tabSize: Int? = nil,
indentSize: Int? = nil,
usesTabsForIndentation: Bool? = nil,
lineEnding: String = "\n"
) {
self.uti = uti
self.tabSize = tabSize
self.indentSize = indentSize
self.usesTabsForIndentation = usesTabsForIndentation
self.lineEnding = lineEnding
}
public mutating func guessLineEnding(from text: String?) {
lineEnding = if let proposedEnding = text?.last {
if proposedEnding.isNewline {
String(proposedEnding)
} else {
"\n"
}
} else {
"\n"
}
}
}
@dynamicMemberLookup
public final class Filespace: @unchecked Sendable {
struct GitIgnoreStatus {
var isIgnored: Bool
var checkTime: Date
var isExpired: Bool {
Environment.now().timeIntervalSince(checkTime) > 60 * 3
}
}
// MARK: Metadata
public let fileURL: URL
public private(set) lazy var language: CodeLanguage = languageIdentifierFromFileURL(fileURL)
public var codeMetadata: FilespaceCodeMetadata = .init()
public var isTextReadable: Bool {
fileURL.pathExtension != "mlmodel"
}
// MARK: Suggestions
public private(set) var suggestionIndex: Int = 0
public internal(set) var suggestions: [CodeSuggestion] = [] {
didSet { refreshUpdateTime() }
}
public var presentingSuggestion: CodeSuggestion? {
guard suggestions.endIndex > suggestionIndex, suggestionIndex >= 0 else {
if suggestions.isEmpty {
return nil
}
return suggestions.first
}
return suggestions[suggestionIndex]
}
// MARK: Life Cycle
public var isExpired: Bool {
Environment.now().timeIntervalSince(lastUpdateTime) > 60 * 60
}
public internal(set) var lastUpdateTime: Date = Environment.now()
private var additionalProperties = FilespacePropertyValues()
let fileSaveWatcher: FileSaveWatcher
let onClose: (URL) -> Void
// MARK: Git Ignore
@WorkspaceActor
private var gitIgnoreStatus: GitIgnoreStatus?
@WorkspaceActor
public var isGitIgnored: Bool {
get async {
@Dependency(\.gitIgnoredChecker) var gitIgnoredChecker
@Dependency(\.date) var date
if let gitIgnoreStatus = gitIgnoreStatus, !gitIgnoreStatus.isExpired {
return gitIgnoreStatus.isIgnored
}
let isIgnored = await gitIgnoredChecker.checkIfGitIgnored(fileURL: fileURL)
gitIgnoreStatus = .init(isIgnored: isIgnored, checkTime: date())
return isIgnored
}
}
@WorkspaceActor
public private(set) var version: Int = 0
// MARK: Methods
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)
}
}
@WorkspaceActor
public subscript<K>(
dynamicMember dynamicMember: WritableKeyPath<FilespacePropertyValues, K>
) -> K {
get { additionalProperties[keyPath: dynamicMember] }
set { additionalProperties[keyPath: dynamicMember] = newValue }
}
@WorkspaceActor
public func reset() {
suggestions = []
suggestionIndex = 0
}
public func refreshUpdateTime() {
lastUpdateTime = Environment.now()
}
@WorkspaceActor
public func setSuggestions(_ suggestions: [CodeSuggestion]) {
self.suggestions = suggestions
suggestionIndex = 0
}
@WorkspaceActor
public func nextSuggestion() {
suggestionIndex += 1
if suggestionIndex >= suggestions.endIndex {
suggestionIndex = 0
}
}
@WorkspaceActor
public func previousSuggestion() {
suggestionIndex -= 1
if suggestionIndex < 0 {
suggestionIndex = suggestions.endIndex - 1
}
}
@WorkspaceActor
public func bumpVersion() {
version += 1
}
}