Skip to content

Commit dd6cae8

Browse files
committed
Add Undo Registration
1 parent 9b7a26e commit dd6cae8

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

CodeEdit/Features/Documents/Controllers/CodeEditSplitViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ final class CodeEditSplitViewController: NSSplitViewController {
7676
.environmentObject(statusBarViewModel)
7777
.environmentObject(utilityAreaModel)
7878
.environmentObject(taskManager)
79+
.environmentObject(workspace.undoRegistration)
7980
}
8081
}
8182

CodeEdit/Features/Documents/WorkspaceDocument/WorkspaceDocument.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ final class WorkspaceDocument: NSDocument, ObservableObject, NSToolbarDelegate {
4545
var workspaceSettingsManager: CEWorkspaceSettings?
4646
var taskNotificationHandler: TaskNotificationHandler = TaskNotificationHandler()
4747

48+
var undoRegistration: UndoManagerRegistration = UndoManagerRegistration()
49+
4850
@Published var notificationPanel = NotificationPanelViewModel()
4951
private var cancellables = Set<AnyCancellable>()
5052

CodeEdit/Features/Editor/Models/EditorStateRestoration.swift renamed to CodeEdit/Features/Editor/Models/Restoration/EditorStateRestoration.swift

File renamed without changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// UndoManagerRegistration.swift
3+
// CodeEdit
4+
//
5+
// Created by Khan Winter on 6/27/25.
6+
//
7+
8+
import SwiftUI
9+
import CodeEditTextView
10+
11+
/// Very simple class for registering undo manager for files for a project session. This does not do any saving, it
12+
/// just stores the undo managers in memory and retrieves them as necessary for files.
13+
final class UndoManagerRegistration: ObservableObject {
14+
private var managerMap: [CEWorkspaceFile.ID: CEUndoManager] = [:]
15+
16+
init() { }
17+
18+
func manager(forFile file: CEWorkspaceFile) -> CEUndoManager {
19+
if let manager = managerMap[file.id] {
20+
return manager
21+
} else {
22+
let newManager = CEUndoManager()
23+
managerMap[file.id] = newManager
24+
return newManager
25+
}
26+
}
27+
}

CodeEdit/Features/Editor/Views/CodeFileView.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ struct CodeFileView: View {
5858
@Environment(\.colorScheme)
5959
private var colorScheme
6060

61+
@EnvironmentObject
62+
var undoRegistration: UndoManagerRegistration
63+
6164
@ObservedObject private var themeModel: ThemeModel = .shared
6265

6366
@State private var treeSitter = TreeSitterClient()
@@ -66,8 +69,6 @@ struct CodeFileView: View {
6669

6770
private let isEditable: Bool
6871

69-
private let undoManager: CEUndoManager
70-
7172
init(
7273
editorInstance: EditorInstance,
7374
codeFile: CodeFileDocument,
@@ -88,15 +89,6 @@ struct CodeFileView: View {
8889
editorInstance.cursorPositions = openOptions.cursorPositions
8990
}
9091

91-
// Share the undo manager for the document between editors
92-
if let undoManager = codeFile.undoManager as? CEUndoManager {
93-
self.undoManager = undoManager
94-
} else {
95-
let undoManager = CEUndoManager()
96-
codeFile.undoManager = undoManager
97-
self.undoManager = undoManager
98-
}
99-
10092
updateHighlightProviders()
10193

10294
codeFile
@@ -170,7 +162,7 @@ struct CodeFileView: View {
170162
}
171163
),
172164
highlightProviders: highlightProviders,
173-
undoManager: undoManager,
165+
undoManager: undoRegistration.manager(forFile: editorInstance.file),
174166
coordinators: textViewCoordinators
175167
)
176168
.id(codeFile.fileURL)

CodeEdit/Features/Editor/Views/WindowCodeFileView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SwiftUI
1212
/// # Should **not** be used other than in a single file window.
1313
struct WindowCodeFileView: View {
1414
@StateObject var editorInstance: EditorInstance
15+
@StateObject var undoRegistration: UndoManagerRegistration = UndoManagerRegistration()
1516
var codeFile: CodeFileDocument
1617

1718
init(codeFile: CodeFileDocument) {
@@ -27,6 +28,7 @@ struct WindowCodeFileView: View {
2728
var body: some View {
2829
if let utType = codeFile.utType, utType.conforms(to: .text) {
2930
CodeFileView(editorInstance: editorInstance, codeFile: codeFile)
31+
.environmentObject(undoRegistration)
3032
} else {
3133
NonTextFileView(fileDocument: codeFile)
3234
}

0 commit comments

Comments
 (0)