Skip to content

Commit

Permalink
EditorManager Always Opens Files (#1892)
Browse files Browse the repository at this point in the history
* Editor Opens All Files

* Replace Temporary Tab

---------

Co-authored-by: Tom Ludwig <tommludwig@icloud.com>
  • Loading branch information
thecoolwinter and tom-ludwig authored Oct 1, 2024
1 parent b4f7091 commit eb50bed
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 25 deletions.
16 changes: 9 additions & 7 deletions CodeEdit/Features/Editor/Models/Editor+History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ extension Editor {
/// Add the tab to the history list.
/// - Parameter tab: The tab to add to the history.
func addToHistory(_ tab: Tab) {
if history.first != tab {
history.prepend(tab)
if history.first != tab.file {
history.prepend(tab.file)
}
}

Expand Down Expand Up @@ -57,15 +57,17 @@ extension Editor {
/// If the tab is not opened, it is opened without modifying the history list.
/// - Warning: Do not use except in the ``historyOffset``'s `didSet`.
func historyOffsetDidChange() {
let tab = history[historyOffset]
let file = history[historyOffset]

if !tabs.contains(tab) {
if !tabs.contains(where: { $0.file == file }) {
if let temporaryTab, tabs.contains(temporaryTab) {
closeTab(file: temporaryTab.file, fromHistory: true)
}
temporaryTab = tab
openTab(file: tab.file, fromHistory: true)
openTab(file: file, fromHistory: true)
if let tab = tabs.first(where: { $0.file.id == file.id }) {
temporaryTab = tab
}
}
selectedTab = tab
setSelectedTab(file)
}
}
8 changes: 4 additions & 4 deletions CodeEdit/Features/Editor/Models/Editor+TabSwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ extension Editor {
guard let currentTab = selectedTab, let currentIndex = tabs.firstIndex(of: currentTab) else { return }
let nextIndex = tabs.index(after: currentIndex)
if nextIndex < tabs.endIndex {
selectedTab = tabs[nextIndex]
setSelectedTab(tabs[nextIndex].file)
} else {
// Wrap around to the first tab if it's the last one
selectedTab = tabs.first
setSelectedTab(tabs.first?.file)
}
}

func selectPreviousTab() {
guard let currentTab = selectedTab, let currentIndex = tabs.firstIndex(of: currentTab) else { return }
let previousIndex = tabs.index(before: currentIndex)
if previousIndex >= tabs.startIndex {
selectedTab = tabs[previousIndex]
setSelectedTab(tabs[previousIndex].file)
} else {
// Wrap around to the last tab if it's the first one
selectedTab = tabs.last
setSelectedTab(tabs.last?.file)
}
}
}
30 changes: 25 additions & 5 deletions CodeEdit/Features/Editor/Models/Editor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ final class Editor: ObservableObject, Identifiable {

if tabs.count > oldValue.count {
// Amount of tabs grew, so set the first new as selected.
selectedTab = change.first
setSelectedTab(change.first?.file)
} else {
// Selected file was removed
if let selectedTab, change.contains(selectedTab) {
if let oldIndex = oldValue.firstIndex(of: selectedTab), oldIndex - 1 < tabs.count, !tabs.isEmpty {
self.selectedTab = tabs[max(0, oldIndex-1)]
setSelectedTab(tabs[max(0, oldIndex-1)].file)
} else {
self.selectedTab = nil
setSelectedTab(nil)
}
}
}
Expand All @@ -45,10 +45,10 @@ final class Editor: ObservableObject, Identifiable {

/// Maintains the list of tabs that have been switched to.
/// - Warning: Use the ``addToHistory(_:)`` or ``clearFuture()`` methods to modify this. Do not modify directly.
@Published var history: Deque<Tab> = []
@Published var history: Deque<CEWorkspaceFile> = []

/// Currently selected tab.
@Published var selectedTab: Tab?
@Published private(set) var selectedTab: Tab?

@Published var temporaryTab: Tab?

Expand Down Expand Up @@ -98,6 +98,26 @@ final class Editor: ObservableObject, Identifiable {
return parent?.getEditorLayout(with: id)
}

/// Set the selected tab. Loads the file's contents if it hasn't already been opened.
/// - Parameter file: The file to set as the selected tab.
func setSelectedTab(_ file: CEWorkspaceFile?) {
guard let file else {
selectedTab = nil
return
}
guard let tab = self.tabs.first(where: { $0.file == file }) else {
return
}
self.selectedTab = tab
if tab.file.fileDocument == nil {
do { // Ignore this error for simpler API usage.
try openFile(item: tab)
} catch {
print(error)
}
}
}

/// Closes a tab in the editor.
/// This will also write any changes to the file on disk and will add the tab to the tab history.
/// - Parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ extension EditorManager {
editor.tabs = OrderedSet(resolvedTabs)
if let selectedTab = editor.selectedTab {
if let resolvedFile = fileManager.getFile(selectedTab.file.url.path(), createIfNotFound: true) {
editor.selectedTab = EditorInstance(file: resolvedFile)
editor.setSelectedTab(resolvedFile)
} else {
editor.selectedTab = nil
editor.setSelectedTab(nil)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct EditorTabView: View {
editorManager.activeEditor = editor
if editor.selectedTab?.file != item {
let tabItem = EditorInstance(file: item)
editor.selectedTab = tabItem
editor.setSelectedTab(item)
editor.clearFuture()
editor.addToHistory(tabItem)
}
Expand Down
12 changes: 6 additions & 6 deletions CodeEdit/Features/Editor/TabBar/Views/EditorHistoryMenus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ struct EditorHistoryMenus: View {
ForEach(
Array(editor.history.dropFirst(editor.historyOffset+1).enumerated()),
id: \.offset
) { index, tab in
) { index, file in
Button {
editorManager.activeEditor = editor
editor.historyOffset += index + 1
} label: {
HStack {
tab.file.icon
Text(tab.file.name)
file.icon
Text(file.name)
}
}
}
Expand All @@ -44,14 +44,14 @@ struct EditorHistoryMenus: View {
ForEach(
Array(editor.history.prefix(editor.historyOffset).reversed().enumerated()),
id: \.offset
) { index, tab in
) { index, file in
Button {
editorManager.activeEditor = editor
editor.historyOffset -= index + 1
} label: {
HStack {
tab.file.icon
Text(tab.file.name)
file.icon
Text(file.name)
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions CodeEdit/Features/Editor/Views/EditorAreaView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ struct EditorAreaView: View {
.opacity(dimEditorsWithoutFocus && editor != editorManager.activeEditor ? 0.5 : 1)
} else {
LoadingFileView(selected.file.name)
.onAppear {
if let file = selected.file.fileDocument {
self.codeFile = file
}
}
.onReceive(selected.file.fileDocumentPublisher) { latestValue in
self.codeFile = latestValue
}
}

} else {
Expand Down

0 comments on commit eb50bed

Please sign in to comment.