@@ -13,22 +13,38 @@ extension EditorManager {
13
13
/// Restores the tab manager from a captured state obtained using `saveRestorationState`
14
14
/// - Parameter workspace: The workspace to retrieve state from.
15
15
func restoreFromState( _ workspace: WorkspaceDocument ) {
16
- guard let fileManager = workspace . workspaceFileManager ,
17
- let data = workspace. getFromWorkspaceState ( . openTabs ) as? Data ,
18
- let state = try ? JSONDecoder ( ) . decode ( EditorRestorationState . self , from : data ) else {
19
- return
20
- }
16
+ do {
17
+ guard let fileManager = workspace. workspaceFileManager ,
18
+ let data = workspace . getFromWorkspaceState ( . openTabs ) as? Data else {
19
+ return
20
+ }
21
21
22
- guard !state. groups. isEmpty else {
23
- logger. warning ( " Empty Editor State found, restoring to clean editor state. " )
24
- initCleanState ( )
25
- return
26
- }
22
+ let state = try JSONDecoder ( ) . decode ( EditorRestorationState . self, from: data)
23
+
24
+ guard !state. groups. isEmpty else {
25
+ logger. warning ( " Empty Editor State found, restoring to clean editor state. " )
26
+ initCleanState ( )
27
+ return
28
+ }
27
29
28
- fixRestoredEditorLayout ( state. groups, fileManager: fileManager)
29
- self . editorLayout = state. groups
30
- self . activeEditor = activeEditor
31
- switchToActiveEditor ( )
30
+ guard let activeEditor = state. groups. find (
31
+ editor: state. activeEditor
32
+ ) ?? state. groups. findSomeEditor ( ) else {
33
+ logger. warning ( " Editor state could not restore active editor. " )
34
+ initCleanState ( )
35
+ return
36
+ }
37
+
38
+ fixRestoredEditorLayout ( state. groups, fileManager: fileManager)
39
+
40
+ self . editorLayout = state. groups
41
+ self . activeEditor = activeEditor
42
+ switchToActiveEditor ( )
43
+ } catch {
44
+ logger. warning (
45
+ " Could not restore editor state from saved data: \( error. localizedDescription, privacy: . public) "
46
+ )
47
+ }
32
48
}
33
49
34
50
/// Fix any hanging files after restoring from saved state.
@@ -89,7 +105,7 @@ extension EditorManager {
89
105
90
106
func saveRestorationState( _ workspace: WorkspaceDocument ) {
91
107
if let data = try ? JSONEncoder ( ) . encode (
92
- EditorRestorationState ( focus : activeEditor, groups: editorLayout)
108
+ EditorRestorationState ( activeEditor : activeEditor. id , groups: editorLayout)
93
109
) {
94
110
workspace. addToWorkspaceState ( key: . openTabs, value: data)
95
111
} else {
@@ -99,7 +115,7 @@ extension EditorManager {
99
115
}
100
116
101
117
struct EditorRestorationState : Codable {
102
- var focus : Editor
118
+ var activeEditor : UUID
103
119
var groups : EditorLayout
104
120
}
105
121
0 commit comments