| @@ -42,30 +42,36 @@ public enum SaveMode: String, Sendable, CaseIterable { |
| 42 | case explicit |
42 | case explicit |
| 43 | } |
43 | } |
| 44 | |
44 | |
| 45 | /// The files open in a window, as Emacs's buffers: each keeps its text, caret, undo and |
45 | /// The files open in the app, as Emacs's buffers: each keeps its text, caret, undo and |
| 46 | /// unsaved edits while another is shown. The current buffer's outline, saving and requests |
46 | /// unsaved edits while another is shown. Buffers show in panes (`EditorPane`), one or more |
| 47 | /// for the editor to show a location are what most views use. The view layer attaches an |
47 | /// per window; `focused` is the pane commands and menus act on, and the properties below |
| 48 | /// editor to each buffer and follows `path` and `revealRequest`. |
48 | /// that describe one buffer (`path`, `outline`, `isDirty`, `conflict`, `revealRequest`) are |
| |
49 | /// its. The view layer attaches an editor to each buffer and, per pane, follows the pane's |
| |
50 | /// `path` and `revealRequest`. |
| 49 | @MainActor |
51 | @MainActor |
| 50 | @Observable |
52 | @Observable |
| 51 | public final class DocumentSession { |
53 | public final class DocumentSession { |
| 52 | /// The current buffer's file. |
54 | /// Where buffers show. There is always at least one. |
| 53 | public private(set) var path: String? |
55 | public private(set) var panes: [EditorPane] |
| 54 | /// A copy of the current buffer's state as of its last change. Not observed: it changes |
56 | /// The pane with focus: what `open`, `reveal`, saving and the forwarders below act on. |
| 55 | /// on every keystroke; views follow `path`, `isDirty` and `outline`. |
57 | public var focused: EditorPane |
| 56 | @ObservationIgnored public private(set) var document: DocumentState? |
58 | /// The focused pane's file. |
| 57 | public private(set) var outline: [OutlineEntry] = [] |
59 | public var path: String? { focused.path } |
| 58 | /// Bumped when the buffer settles after edits, and when a file opens: views that read |
60 | /// The focused pane's buffer state. Not observed: it changes on every keystroke; views |
| 59 | /// `document` follow this. |
61 | /// follow `path`, `isDirty` and `outline`. |
| 60 | public private(set) var editVersion = 0 |
62 | public var document: DocumentState? { current?.buffer.document } |
| 61 | public private(set) var isDirty = false |
63 | public var outline: [OutlineEntry] { focused.outline } |
| |
64 | /// Bumped when the focused buffer settles after edits, and when a file opens: views that |
| |
65 | /// read `document` follow this. |
| |
66 | public var editVersion: Int { focused.editVersion } |
| |
67 | public var isDirty: Bool { focused.isDirty } |
| 62 | /// The file isn't UTF-8, so it can't be edited or saved. |
68 | /// The file isn't UTF-8, so it can't be edited or saved. |
| 63 | public private(set) var isReadOnly = false |
69 | public var isReadOnly: Bool { focused.isReadOnly } |
| 64 | /// Set while the buffer and the file on disk conflict; says how to resolve it. |
70 | /// Set while the focused buffer and the file on disk conflict; says how to resolve it. |
| 65 | public private(set) var conflict: String? |
71 | public var conflict: String? { focused.conflict } |
| 66 | /// Bumped to ask the editor to reveal an offset. |
72 | /// Bumped to ask the focused pane's editor to reveal an offset. |
| 67 | public private(set) var revealRequest: (offset: Int, id: Int)? |
73 | public var revealRequest: (offset: Int, id: Int)? { focused.revealRequest } |
| 68 | /// Open files, in the order buffers were opened (each after the one current then). |
74 | /// Open files, in the order buffers were opened (each after the one focused then). |
| 69 | public private(set) var openPaths: [String] = [] |
75 | public private(set) var openPaths: [String] = [] |
| 70 | /// Open files with unsaved edits. |
76 | /// Open files with unsaved edits. |
| 71 | public private(set) var dirtyPaths: Set<String> = [] |
77 | public private(set) var dirtyPaths: Set<String> = [] |
| @@ -91,8 +97,10 @@ public final class DocumentSession { |
| 91 | public func refreshSetup() { |
97 | public func refreshSetup() { |
| 92 | guard let entry = current else { return } |
98 | guard let entry = current else { return } |
| 93 | resolveSetup(entry) |
99 | resolveSetup(entry) |
| 94 | refresh() |
100 | for pane in panes(showing: entry.path) { |
| 95 | editVersion += 1 |
101 | refresh(pane) |
| |
102 | pane.editVersion += 1 |
| |
103 | } |
| 96 | } |
104 | } |
| 97 | |
105 | |
| 98 | /// The entry's setup files read again, as its text names them now. |
106 | /// The entry's setup files read again, as its text names them now. |
| @@ -118,10 +126,11 @@ public final class DocumentSession { |
| 118 | private let fileSystem: FileSystem |
126 | private let fileSystem: FileSystem |
| 119 | private let recovery: RecoveryStore |
127 | private let recovery: RecoveryStore |
| 120 | @ObservationIgnored private var entries: [Entry] = [] |
128 | @ObservationIgnored private var entries: [Entry] = [] |
| 121 | @ObservationIgnored private var current: Entry? |
129 | /// The focused pane's buffer. |
| 122 | /// Paths, most recently shown first. |
130 | private var current: Entry? { focused.path.flatMap(entry) } |
| |
131 | /// Paths, most recently shown first, across every pane. |
| 123 | @ObservationIgnored private var history: [String] = [] |
132 | @ObservationIgnored private var history: [String] = [] |
| 124 | @ObservationIgnored private var outlineUpdate: Task<Void, Never>? |
133 | @ObservationIgnored private var outlineUpdates: [String: Task<Void, Never>] = [:] |
| 125 | private var requestID = 0 |
134 | private var requestID = 0 |
| 126 | |
135 | |
| 127 | public init( |
136 | public init( |
| @@ -130,10 +139,34 @@ public final class DocumentSession { |
| 130 | ) { |
139 | ) { |
| 131 | self.fileSystem = fileSystem |
140 | self.fileSystem = fileSystem |
| 132 | self.recovery = recovery |
141 | self.recovery = recovery |
| |
142 | let pane = EditorPane() |
| |
143 | panes = [pane] |
| |
144 | focused = pane |
| |
145 | } |
| |
146 | |
| |
147 | // MARK: - Panes |
| |
148 | |
| |
149 | /// A new, empty pane. |
| |
150 | public func makePane() -> EditorPane { |
| |
151 | let pane = EditorPane() |
| |
152 | panes.append(pane) |
| |
153 | return pane |
| |
154 | } |
| |
155 | |
| |
156 | /// Removes `pane`; its buffers stay open. The last pane stays. |
| |
157 | public func closePane(_ pane: EditorPane) { |
| |
158 | guard panes.count > 1, let index = panes.firstIndex(where: { $0 === pane }) else { return } |
| |
159 | panes.remove(at: index) |
| |
160 | if focused === pane { focused = panes[0] } |
| |
161 | } |
| |
162 | |
| |
163 | /// The panes showing `path`. |
| |
164 | private func panes(showing path: String) -> [EditorPane] { |
| |
165 | panes.filter { $0.path == path } |
| 133 | } |
166 | } |
| 134 | |
167 | |
| 135 | private var buffer: DocumentBuffer? { current?.buffer } |
168 | private var buffer: DocumentBuffer? { current?.buffer } |
| 136 | /// The current buffer's pending autosave, for tests to wait on. |
169 | /// The focused buffer's pending autosave, for tests to wait on. |
| 137 | var autosave: Task<Void, Never>? { current?.autosave } |
170 | var autosave: Task<Void, Never>? { current?.autosave } |
| 138 | private var url: URL? { path.map { URL(fileURLWithPath: $0) } } |
171 | private var url: URL? { path.map { URL(fileURLWithPath: $0) } } |
| 139 | /// Whether the open file is an org file (`.org`, `.org_archive`), not plain text. |
172 | /// Whether the open file is an org file (`.org`, `.org_archive`), not plain text. |
| @@ -186,12 +219,17 @@ public final class DocumentSession { |
| 186 | /// Open buffers, most recently shown first. |
219 | /// Open buffers, most recently shown first. |
| 187 | public var recentPaths: [String] { history.filter(isOpen) } |
220 | public var recentPaths: [String] { history.filter(isOpen) } |
| 188 | |
221 | |
| 189 | /// Shows the buffer of `path`, opening the file if it isn't open. |
222 | /// Shows the buffer of `path` in the focused pane, opening the file if it isn't open. |
| 190 | public func open(_ path: String) { |
223 | public func open(_ path: String) { |
| |
224 | open(path, in: focused) |
| |
225 | } |
| |
226 | |
| |
227 | /// Shows the buffer of `path` in `pane`, opening the file if it isn't open. |
| |
228 | public func open(_ path: String, in pane: EditorPane) { |
| 191 | let interval = Perf.begin("session.open") |
229 | let interval = Perf.begin("session.open") |
| 192 | defer { interval.end((path as NSString).lastPathComponent) } |
230 | defer { interval.end((path as NSString).lastPathComponent) } |
| 193 | if let existing = entry(path) { |
231 | if let existing = entry(path) { |
| 194 | activate(existing) |
232 | show(existing, in: pane) |
| 195 | return |
233 | return |
| 196 | } |
234 | } |
| 197 | do { |
235 | do { |
| @@ -200,108 +238,141 @@ public final class DocumentSession { |
| 200 | return |
238 | return |
| 201 | } |
239 | } |
| 202 | let entry = Entry(path: path, buffer: StateBuffer(loaded(bytes, path: path))) |
240 | let entry = Entry(path: path, buffer: StateBuffer(loaded(bytes, path: path))) |
| 203 | let index = current.flatMap { c in entries.firstIndex { $0 === c } }.map { $0 + 1 } ?? entries.count |
241 | let index = pane.path.flatMap(self.entry).flatMap { c in entries.firstIndex { $0 === c } }.map { $0 + 1 } ?? entries.count |
| 204 | entries.insert(entry, at: index) |
242 | entries.insert(entry, at: index) |
| 205 | activate(entry) |
243 | show(entry, in: pane) |
| 206 | } catch { |
244 | } catch { |
| 207 | lastError = "Can't open \(path): \(error.localizedDescription)" |
245 | lastError = "Can't open \(path): \(error.localizedDescription)" |
| 208 | } |
246 | } |
| 209 | } |
247 | } |
| 210 | |
248 | |
| 211 | private func activate(_ entry: Entry) { |
249 | private func show(_ entry: Entry, in pane: EditorPane) { |
| 212 | current = entry |
250 | pane.path = entry.path |
| 213 | path = entry.path |
251 | if !pane.shown.contains(entry.path) { pane.shown.append(entry.path) } |
| 214 | conflict = entry.conflict |
252 | pane.history.removeAll { $0 == entry.path } |
| 215 | revealRequest = nil |
253 | pane.history.insert(entry.path, at: 0) |
| |
254 | pane.revealRequest = nil |
| 216 | history.removeAll { $0 == entry.path } |
255 | history.removeAll { $0 == entry.path } |
| 217 | history.insert(entry.path, at: 0) |
256 | history.insert(entry.path, at: 0) |
| 218 | refresh() |
257 | refresh(pane) |
| 219 | } |
258 | } |
| 220 | |
259 | |
| 221 | /// The buffer before or after the current one, wrapping. |
260 | /// The buffer before or after the focused one, wrapping, in the focused pane. |
| 222 | public func cycle(_ step: Int) { |
261 | public func cycle(_ step: Int) { |
| 223 | guard let current, let index = entries.firstIndex(where: { $0 === current }), entries.count > 1 else { return } |
262 | guard let current, let index = entries.firstIndex(where: { $0 === current }), entries.count > 1 else { return } |
| 224 | activate(entries[((index + step) % entries.count + entries.count) % entries.count]) |
263 | show(entries[((index + step) % entries.count + entries.count) % entries.count], in: focused) |
| 225 | } |
264 | } |
| 226 | |
265 | |
| 227 | /// The buffer shown before the current one. |
266 | /// The buffer shown before the focused one. |
| 228 | public func showPrevious() { |
267 | public func showPrevious() { |
| 229 | guard let previous = recentPaths.dropFirst().first else { return } |
268 | guard let previous = recentPaths.dropFirst().first else { return } |
| 230 | open(previous) |
269 | open(previous) |
| 231 | } |
270 | } |
| 232 | |
271 | |
| 233 | /// Closes the buffer of `path` without saving it; the most recent other buffer shows. |
272 | /// Closes the buffer of `path` without saving it. A pane showing it shows the buffer it |
| |
273 | /// showed before, else the most recent other buffer, else nothing. |
| 234 | public func kill(_ path: String) { |
274 | public func kill(_ path: String) { |
| 235 | guard let entry = entry(path) else { return } |
275 | guard let entry = entry(path) else { return } |
| 236 | entry.autosave?.cancel() |
276 | entry.autosave?.cancel() |
| 237 | entries.removeAll { $0 === entry } |
277 | entries.removeAll { $0 === entry } |
| 238 | history.removeAll { $0 == path } |
278 | history.removeAll { $0 == path } |
| 239 | if entry === current { |
279 | outlineUpdates[path]?.cancel() |
| 240 | current = nil |
280 | outlineUpdates[path] = nil |
| 241 | if let next = recentPaths.first.flatMap(self.entry) ?? entries.last { |
281 | for pane in panes { |
| 242 | activate(next) |
282 | pane.shown.removeAll { $0 == path } |
| |
283 | pane.history.removeAll { $0 == path } |
| |
284 | guard pane.path == path else { continue } |
| |
285 | if let next = pane.history.first.flatMap(self.entry) ?? recentPaths.first.flatMap(self.entry) ?? entries.last { |
| |
286 | show(next, in: pane) |
| 243 | } else { |
287 | } else { |
| 244 | outlineUpdate?.cancel() |
288 | pane.path = nil |
| 245 | self.path = nil |
289 | pane.outline = [] |
| 246 | document = nil |
290 | pane.isDirty = false |
| 247 | outline = [] |
291 | pane.isReadOnly = false |
| 248 | isDirty = false |
292 | pane.conflict = nil |
| 249 | conflict = nil |
293 | pane.revealRequest = nil |
| 250 | revealRequest = nil |
| |
| 251 | refreshLists() |
| |
| 252 | } |
294 | } |
| 253 | } else { |
| |
| 254 | refreshLists() |
| |
| 255 | } |
295 | } |
| |
296 | refreshLists() |
| 256 | } |
297 | } |
| 257 | |
298 | |
| 258 | /// Closes the current buffer without saving, as when its file was deleted. |
299 | /// Closes the focused buffer without saving, as when its file was deleted. |
| 259 | public func close() { |
300 | public func close() { |
| 260 | if let path { kill(path) } |
301 | if let path { kill(path) } |
| 261 | } |
302 | } |
| 262 | |
303 | |
| 263 | /// Makes `buffer`, built from `document`, the live state of the current file. |
304 | /// Makes `buffer`, built from `document`, the live state of the focused file. |
| 264 | public func attach(_ buffer: DocumentBuffer) { |
305 | public func attach(_ buffer: DocumentBuffer) { |
| 265 | current?.buffer = buffer |
306 | if let path { attach(buffer, for: path) } |
| 266 | refresh() |
307 | } |
| |
308 | |
| |
309 | /// Makes `buffer`, built from the file's `document`, the live state of `path`. |
| |
310 | public func attach(_ buffer: DocumentBuffer, for path: String) { |
| |
311 | guard let entry = entry(path) else { return } |
| |
312 | entry.buffer = buffer |
| |
313 | for pane in panes(showing: path) { refresh(pane) } |
| 267 | } |
314 | } |
| 268 | |
315 | |
| 269 | /// The buffer of `path` changed in its editor: the current one updates its views, another |
316 | /// The buffer of `path` changed in its editor: the panes showing it update their views |
| 270 | /// saves on its own. |
317 | /// once typing pauses, and it saves on its own. |
| 271 | public func bufferChanged(_ path: String) { |
318 | public func bufferChanged(_ path: String) { |
| 272 | guard let entry = entry(path) else { return } |
319 | guard let entry = entry(path) else { return } |
| 273 | if entry === current { bufferChanged() } else { scheduleAutosave(entry); refreshLists() } |
320 | changed(entry) |
| 274 | } |
321 | } |
| 275 | |
322 | |
| 276 | /// The buffer changed through typing. The outline follows once typing pauses. |
323 | /// The focused buffer changed through typing. |
| 277 | public func bufferChanged() { |
324 | public func bufferChanged() { |
| 278 | refresh(outline: false) |
325 | if let current { changed(current) } |
| 279 | outlineUpdate?.cancel() |
326 | } |
| 280 | outlineUpdate = Task { [weak self] in |
327 | |
| 281 | try? await Task.sleep(for: .milliseconds(300)) |
328 | /// A buffer changed, typed or not: the panes showing it update their views, the outline |
| 282 | guard !Task.isCancelled, let self, let document = self.document else { return } |
329 | /// once typing pauses, and it saves on its own. |
| 283 | self.outline = self.isOrg ? Presentation.outline(document.tree) : [] |
330 | private func changed(_ entry: Entry) { |
| 284 | self.editVersion += 1 |
331 | let showing = panes(showing: entry.path) |
| |
332 | for pane in showing { refresh(pane, outline: false) } |
| |
333 | outlineUpdates[entry.path]?.cancel() |
| |
334 | if !showing.isEmpty { |
| |
335 | let path = entry.path |
| |
336 | outlineUpdates[path] = Task { [weak self, weak entry] in |
| |
337 | try? await Task.sleep(for: .milliseconds(300)) |
| |
338 | guard !Task.isCancelled, let self, let entry else { return } |
| |
339 | let outline = Self.isOrgPath(path) ? Presentation.outline(entry.buffer.document.tree) : [] |
| |
340 | for pane in self.panes(showing: path) { |
| |
341 | pane.outline = outline |
| |
342 | pane.editVersion += 1 |
| |
343 | } |
| |
344 | } |
| 285 | } |
345 | } |
| 286 | if let current { scheduleAutosave(current) } |
346 | scheduleAutosave(entry) |
| |
347 | refreshLists() |
| 287 | } |
348 | } |
| 288 | |
349 | |
| 289 | private func refresh(outline updateOutline: Bool = true) { |
350 | /// `pane`'s view of its buffer, from the buffer's state. |
| |
351 | private func refresh(_ pane: EditorPane, outline updateOutline: Bool = true) { |
| 290 | let interval = Perf.begin("session.refresh") |
352 | let interval = Perf.begin("session.refresh") |
| 291 | defer { interval.end() } |
353 | defer { interval.end() } |
| 292 | refreshLists() |
354 | refreshLists() |
| 293 | guard let buffer else { return } |
355 | guard let entry = pane.path.flatMap(entry) else { return } |
| 294 | let state = buffer.document |
356 | let state = entry.buffer.document |
| 295 | document = state |
357 | if pane.isDirty != state.isDirty { pane.isDirty = state.isDirty } |
| 296 | if isDirty != state.isDirty { isDirty = state.isDirty } |
358 | if pane.isReadOnly == state.isEditable { pane.isReadOnly = !state.isEditable } |
| 297 | if isReadOnly == state.isEditable { isReadOnly = !state.isEditable } |
359 | if pane.conflict != entry.conflict { pane.conflict = entry.conflict } |
| 298 | if conflict != current?.conflict { conflict = current?.conflict } |
| |
| 299 | if updateOutline { |
360 | if updateOutline { |
| 300 | outline = isOrg ? Presentation.outline(state.tree) : [] |
361 | pane.outline = Self.isOrgPath(entry.path) ? Presentation.outline(state.tree) : [] |
| 301 | editVersion += 1 |
362 | pane.editVersion += 1 |
| 302 | } |
363 | } |
| 303 | } |
364 | } |
| 304 | |
365 | |
| |
366 | /// Every pane showing `entry`, from the buffer's state. |
| |
367 | private func refresh(_ entry: Entry) { |
| |
368 | for pane in panes(showing: entry.path) { refresh(pane) } |
| |
369 | } |
| |
370 | |
| |
371 | /// The focused pane, from its buffer's state. |
| |
372 | private func refresh() { |
| |
373 | refresh(focused) |
| |
374 | } |
| |
375 | |
| 305 | private func refreshLists() { |
376 | private func refreshLists() { |
| 306 | let paths = entries.map(\.path) |
377 | let paths = entries.map(\.path) |
| 307 | if paths != openPaths { openPaths = paths } |
378 | if paths != openPaths { openPaths = paths } |
| @@ -366,7 +437,7 @@ public final class DocumentSession { |
| 366 | } catch { |
437 | } catch { |
| 367 | lastError = "Can't save \(name): \(error.localizedDescription)" |
438 | lastError = "Can't save \(name): \(error.localizedDescription)" |
| 368 | } |
439 | } |
| 369 | if entry === current { refresh() } else { refreshLists() } |
440 | refresh(entry) |
| 370 | return !entry.buffer.document.isDirty |
441 | return !entry.buffer.document.isDirty |
| 371 | } |
442 | } |
| 372 | |
443 | |
| @@ -669,9 +740,7 @@ public final class DocumentSession { |
| 669 | do { |
740 | do { |
| 670 | let options = self.options |
741 | let options = self.options |
| 671 | let step = try buffer.update { try $0.run(command, selection: [offset..<offset], now: now, answers: answers, options: options) } |
742 | let step = try buffer.update { try $0.run(command, selection: [offset..<offset], now: now, answers: answers, options: options) } |
| 672 | if case .commit = step { |
743 | if case .commit = step { changed(entry) } |
| 673 | if entry === current { bufferChanged() } else { scheduleAutosave(entry); refreshLists() } |
| |
| 674 | } |
| |
| 675 | return step |
744 | return step |
| 676 | } catch { |
745 | } catch { |
| 677 | return .failed("\(error)") |
746 | return .failed("\(error)") |
| @@ -830,11 +899,6 @@ public final class DocumentSession { |
| 830 | babelTask?.cancel() |
899 | babelTask?.cancel() |
| 831 | } |
900 | } |
| 832 | |
901 | |
| 833 | /// A buffer changed outside typing: the current one updates its views, another saves. |
| |
| 834 | private func changed(_ entry: Entry) { |
| |
| 835 | if entry === current { bufferChanged() } else { scheduleAutosave(entry); refreshLists() } |
| |
| 836 | } |
| |
| 837 | |
| |
| 838 | // MARK: - Locations |
902 | // MARK: - Locations |
| 839 | |
903 | |
| 840 | /// Opens the file of `location` if needed and asks the editor to show the heading. Offsets |
904 | /// Opens the file of `location` if needed and asks the editor to show the heading. Offsets |
| @@ -859,9 +923,15 @@ public final class DocumentSession { |
| 859 | reveal(offset) |
923 | reveal(offset) |
| 860 | } |
924 | } |
| 861 | |
925 | |
| |
926 | /// Asks the focused pane's editor to show `offset`. |
| 862 | public func reveal(_ offset: Int) { |
927 | public func reveal(_ offset: Int) { |
| |
928 | reveal(offset, in: focused) |
| |
929 | } |
| |
930 | |
| |
931 | /// Asks `pane`'s editor to show `offset`. |
| |
932 | public func reveal(_ offset: Int, in pane: EditorPane) { |
| 863 | requestID += 1 |
933 | requestID += 1 |
| 864 | revealRequest = (offset, requestID) |
934 | pane.revealRequest = (offset, requestID) |
| 865 | } |
935 | } |
| 866 | |
936 | |
| 867 | /// Called when the workspace sees a file change on disk: an open buffer of it takes the |
937 | /// Called when the workspace sees a file change on disk: an open buffer of it takes the |
| @@ -885,6 +955,6 @@ public final class DocumentSession { |
| 885 | case .unchanged: |
955 | case .unchanged: |
| 886 | break |
956 | break |
| 887 | } |
957 | } |
| 888 | if entry === current { refresh() } else { refreshLists() } |
958 | refresh(entry) |
| 889 | } |
959 | } |
| 890 | } |
960 | } |