Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Editor Layout Setup #1588

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ final class CEWorkspaceFileManager {
if let file = flattenedFileItems[path] {
return file
} else if createIfNotFound {
let url = URL(fileURLWithPath: path, relativeTo: folderUrl)
guard let url = URL(string: path, relativeTo: folderUrl) else {
return nil
}

// Drill down towards the file, indexing any directories needed. If file is not in the `folderURL` or
// subdirectories, exit.
Expand Down
48 changes: 32 additions & 16 deletions CodeEdit/Features/Editor/Models/EditorLayout+StateRestoration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,38 @@ extension EditorManager {
/// Restores the tab manager from a captured state obtained using `saveRestorationState`
/// - Parameter workspace: The workspace to retrieve state from.
func restoreFromState(_ workspace: WorkspaceDocument) {
guard let fileManager = workspace.workspaceFileManager,
let data = workspace.getFromWorkspaceState(.openTabs) as? Data,
let state = try? JSONDecoder().decode(EditorRestorationState.self, from: data) else {
return
}
do {
guard let fileManager = workspace.workspaceFileManager,
let data = workspace.getFromWorkspaceState(.openTabs) as? Data else {
return
}

guard !state.groups.isEmpty else {
logger.warning("Empty Editor State found, restoring to clean editor state.")
initCleanState()
return
}
let state = try JSONDecoder().decode(EditorRestorationState.self, from: data)

guard !state.groups.isEmpty else {
logger.warning("Empty Editor State found, restoring to clean editor state.")
initCleanState()
return
}

fixRestoredEditorLayout(state.groups, fileManager: fileManager)
self.editorLayout = state.groups
self.activeEditor = activeEditor
switchToActiveEditor()
guard let activeEditor = state.groups.find(
editor: state.activeEditor
) ?? state.groups.findSomeEditor() else {
logger.warning("Editor state could not restore active editor.")
initCleanState()
return
}

fixRestoredEditorLayout(state.groups, fileManager: fileManager)

self.editorLayout = state.groups
self.activeEditor = activeEditor
switchToActiveEditor()
} catch {
logger.warning(
"Could not restore editor state from saved data: \(error.localizedDescription, privacy: .public)"
)
}
}

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

func saveRestorationState(_ workspace: WorkspaceDocument) {
if let data = try? JSONEncoder().encode(
EditorRestorationState(focus: activeEditor, groups: editorLayout)
EditorRestorationState(activeEditor: activeEditor.id, groups: editorLayout)
) {
workspace.addToWorkspaceState(key: .openTabs, value: data)
} else {
Expand All @@ -99,7 +115,7 @@ extension EditorManager {
}

struct EditorRestorationState: Codable {
var focus: Editor
var activeEditor: UUID
var groups: EditorLayout
}

Expand Down
17 changes: 17 additions & 0 deletions CodeEdit/Features/Editor/Models/EditorLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ enum EditorLayout: Equatable {
}
}

func find(editor id: UUID) -> Editor? {
switch self {
case .one(let editor):
if editor.id == id {
return editor
}
case .vertical(let splitViewData), .horizontal(let splitViewData):
for layout in splitViewData.editorLayouts {
if let editor = layout.find(editor: id) {
return editor
}
}
}

return nil
}

/// Forms a set of all files currently represented by tabs.
func gatherOpenFiles() -> Set<CEWorkspaceFile> {
switch self {
Expand Down