| @@ -42,30 +42,36 @@ public enum SaveMode: String, Sendable, CaseIterable { |
| 42 | 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 |
| 46 | | /// unsaved edits while another is shown. The current buffer's outline, saving and requests |
| 47 | | /// for the editor to show a location are what most views use. The view layer attaches an |
| 48 | | /// editor to each buffer and follows `path` and `revealRequest`. |
| 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. Buffers show in panes (`EditorPane`), one or more |
| 47 | /// per window; `focused` is the pane commands and menus act on, and the properties below |
| 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 | 51 | @MainActor |
| 50 | 52 | @Observable |
| 51 | 53 | public final class DocumentSession { |
| 52 | | /// The current buffer's file. |
| 53 | | public private(set) var path: String? |
| 54 | | /// A copy of the current buffer's state as of its last change. Not observed: it changes |
| 55 | | /// on every keystroke; views follow `path`, `isDirty` and `outline`. |
| 56 | | @ObservationIgnored public private(set) var document: DocumentState? |
| 57 | | public private(set) var outline: [OutlineEntry] = [] |
| 58 | | /// Bumped when the buffer settles after edits, and when a file opens: views that read |
| 59 | | /// `document` follow this. |
| 60 | | public private(set) var editVersion = 0 |
| 61 | | public private(set) var isDirty = false |
| 54 | /// Where buffers show. There is always at least one. |
| 55 | public private(set) var panes: [EditorPane] |
| 56 | /// The pane with focus: what `open`, `reveal`, saving and the forwarders below act on. |
| 57 | public var focused: EditorPane |
| 58 | /// The focused pane's file. |
| 59 | public var path: String? { focused.path } |
| 60 | /// The focused pane's buffer state. Not observed: it changes on every keystroke; views |
| 61 | /// follow `path`, `isDirty` and `outline`. |
| 62 | public var document: DocumentState? { current?.buffer.document } |
| 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 | 68 | /// The file isn't UTF-8, so it can't be edited or saved. |
| 63 | | public private(set) var isReadOnly = false |
| 64 | | /// Set while the buffer and the file on disk conflict; says how to resolve it. |
| 65 | | public private(set) var conflict: String? |
| 66 | | /// Bumped to ask the editor to reveal an offset. |
| 67 | | public private(set) var revealRequest: (offset: Int, id: Int)? |
| 68 | | /// Open files, in the order buffers were opened (each after the one current then). |
| 69 | public var isReadOnly: Bool { focused.isReadOnly } |
| 70 | /// Set while the focused buffer and the file on disk conflict; says how to resolve it. |
| 71 | public var conflict: String? { focused.conflict } |
| 72 | /// Bumped to ask the focused pane's editor to reveal an offset. |
| 73 | public var revealRequest: (offset: Int, id: Int)? { focused.revealRequest } |
| 74 | /// Open files, in the order buffers were opened (each after the one focused then). |
| 69 | 75 | public private(set) var openPaths: [String] = [] |
| 70 | 76 | /// Open files with unsaved edits. |
| 71 | 77 | public private(set) var dirtyPaths: Set<String> = [] |
| @@ -91,8 +97,10 @@ public final class DocumentSession { |
| 91 | 97 | public func refreshSetup() { |
| 92 | 98 | guard let entry = current else { return } |
| 93 | 99 | resolveSetup(entry) |
| 94 | | refresh() |
| 95 | | editVersion += 1 |
| 100 | for pane in panes(showing: entry.path) { |
| 101 | refresh(pane) |
| 102 | pane.editVersion += 1 |
| 103 | } |
| 96 | 104 | } |
| 97 | 105 | |
| 98 | 106 | /// The entry's setup files read again, as its text names them now. |
| @@ -118,10 +126,11 @@ public final class DocumentSession { |
| 118 | 126 | private let fileSystem: FileSystem |
| 119 | 127 | private let recovery: RecoveryStore |
| 120 | 128 | @ObservationIgnored private var entries: [Entry] = [] |
| 121 | | @ObservationIgnored private var current: Entry? |
| 122 | | /// Paths, most recently shown first. |
| 129 | /// The focused pane's buffer. |
| 130 | private var current: Entry? { focused.path.flatMap(entry) } |
| 131 | /// Paths, most recently shown first, across every pane. |
| 123 | 132 | @ObservationIgnored private var history: [String] = [] |
| 124 | | @ObservationIgnored private var outlineUpdate: Task<Void, Never>? |
| 133 | @ObservationIgnored private var outlineUpdates: [String: Task<Void, Never>] = [:] |
| 125 | 134 | private var requestID = 0 |
| 126 | 135 | |
| 127 | 136 | public init( |
| @@ -130,10 +139,34 @@ public final class DocumentSession { |
| 130 | 139 | ) { |
| 131 | 140 | self.fileSystem = fileSystem |
| 132 | 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 | 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 | 170 | var autosave: Task<Void, Never>? { current?.autosave } |
| 138 | 171 | private var url: URL? { path.map { URL(fileURLWithPath: $0) } } |
| 139 | 172 | /// Whether the open file is an org file (`.org`, `.org_archive`), not plain text. |
| @@ -186,12 +219,17 @@ public final class DocumentSession { |
| 186 | 219 | /// Open buffers, most recently shown first. |
| 187 | 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 | 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 | 229 | let interval = Perf.begin("session.open") |
| 192 | 230 | defer { interval.end((path as NSString).lastPathComponent) } |
| 193 | 231 | if let existing = entry(path) { |
| 194 | | activate(existing) |
| 232 | show(existing, in: pane) |
| 195 | 233 | return |
| 196 | 234 | } |
| 197 | 235 | do { |
| @@ -200,108 +238,141 @@ public final class DocumentSession { |
| 200 | 238 | return |
| 201 | 239 | } |
| 202 | 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 | 242 | entries.insert(entry, at: index) |
| 205 | | activate(entry) |
| 243 | show(entry, in: pane) |
| 206 | 244 | } catch { |
| 207 | 245 | lastError = "Can't open \(path): \(error.localizedDescription)" |
| 208 | 246 | } |
| 209 | 247 | } |
| 210 | 248 | |
| 211 | | private func activate(_ entry: Entry) { |
| 212 | | current = entry |
| 213 | | path = entry.path |
| 214 | | conflict = entry.conflict |
| 215 | | revealRequest = nil |
| 249 | private func show(_ entry: Entry, in pane: EditorPane) { |
| 250 | pane.path = entry.path |
| 251 | if !pane.shown.contains(entry.path) { pane.shown.append(entry.path) } |
| 252 | pane.history.removeAll { $0 == entry.path } |
| 253 | pane.history.insert(entry.path, at: 0) |
| 254 | pane.revealRequest = nil |
| 216 | 255 | history.removeAll { $0 == entry.path } |
| 217 | 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 | 261 | public func cycle(_ step: Int) { |
| 223 | 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 | 267 | public func showPrevious() { |
| 229 | 268 | guard let previous = recentPaths.dropFirst().first else { return } |
| 230 | 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 | 274 | public func kill(_ path: String) { |
| 235 | 275 | guard let entry = entry(path) else { return } |
| 236 | 276 | entry.autosave?.cancel() |
| 237 | 277 | entries.removeAll { $0 === entry } |
| 238 | 278 | history.removeAll { $0 == path } |
| 239 | | if entry === current { |
| 240 | | current = nil |
| 241 | | if let next = recentPaths.first.flatMap(self.entry) ?? entries.last { |
| 242 | | activate(next) |
| 279 | outlineUpdates[path]?.cancel() |
| 280 | outlineUpdates[path] = nil |
| 281 | for pane in panes { |
| 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 | 287 | } else { |
| 244 | | outlineUpdate?.cancel() |
| 245 | | self.path = nil |
| 246 | | document = nil |
| 247 | | outline = [] |
| 248 | | isDirty = false |
| 249 | | conflict = nil |
| 250 | | revealRequest = nil |
| 251 | | refreshLists() |
| 288 | pane.path = nil |
| 289 | pane.outline = [] |
| 290 | pane.isDirty = false |
| 291 | pane.isReadOnly = false |
| 292 | pane.conflict = nil |
| 293 | pane.revealRequest = nil |
| 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 | 300 | public func close() { |
| 260 | 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 | 305 | public func attach(_ buffer: DocumentBuffer) { |
| 265 | | current?.buffer = buffer |
| 266 | | refresh() |
| 306 | if let path { attach(buffer, for: path) } |
| 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 |
| 270 | | /// saves on its own. |
| 316 | /// The buffer of `path` changed in its editor: the panes showing it update their views |
| 317 | /// once typing pauses, and it saves on its own. |
| 271 | 318 | public func bufferChanged(_ path: String) { |
| 272 | 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 | 324 | public func bufferChanged() { |
| 278 | | refresh(outline: false) |
| 279 | | outlineUpdate?.cancel() |
| 280 | | outlineUpdate = Task { [weak self] in |
| 281 | | try? await Task.sleep(for: .milliseconds(300)) |
| 282 | | guard !Task.isCancelled, let self, let document = self.document else { return } |
| 283 | | self.outline = self.isOrg ? Presentation.outline(document.tree) : [] |
| 284 | | self.editVersion += 1 |
| 325 | if let current { changed(current) } |
| 326 | } |
| 327 | |
| 328 | /// A buffer changed, typed or not: the panes showing it update their views, the outline |
| 329 | /// once typing pauses, and it saves on its own. |
| 330 | private func changed(_ entry: Entry) { |
| 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 | 352 | let interval = Perf.begin("session.refresh") |
| 291 | 353 | defer { interval.end() } |
| 292 | 354 | refreshLists() |
| 293 | | guard let buffer else { return } |
| 294 | | let state = buffer.document |
| 295 | | document = state |
| 296 | | if isDirty != state.isDirty { isDirty = state.isDirty } |
| 297 | | if isReadOnly == state.isEditable { isReadOnly = !state.isEditable } |
| 298 | | if conflict != current?.conflict { conflict = current?.conflict } |
| 355 | guard let entry = pane.path.flatMap(entry) else { return } |
| 356 | let state = entry.buffer.document |
| 357 | if pane.isDirty != state.isDirty { pane.isDirty = state.isDirty } |
| 358 | if pane.isReadOnly == state.isEditable { pane.isReadOnly = !state.isEditable } |
| 359 | if pane.conflict != entry.conflict { pane.conflict = entry.conflict } |
| 299 | 360 | if updateOutline { |
| 300 | | outline = isOrg ? Presentation.outline(state.tree) : [] |
| 301 | | editVersion += 1 |
| 361 | pane.outline = Self.isOrgPath(entry.path) ? Presentation.outline(state.tree) : [] |
| 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 | 376 | private func refreshLists() { |
| 306 | 377 | let paths = entries.map(\.path) |
| 307 | 378 | if paths != openPaths { openPaths = paths } |
| @@ -366,7 +437,7 @@ public final class DocumentSession { |
| 366 | 437 | } catch { |
| 367 | 438 | lastError = "Can't save \(name): \(error.localizedDescription)" |
| 368 | 439 | } |
| 369 | | if entry === current { refresh() } else { refreshLists() } |
| 440 | refresh(entry) |
| 370 | 441 | return !entry.buffer.document.isDirty |
| 371 | 442 | } |
| 372 | 443 | |
| @@ -669,9 +740,7 @@ public final class DocumentSession { |
| 669 | 740 | do { |
| 670 | 741 | let options = self.options |
| 671 | 742 | let step = try buffer.update { try $0.run(command, selection: [offset..<offset], now: now, answers: answers, options: options) } |
| 672 | | if case .commit = step { |
| 673 | | if entry === current { bufferChanged() } else { scheduleAutosave(entry); refreshLists() } |
| 674 | | } |
| 743 | if case .commit = step { changed(entry) } |
| 675 | 744 | return step |
| 676 | 745 | } catch { |
| 677 | 746 | return .failed("\(error)") |
| @@ -830,11 +899,6 @@ public final class DocumentSession { |
| 830 | 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 | 902 | // MARK: - Locations |
| 839 | 903 | |
| 840 | 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 | 923 | reveal(offset) |
| 860 | 924 | } |
| 861 | 925 | |
| 926 | /// Asks the focused pane's editor to show `offset`. |
| 862 | 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 | 933 | requestID += 1 |
| 864 | | revealRequest = (offset, requestID) |
| 934 | pane.revealRequest = (offset, requestID) |
| 865 | 935 | } |
| 866 | 936 | |
| 867 | 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 | 955 | case .unchanged: |
| 886 | 956 | break |
| 887 | 957 | } |
| 888 | | if entry === current { refresh() } else { refreshLists() } |
| 958 | refresh(entry) |
| 889 | 959 | } |
| 890 | 960 | } |