@@ -20,14 +20,14 @@ final class Editor: ObservableObject, Identifiable {
2020
2121 if tabs. count > oldValue. count {
2222 // Amount of tabs grew, so set the first new as selected.
23- selectedTab = change. first
23+ setSelectedTab ( change. first? . file )
2424 } else {
2525 // Selected file was removed
2626 if let selectedTab, change. contains ( selectedTab) {
2727 if let oldIndex = oldValue. firstIndex ( of: selectedTab) , oldIndex - 1 < tabs. count, !tabs. isEmpty {
28- self . selectedTab = tabs [ max ( 0 , oldIndex- 1 ) ]
28+ setSelectedTab ( tabs [ max ( 0 , oldIndex- 1 ) ] . file )
2929 } else {
30- self . selectedTab = nil
30+ setSelectedTab ( nil )
3131 }
3232 }
3333 }
@@ -45,10 +45,10 @@ final class Editor: ObservableObject, Identifiable {
4545
4646 /// Maintains the list of tabs that have been switched to.
4747 /// - Warning: Use the ``addToHistory(_:)`` or ``clearFuture()`` methods to modify this. Do not modify directly.
48- @Published var history : Deque < Tab > = [ ]
48+ @Published var history : Deque < CEWorkspaceFile > = [ ]
4949
5050 /// Currently selected tab.
51- @Published var selectedTab : Tab ?
51+ @Published private ( set ) var selectedTab : Tab ?
5252
5353 @Published var temporaryTab : Tab ?
5454
@@ -98,6 +98,26 @@ final class Editor: ObservableObject, Identifiable {
9898 return parent? . getEditorLayout ( with: id)
9999 }
100100
101+ /// Set the selected tab. Loads the file's contents if it hasn't already been opened.
102+ /// - Parameter file: The file to set as the selected tab.
103+ func setSelectedTab( _ file: CEWorkspaceFile ? ) {
104+ guard let file else {
105+ selectedTab = nil
106+ return
107+ }
108+ guard let tab = self . tabs. first ( where: { $0. file == file } ) else {
109+ return
110+ }
111+ self . selectedTab = tab
112+ if tab. file. fileDocument == nil {
113+ do { // Ignore this error for simpler API usage.
114+ try openFile ( item: tab)
115+ } catch {
116+ print ( error)
117+ }
118+ }
119+ }
120+
101121 /// Closes a tab in the editor.
102122 /// This will also write any changes to the file on disk and will add the tab to the tab history.
103123 /// - Parameters:
0 commit comments