macos: simplify some ServiceProvider code (#7508)

First, remove the always-inlined openTerminalFromPasteboard code and
combine it with openTerminal. Now that we're doing a bit of work inside
openTerminal, there's little better to having an intermediate, inlined
function.

Second, combine some type-casting operations (saving a .map() call).

Lastly, adjust some variable names because a generic `objs` or `urls`
was a little ambiguous now that we're all in one function scope.
pull/7527/head
Mitchell Hashimoto 2025-06-05 07:30:03 -07:00 committed by GitHub
commit d4249679e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 22 deletions

View File

@ -5,7 +5,7 @@ class ServiceProvider: NSObject {
static private let errorNoString = NSString(string: "Could not load any text from the clipboard.") static private let errorNoString = NSString(string: "Could not load any text from the clipboard.")
/// The target for an open operation /// The target for an open operation
enum OpenTarget { private enum OpenTarget {
case tab case tab
case window case window
} }
@ -15,7 +15,7 @@ class ServiceProvider: NSObject {
userData: String?, userData: String?,
error: AutoreleasingUnsafeMutablePointer<NSString> error: AutoreleasingUnsafeMutablePointer<NSString>
) { ) {
openTerminalFromPasteboard(pasteboard: pasteboard, target: .tab, error: error) openTerminal(from: pasteboard, target: .tab, error: error)
} }
@objc func openWindow( @objc func openWindow(
@ -23,40 +23,33 @@ class ServiceProvider: NSObject {
userData: String?, userData: String?,
error: AutoreleasingUnsafeMutablePointer<NSString> error: AutoreleasingUnsafeMutablePointer<NSString>
) { ) {
openTerminalFromPasteboard(pasteboard: pasteboard, target: .window, error: error) openTerminal(from: pasteboard, target: .window, error: error)
} }
@inline(__always) private func openTerminal(
private func openTerminalFromPasteboard( from pasteboard: NSPasteboard,
pasteboard: NSPasteboard,
target: OpenTarget, target: OpenTarget,
error: AutoreleasingUnsafeMutablePointer<NSString> error: AutoreleasingUnsafeMutablePointer<NSString>
) { ) {
guard let objs = pasteboard.readObjects(forClasses: [NSURL.self]) as? [NSURL] else { guard let delegate = NSApp.delegate as? AppDelegate else { return }
let terminalManager = delegate.terminalManager
guard let pathURLs = pasteboard.readObjects(forClasses: [NSURL.self]) as? [URL] else {
error.pointee = Self.errorNoString error.pointee = Self.errorNoString
return return
} }
let urlObjects = objs.map { $0 as URL }
openTerminal(urlObjects, target: target) // Build a set of unique directory URLs to open. File paths are truncated
} // to their directories because that's the only thing we can open.
let directoryURLs = Set(
private func openTerminal(_ urls: [URL], target: OpenTarget) { pathURLs.map { url -> URL in
guard let delegateRaw = NSApp.delegate else { return }
guard let delegate = delegateRaw as? AppDelegate else { return }
let terminalManager = delegate.terminalManager
let uniqueCwds: Set<URL> = Set(
urls.map { url -> URL in
// We only open in directories.
url.hasDirectoryPath ? url : url.deletingLastPathComponent() url.hasDirectoryPath ? url : url.deletingLastPathComponent()
} }
) )
for cwd in uniqueCwds { for url in directoryURLs {
// Build our config
var config = Ghostty.SurfaceConfiguration() var config = Ghostty.SurfaceConfiguration()
config.workingDirectory = cwd.path(percentEncoded: false) config.workingDirectory = url.path(percentEncoded: false)
switch (target) { switch (target) {
case .window: case .window: