Files
helder/Sources/HelderUI/State/WorkspaceModel+Commands.swift
T

100 lines
3.6 KiB
Swift

import CoreGraphics
import Foundation
import HelderCore
import HelderServices
extension WorkspaceModel {
// MARK: Overlays
func overlayChanged(from old: Overlay, to new: Overlay) {
switch old {
case .note:
if let note { track { await note.save() } }
case .diff:
editor?.closeDiff()
default:
break
}
}
/// The recent projects other than this one, for ⇧⌘O.
public var otherRecentProjects: [RecentProject] {
recentProjects.filter { $0.path != root?.path }
}
/// ⇧⌘O: the recent-project list; the recents are re-read, since another window may have added one.
func showRecentProjects() {
overlay.present(.launcher)
track {
self.recentProjects = await self.app.recentProjects.list()
}
}
func activateLauncher() {
switch launcher.selectedChoice {
case .chooseFolder: perform(.openFolder)
case let .open(path): perform(.openRecentProject(path))
case nil: break
}
}
// MARK: Focus and the row cursor
func focus(_ panel: Panel) {
let previous = layout.activePanel
layout.focus(panel)
guard layout.activePanel == panel else { return }
focusRequest = FocusRequest(.panel(panel))
if panel == .editor { editor?.requestTextFocus() }
if panel == .sourceControl && previous != .sourceControl { scheduleRefresh(.git) }
}
func moveRowCursor(by step: Int) {
switch layout.activePanel {
case .sourceControl: sourceControl?.moveSelection(by: step)
case .explorer: explorer?.moveSelection(by: step, showHidden: layout.showHidden)
default: break
}
}
func openSelectedRow() {
switch layout.activePanel {
case .sourceControl:
guard let row = selectedGitRow() else { return }
open(row.change.path, OpenOptions(side: row.change.staged ? .staged : .unstaged))
case .explorer:
guard let explorer, let row = selectedTreeRow() else { return }
if row.row.kind == .directory { explorer.toggle(row.row.path) } else { open(row.row.path) }
default:
break
}
}
// MARK: Editor and the note
func passSelection(_ info: TextSelectionInfo?) {
guard let editor, let path = editor.activePath else { return }
if let info {
let reference = AgentReference(path: path, lines: info.startLine...max(info.startLine, info.endLine))
overlay.present(.passOn(PassOnRequest(reference: reference.description, code: info.text, anchor: defaultAnchor)))
} else if let request = editor.passOnSelectionRequest(anchor: defaultAnchor) {
overlay.present(.passOn(request))
}
}
/// ⌘P with the note open: the whole note goes to the agent, and the note closes so the text is seen landing.
func passNote() {
guard let note, let text = note.passableText else { return }
terminal.insertIntoAgent(text: text)
overlay.dismiss()
toasts.show("Note passed to agent", detail: NoteStore.fileName)
}
func editorSelectionFirstLine() -> String {
guard let editor, let path = editor.activePath, let selection = editor.selection, selection.path == path else { return "" }
let lines = editor.text(for: path).split(separator: "\n", omittingEmptySubsequences: false)
guard selection.start >= 1, selection.start <= lines.count else { return "" }
return lines[selection.start - 1].trimmingCharacters(in: .whitespaces)
}
}