-
-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathCSVDocument.swift
More file actions
349 lines (305 loc) · 12.5 KB
/
Copy pathCSVDocument.swift
File metadata and controls
349 lines (305 loc) · 12.5 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import AppKit
import TableProPluginKit
import os
public final class CSVDocument: NSDocument, InspectorDocument {
static let logger = Logger(subsystem: "com.TablePro", category: "CSVInspector")
private static let typeInferenceSampleSize = 200
private(set) var store = CSVRowStore(data: Data(), dialect: .csv)
private(set) var dialect: CSVDialect = .csv
private(set) var inferredTypes: [InspectorColumnType] = []
var typeOverrides: [Int: InspectorColumnType] = [:]
public var onChange: (() -> Void)?
private var lastInternalWriteTime: Date?
private var lastReadModificationDate: Date?
private var isPromptingExternalChange = false
override public class var autosavesInPlace: Bool { false }
override public class func canConcurrentlyReadDocuments(ofType typeName: String) -> Bool { true }
override public class var readableTypes: [String] {
["public.comma-separated-values-text", "public.tab-separated-values-text"]
}
override public class var writableTypes: [String] {
["public.comma-separated-values-text", "public.tab-separated-values-text"]
}
override public func writableTypes(for saveOperation: NSDocument.SaveOperationType) -> [String] {
Self.writableTypes
}
override public func fileNameExtension(
forType typeName: String,
saveOperation: NSDocument.SaveOperationType
) -> String? {
typeName == "public.tab-separated-values-text" ? "tsv" : "csv"
}
override public func makeWindowControllers() {
guard let factory = InspectorWindowFactory.make else {
Self.logger.error("CSVDocument.makeWindowControllers - InspectorWindowFactory.make is nil")
return
}
guard let windowController = factory(self) else {
Self.logger.error("CSVDocument.makeWindowControllers - factory returned nil")
return
}
addWindowController(windowController)
}
override public func read(from url: URL, ofType typeName: String) throws {
let data = try Data(contentsOf: url, options: .mappedIfSafe)
var detected = CSVDialect.detect(from: data)
if typeName == "public.tab-separated-values-text" {
detected.delimiter = 0x09
}
dialect = detected
store = CSVRowStore(data: data, dialect: detected)
let sample = store.pageRows(offset: 0, limit: Self.typeInferenceSampleSize)
inferredTypes = CSVTypeInferrer.inferColumns(rows: sample, columnCount: store.columnCount)
typeOverrides = [:]
let attrs = try? FileManager.default.attributesOfItem(atPath: url.path)
lastReadModificationDate = attrs?[.modificationDate] as? Date
}
override public func revert(toContentsOf url: URL, ofType typeName: String) throws {
try super.revert(toContentsOf: url, ofType: typeName)
NotificationCenter.default.post(name: .inspectorDocumentDidRevert, object: self)
}
override public func write(to url: URL, ofType typeName: String) throws {
try CSVWriter(dialect: dialect).write(store, to: url)
lastInternalWriteTime = Date()
}
override public func presentedItemDidChange() {
Task { @MainActor [weak self] in
guard let self else { return }
guard let url = self.fileURL else { return }
let attrs = try? FileManager.default.attributesOfItem(atPath: url.path)
let currentMtime = attrs?[.modificationDate] as? Date
if let current = currentMtime, let last = self.lastReadModificationDate, current == last {
Self.logger.debug("presentedItemDidChange: mtime unchanged, skip")
return
}
if let last = self.lastInternalWriteTime, Date().timeIntervalSince(last) < 2.0 {
return
}
if self.isPromptingExternalChange {
return
}
if self.isDocumentEdited {
self.promptExternalChangeReload(url: url)
} else {
self.tryRevert(from: url)
}
}
}
@MainActor
private func tryRevert(from url: URL) {
do {
try revert(toContentsOf: url, ofType: fileType ?? "public.comma-separated-values-text")
} catch {
Self.logger.error("Auto-revert failed: \(error.localizedDescription, privacy: .public)")
}
}
@MainActor
private func promptExternalChangeReload(url: URL) {
guard let window = windowControllers.first?.window else { return }
isPromptingExternalChange = true
let alert = NSAlert()
alert.messageText = String(localized: "File modified externally")
alert.informativeText = String(localized: "Another app changed this file. Discard your unsaved changes and reload?")
alert.addButton(withTitle: String(localized: "Reload"))
alert.addButton(withTitle: String(localized: "Keep Changes"))
alert.alertStyle = .warning
alert.beginSheetModal(for: window) { [weak self] response in
guard let self else { return }
self.isPromptingExternalChange = false
if response == .alertFirstButtonReturn {
self.tryRevert(from: url)
}
}
}
override public func updateChangeCount(_ change: NSDocument.ChangeType) {
super.updateChangeCount(change)
let edited = isDocumentEdited
for controller in windowControllers {
controller.window?.isDocumentEdited = edited
controller.synchronizeWindowTitleWithDocumentName()
}
}
// MARK: - InspectorDocument
public var rowCount: Int { store.rowCount }
public var columnNames: [String] { store.columnNames }
public func value(row: Int, column: Int) -> String {
store.value(row: row, column: column)
}
public func pageRows(offset: Int, limit: Int) -> [[String]] {
store.pageRows(offset: offset, limit: limit)
}
public func snapshot() -> any InspectorDataSnapshot {
store.snapshot()
}
public func displayedType(forColumn index: Int) -> InspectorColumnType {
if let override = typeOverrides[index] { return override }
guard index >= 0, index < inferredTypes.count else { return .text }
return inferredTypes[index]
}
public func setTypeOverride(_ type: InspectorColumnType?, forColumn index: Int) {
let previous = typeOverrides[index]
if let type {
typeOverrides[index] = type
} else {
typeOverrides.removeValue(forKey: index)
}
guard previous != type else { return }
registerUndo { document in
document.setTypeOverride(previous, forColumn: index)
}
onChange?()
}
public func setCell(row: Int, column: Int, to newValue: String) {
let previous = store.value(row: row, column: column)
guard previous != newValue else { return }
store.setValue(newValue, row: row, column: column)
registerUndo { document in
document.setCell(row: row, column: column, to: previous)
}
onChange?()
}
public func appendRow() {
let name = String(localized: "Add Row")
let index = store.appendRow(values: [])
registerUndo { document in
document.removeRow(at: index, suppressUndo: false, actionName: name)
}
setUndoActionName(name)
onChange?()
}
public func insertRow(at index: Int) {
let name = String(localized: "Insert Row")
store.insertRow([], at: index)
registerUndo { document in
document.removeRow(at: index, suppressUndo: false, actionName: name)
}
setUndoActionName(name)
onChange?()
}
public func removeRow(at index: Int) {
removeRow(at: index, suppressUndo: false, actionName: String(localized: "Delete Row"))
}
private func removeRow(at index: Int, suppressUndo: Bool, actionName: String) {
guard let removed = store.removeRow(at: index) else { return }
if !suppressUndo {
registerUndo { document in
document.reinsertRow(removed, at: index, actionName: actionName)
}
setUndoActionName(actionName)
}
onChange?()
}
private func reinsertRow(_ values: [String], at index: Int, actionName: String) {
store.insertRow(values, at: index)
registerUndo { document in
document.removeRow(at: index, suppressUndo: false, actionName: actionName)
}
setUndoActionName(actionName)
onChange?()
}
public func removeRows(at indices: IndexSet) {
let removed = store.removeRows(at: indices)
guard !removed.isEmpty else { return }
let name = removed.count == 1
? String(localized: "Delete Row")
: String(localized: "Delete Rows")
registerUndo { document in
document.reinsertRows(removed, actionName: name)
}
setUndoActionName(name)
onChange?()
}
private func reinsertRows(_ rows: [(index: Int, cells: [String])], actionName: String) {
for entry in rows.sorted(by: { $0.index < $1.index }) {
store.insertRow(entry.cells, at: entry.index)
}
let originalIndices = IndexSet(rows.map(\.index))
registerUndo { document in
document.removeRows(at: originalIndices)
}
setUndoActionName(actionName)
onChange?()
}
public func appendColumn(name: String) {
let index = store.columnCount
store.appendColumn(name: name)
inferredTypes.append(.text)
registerUndo { document in
document.removeColumn(at: index, suppressUndo: false)
}
onChange?()
}
public func insertColumn(at index: Int, name: String) {
store.insertColumn(at: index, name: name, values: [])
inferredTypes.insert(.text, at: min(max(index, 0), inferredTypes.count))
shiftTypeOverrides(insertingAt: index)
registerUndo { document in
document.removeColumn(at: index, suppressUndo: false)
}
onChange?()
}
public func removeColumn(at index: Int) {
removeColumn(at: index, suppressUndo: false)
}
private func removeColumn(at index: Int, suppressUndo: Bool) {
guard let removed = store.removeColumn(at: index) else { return }
let removedType = (index < inferredTypes.count) ? inferredTypes.remove(at: index) : .text
let removedOverride = typeOverrides[index]
shiftTypeOverrides(removingAt: index)
if !suppressUndo {
registerUndo { document in
document.reinsertColumn(
name: removed.name,
values: removed.values,
inferredType: removedType,
override: removedOverride,
at: index
)
}
}
onChange?()
}
private func reinsertColumn(
name: String,
values: [String],
inferredType: InspectorColumnType,
override: InspectorColumnType?,
at index: Int
) {
store.insertColumn(at: index, name: name, values: values)
inferredTypes.insert(inferredType, at: min(max(index, 0), inferredTypes.count))
shiftTypeOverrides(insertingAt: index)
if let override {
typeOverrides[index] = override
}
registerUndo { document in
document.removeColumn(at: index, suppressUndo: false)
}
onChange?()
}
public func renameColumn(at index: Int, to name: String) {
guard let previous = store.renameColumn(at: index, to: name), previous != name else { return }
registerUndo { document in
document.renameColumn(at: index, to: previous)
}
onChange?()
}
private func registerUndo(_ action: @escaping (CSVDocument) -> Void) {
undoManager?.registerUndo(withTarget: self, handler: action)
}
private func setUndoActionName(_ name: String) {
undoManager?.setActionName(name)
}
private func shiftTypeOverrides(insertingAt index: Int) {
typeOverrides = typeOverrides.reduce(into: [Int: InspectorColumnType]()) { result, entry in
result[entry.key >= index ? entry.key + 1 : entry.key] = entry.value
}
}
private func shiftTypeOverrides(removingAt index: Int) {
var shifted: [Int: InspectorColumnType] = [:]
for (key, value) in typeOverrides where key != index {
shifted[key > index ? key - 1 : key] = value
}
typeOverrides = shifted
}
}