Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions macos/Sources/Features/Terminal/TerminalController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,64 @@ class TerminalController: BaseTerminalController, TabGroupCloseCoordinator.Contr
Task { await gitDiffSidebarState.setVisible(false, cwd: nil) }
}

@objc func openInEditor(_ sender: Any?) {
// If the dropdown segment (1) was clicked, show the editor menu
if let segmented = sender as? NSSegmentedControl, segmented.selectedSegment == 1 {
if let menu = segmented.menu(forSegment: 1) {
let screenRect = segmented.window?.convertToScreen(
segmented.convert(segmented.bounds, to: nil)
) ?? .zero
let origin = NSPoint(x: screenRect.minX, y: screenRect.minY)
menu.popUp(positioning: nil, at: origin, in: nil)
}
return
}

guard let editor = WorktrunkPreferences.preferredEditor else { return }
openIn(editor: editor)
}

@objc func openInSpecificEditor(_ sender: Any?) {
guard let menuItem = sender as? NSMenuItem,
let editor = menuItem.representedObject as? ExternalEditor else { return }
openIn(editor: editor)
}

private func openIn(editor: ExternalEditor) {
guard let appURL = editor.appURL else { return }
let cwd = currentEditorPath()
guard let cwd else { return }

WorktrunkPreferences.lastEditor = editor
refreshEditorToolbarIcon(for: editor)

let url = URL(fileURLWithPath: cwd)
let config = NSWorkspace.OpenConfiguration()
NSWorkspace.shared.open([url], withApplicationAt: appURL, configuration: config)
}

private func refreshEditorToolbarIcon(for editor: ExternalEditor) {
guard let toolbar = window?.toolbar else { return }
for item in toolbar.items {
guard item.itemIdentifier == .openInEditor,
let segmented = item.view as? NSSegmentedControl else { continue }
EditorSplitButton.updateIcon(segmented, editor: editor)
break
}
}

private func currentEditorPath() -> String? {
// Prefer selected worktree path from sidebar, fall back to focused surface pwd
switch worktrunkSidebarState.selection {
case .worktree(_, let path):
return path
case .session(_, _, let worktreePath):
return worktreePath
Comment on lines +1910 to +1914

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use focused terminal cwd before sidebar selection

currentEditorPath() currently returns the selected worktree/session path whenever the sidebar has a selection, which means the new toolbar action opens the worktree root instead of the active terminal's actual pwd for common cases like working in a subdirectory. In practice, users clicking “Open in Editor” from a tab at /repo/subdir will be sent to /repo if the sidebar selection is on that worktree/session, so the feature does not open the current working directory as intended.

Useful? React with 👍 / 👎.

default:
return focusedSurface?.pwd
}
}

private func resumeAISession(_ session: AISession) {
var base = Ghostty.SurfaceConfiguration()
base.workingDirectory = session.cwd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,23 +286,24 @@ class TitlebarTabsTahoeTerminalWindow: TransparentTitlebarTerminalWindow, NSTool
// MARK: NSToolbarDelegate

func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
var items: [NSToolbarItem.Identifier] = [
[
.toggleSidebar,
.sidebarTrackingSeparator,
.title,
.flexibleSpace,
.space,
.openInEditor,
]
return items
}

func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
return [
[
.toggleSidebar,
.sidebarTrackingSeparator,
.flexibleSpace,
.title,
.flexibleSpace,
.openInEditor,
]
}

Expand Down Expand Up @@ -335,11 +336,32 @@ class TitlebarTabsTahoeTerminalWindow: TransparentTitlebarTerminalWindow, NSTool
item.label = "Toggle Sidebar"
item.isNavigational = true
return item
case .openInEditor:
return makeOpenInEditorItem()
default:
return NSToolbarItem(itemIdentifier: itemIdentifier)
}
}

private func makeOpenInEditorItem() -> NSToolbarItem? {
let installed = ExternalEditor.installedEditors()
guard !installed.isEmpty else { return nil }

let controller = windowController as? TerminalController

let item = NSToolbarItem(itemIdentifier: .openInEditor)
item.label = "Open in Editor"
item.toolTip = "Open in Editor"

let segmented = EditorSplitButton.make(
editors: installed,
target: controller
)

item.view = segmented
return item
}

// MARK: SwiftUI

class ViewModel: ObservableObject {
Expand Down
154 changes: 154 additions & 0 deletions macos/Sources/Features/Worktrunk/WorktrunkPreferences.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AppKit
import Foundation

enum WorktrunkAgent: String, CaseIterable, Identifiable {
Expand Down Expand Up @@ -132,12 +133,147 @@ enum WorktrunkOpenBehavior: String, CaseIterable, Identifiable {
}
}

enum ExternalEditorCategory: String {
case editors
case git
case finder
}

enum ExternalEditor: String, CaseIterable, Identifiable {
// Editors
case cursor
case vscode
case vscodium
case zed
case sublime
case nova
case textmate
case xcode
// JetBrains
case intellij
case webstorm
case pycharm
case goland
case rubymine
case clion
case rider
case phpstorm
case fleet
// Git clients
case tower
case fork
case gitkraken
case sourcetree
case githubDesktop
// Finder
case finder

var id: String { rawValue }

var category: ExternalEditorCategory {
switch self {
case .tower, .fork, .gitkraken, .sourcetree, .githubDesktop:
return .git
case .finder:
return .finder
default:
return .editors
}
}

var title: String {
switch self {
case .cursor: return "Cursor"
case .vscode: return "VS Code"
case .vscodium: return "VSCodium"
case .zed: return "Zed"
case .sublime: return "Sublime Text"
case .nova: return "Nova"
case .xcode: return "Xcode"
case .textmate: return "TextMate"
case .intellij: return "IntelliJ IDEA"
case .webstorm: return "WebStorm"
case .pycharm: return "PyCharm"
case .goland: return "GoLand"
case .rubymine: return "RubyMine"
case .clion: return "CLion"
case .rider: return "Rider"
case .phpstorm: return "PhpStorm"
case .fleet: return "Fleet"
case .tower: return "Tower"
case .fork: return "Fork"
case .gitkraken: return "GitKraken"
case .sourcetree: return "Sourcetree"
case .githubDesktop: return "GitHub Desktop"
case .finder: return "Finder"
}
}

var bundleIdentifier: String {
switch self {
case .cursor: return "com.todesktop.230313mzl4w4u92"
case .vscode: return "com.microsoft.VSCode"
case .vscodium: return "com.vscodium"
case .zed: return "dev.zed.Zed"
case .sublime: return "com.sublimetext.4"
case .nova: return "com.panic.Nova"
case .xcode: return "com.apple.dt.Xcode"
case .textmate: return "com.macromates.TextMate"
case .intellij: return "com.jetbrains.intellij"
case .webstorm: return "com.jetbrains.WebStorm"
case .pycharm: return "com.jetbrains.pycharm"
case .goland: return "com.jetbrains.goland"
case .rubymine: return "com.jetbrains.rubymine"
case .clion: return "com.jetbrains.CLion"
case .rider: return "com.jetbrains.rider"
case .phpstorm: return "com.jetbrains.PhpStorm"
case .fleet: return "fleet.app"
case .tower: return "com.fournova.Tower3"
case .fork: return "com.DanPristupov.Fork"
case .gitkraken: return "com.axosoft.gitkraken"
case .sourcetree: return "com.torusknot.SourceTreeNotMAS"
case .githubDesktop: return "com.github.GitHubClient"
case .finder: return "com.apple.finder"
}
}

var appURL: URL? {
NSWorkspace.shared.urlForApplication(withBundleIdentifier: bundleIdentifier)
}

var isInstalled: Bool {
appURL != nil
}

var appIcon: NSImage? {
guard let url = appURL else { return nil }
return NSWorkspace.shared.icon(forFile: url.path)
}

static func installedEditors() -> [ExternalEditor] {
allCases.filter { $0.isInstalled }
}

static func installedByCategory() -> [(category: ExternalEditorCategory, editors: [ExternalEditor])] {
let installed = installedEditors()
var result: [(category: ExternalEditorCategory, editors: [ExternalEditor])] = []
for cat in [ExternalEditorCategory.editors, .git, .finder] {
let group = installed.filter { $0.category == cat }
if !group.isEmpty {
result.append((category: cat, editors: group))
}
}
return result
}
}

enum WorktrunkPreferences {
static let openBehaviorKey = "GhosttyWorktrunkOpenBehavior.v1"
static let worktreeTabsKey = "GhosttyWorktreeTabs.v1"
static let sidebarTabsKey = "GhostreeWorktrunkSidebarTabs.v1"
static let defaultAgentKey = "GhosttyWorktrunkDefaultAgent.v1"
static let githubIntegrationKey = "GhostreeGitHubIntegration.v1"
static let lastEditorKey = "GhostreeLastEditor.v1"

static var worktreeTabsEnabled: Bool {
UserDefaults.standard.bool(forKey: worktreeTabsKey)
Expand All @@ -157,4 +293,22 @@ enum WorktrunkPreferences {
}
return UserDefaults.standard.bool(forKey: githubIntegrationKey)
}

static var lastEditor: ExternalEditor? {
get {
guard let raw = UserDefaults.standard.string(forKey: lastEditorKey) else { return nil }
return ExternalEditor(rawValue: raw)
}
set {
UserDefaults.standard.set(newValue?.rawValue, forKey: lastEditorKey)
}
}

static var preferredEditor: ExternalEditor? {
let installed = ExternalEditor.installedEditors()
if let last = lastEditor, installed.contains(last) {
return last
}
return installed.first
}
}
Loading
Loading