Skip to content

Commit 80c5535

Browse files
Fix Editor Layout Setup (#1588)
1 parent 779d624 commit 80c5535

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFileManager.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ final class CEWorkspaceFileManager {
9696
if let file = flattenedFileItems[path] {
9797
return file
9898
} else if createIfNotFound {
99-
let url = URL(fileURLWithPath: path, relativeTo: folderUrl)
99+
guard let url = URL(string: path, relativeTo: folderUrl) else {
100+
return nil
101+
}
100102

101103
// Drill down towards the file, indexing any directories needed. If file is not in the `folderURL` or
102104
// subdirectories, exit.

CodeEdit/Features/Editor/Models/EditorLayout+StateRestoration.swift

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,38 @@ extension EditorManager {
1313
/// Restores the tab manager from a captured state obtained using `saveRestorationState`
1414
/// - Parameter workspace: The workspace to retrieve state from.
1515
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+
}
2121

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+
}
2729

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+
}
3248
}
3349

3450
/// Fix any hanging files after restoring from saved state.
@@ -89,7 +105,7 @@ extension EditorManager {
89105

90106
func saveRestorationState(_ workspace: WorkspaceDocument) {
91107
if let data = try? JSONEncoder().encode(
92-
EditorRestorationState(focus: activeEditor, groups: editorLayout)
108+
EditorRestorationState(activeEditor: activeEditor.id, groups: editorLayout)
93109
) {
94110
workspace.addToWorkspaceState(key: .openTabs, value: data)
95111
} else {
@@ -99,7 +115,7 @@ extension EditorManager {
99115
}
100116

101117
struct EditorRestorationState: Codable {
102-
var focus: Editor
118+
var activeEditor: UUID
103119
var groups: EditorLayout
104120
}
105121

CodeEdit/Features/Editor/Models/EditorLayout.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ enum EditorLayout: Equatable {
4444
}
4545
}
4646

47+
func find(editor id: UUID) -> Editor? {
48+
switch self {
49+
case .one(let editor):
50+
if editor.id == id {
51+
return editor
52+
}
53+
case .vertical(let splitViewData), .horizontal(let splitViewData):
54+
for layout in splitViewData.editorLayouts {
55+
if let editor = layout.find(editor: id) {
56+
return editor
57+
}
58+
}
59+
}
60+
61+
return nil
62+
}
63+
4764
/// Forms a set of all files currently represented by tabs.
4865
func gatherOpenFiles() -> Set<CEWorkspaceFile> {
4966
switch self {

0 commit comments

Comments
 (0)