Skip to content
Open
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
110 changes: 110 additions & 0 deletions CLI/cmux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3333,6 +3333,10 @@ struct CMUXCLI {
case "markdown":
try runMarkdownCommand(commandArgs: commandArgs, client: client, jsonOutput: jsonOutput, idFormat: idFormat)

// Editor commands
case "editor":
try runEditorCommand(commandArgs: commandArgs, client: client, jsonOutput: jsonOutput, idFormat: idFormat)

default:
print(usage())
throw CLIError(message: "Unknown command: \(command)")
Expand Down Expand Up @@ -3477,6 +3481,85 @@ struct CMUXCLI {
}
}

private func runEditorCommand(
commandArgs: [String],
client: SocketClient,
jsonOutput: Bool,
idFormat: CLIIDFormat
) throws {
var args = commandArgs

// Parse routing flags
let (workspaceOpt, argsAfterWorkspace) = parseOption(args, name: "--workspace")
let (windowOpt, argsAfterWindow) = parseOption(argsAfterWorkspace, name: "--window")
let (surfaceOpt, argsAfterSurface) = parseOption(argsAfterWindow, name: "--surface")
let (directionOpt, argsAfterDirection) = parseOption(argsAfterSurface, name: "--direction")
args = argsAfterDirection

let subcommand: String
var subArgs: [String]
if let first = args.first, ["open", "diff"].contains(first.lowercased()) {
subcommand = first.lowercased()
subArgs = Array(args.dropFirst())
} else if args.count == 1, let first = args.first, !first.hasPrefix("-") {
subcommand = "open"
subArgs = [first]
} else if let first = args.first, first.hasPrefix("-") {
throw CLIError(
message: "editor: unknown flag '\(first)'. Usage: cmux editor open <path> | cmux editor diff <path>"
)
} else if let first = args.first, looksLikePath(first) || first.contains(".") {
subcommand = "open"
subArgs = args
} else if let first = args.first {
throw CLIError(message: "Unknown editor subcommand: \(first). Usage: cmux editor open <path> | cmux editor diff <path>")
} else {
throw CLIError(message: "editor requires a subcommand. Usage: cmux editor open <path> | cmux editor diff <path>")
}

guard let rawPath = subArgs.first, !rawPath.isEmpty else {
throw CLIError(message: "editor \(subcommand) requires a file path. Usage: cmux editor \(subcommand) <path>")
}
Comment on lines +3501 to +3522

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Reject extra editor arguments instead of silently ignoring them.

At Line 3520, only subArgs.first is consumed, so trailing tokens are dropped. That makes malformed invocations appear successful and hides user mistakes.

Suggested fix
-        guard let rawPath = subArgs.first, !rawPath.isEmpty else {
+        guard let rawPath = subArgs.first, !rawPath.isEmpty else {
             throw CLIError(message: "editor \(subcommand) requires a file path. Usage: cmux editor \(subcommand) <path>")
         }
+        if subArgs.count > 1 {
+            let extras = Array(subArgs.dropFirst())
+            if let badFlag = extras.first(where: { $0.hasPrefix("-") }) {
+                throw CLIError(
+                    message: "editor: unknown flag '\(badFlag)'. Usage: cmux editor open <path> | cmux editor diff <path>"
+                )
+            }
+            throw CLIError(
+                message: "editor \(subcommand) accepts exactly one <path>; unexpected arguments: \(extras.joined(separator: " "))"
+            )
+        }

Based on learnings: In CLI/cmux.swift, set-workspace-color enforces exactly one trailing argument and rejects unexpected positional arguments to prevent malformed input.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@CLI/cmux.swift` around lines 3501 - 3522, The editor subcommand parsing
currently only consumes subArgs.first and silently ignores extra positional
tokens (args), so update the logic around subArgs/args handling to reject any
additional positional arguments: after you set subArgs (in the branches that set
subcommand/open/diff and the branch that assigns subArgs = args), validate that
subArgs.count == 1 (or throw a CLIError with the same Usage text) before
proceeding to unwrap rawPath; specifically add a check using subArgs.count and
throw an error if > 1 to prevent trailing tokens from being dropped (references:
variables subArgs, args, subcommand, rawPath and the existing error messages).


let absolutePath = resolvePath(rawPath)
let direction = directionOpt ?? "right"

var params: [String: Any] = ["path": absolutePath, "direction": direction]
if let surfaceRaw = surfaceOpt {
if let surface = try normalizeSurfaceHandle(surfaceRaw, client: client) {
params["surface_id"] = surface
}
}
let workspaceRaw = workspaceOpt ?? (windowOpt == nil ? ProcessInfo.processInfo.environment["CMUX_WORKSPACE_ID"] : nil)
if let workspaceRaw {
if let workspace = try normalizeWorkspaceHandle(workspaceRaw, client: client) {
params["workspace_id"] = workspace
}
}
if let windowRaw = windowOpt {
if let window = try normalizeWindowHandle(windowRaw, client: client) {
params["window_id"] = window
}
}

let method = subcommand == "diff" ? "editor.diff" : "editor.open"
let payload = try client.sendV2(method: method, params: params)

if jsonOutput {
print(jsonString(formatIDs(payload, mode: idFormat)))
} else {
let surfaceText = formatHandle(payload, kind: "surface", idFormat: idFormat) ?? "unknown"
let filePath = (payload["path"] as? String) ?? absolutePath
if subcommand == "diff" {
let baseLabel = (payload["base_label"] as? String) ?? "HEAD"
print("OK surface=\(surfaceText) path=\(filePath) diff_base=\(baseLabel)")
} else {
let paneText = formatHandle(payload, kind: "pane", idFormat: idFormat) ?? "unknown"
print("OK surface=\(surfaceText) pane=\(paneText) path=\(filePath)")
}
}
}

/// Returns true if the argument looks like a filesystem path rather than a CLI command.
private func looksLikePath(_ arg: String) -> Bool {
if arg == "." || arg == ".." { return true }
Expand Down Expand Up @@ -9646,6 +9729,31 @@ struct CMUXCLI {
cmux markdown open ./docs/design.md --workspace 0
cmux markdown open plan.md --direction down
"""
case "editor":
return """
Usage: cmux editor open <path> [options]
cmux editor diff <path> [options]
cmux editor <path> (shorthand for 'open')

Open a file in a read-only code viewer panel with Monaco syntax highlighting
and live file watching. Supports diff view against git HEAD.

Subcommands:
open Open a file in the code viewer (default)
diff Open a diff view comparing the file against git HEAD

Options:
--workspace <id|ref|index> Target workspace (default: $CMUX_WORKSPACE_ID)
--surface <id|ref|index> Source surface to split from (default: focused surface)
--window <id|ref|index> Target window
--direction <left|right|up|down> Split direction (default: right)

Examples:
cmux editor open main.swift
cmux editor ~/project/src/app.py
cmux editor diff src/lib.rs
cmux editor open config.yaml --direction down
"""
default:
return nil
}
Expand Down Expand Up @@ -18629,6 +18737,8 @@ export default CMUXSessionRestore;
display-message [-p|--print] <text>

markdown [open] <path> (open markdown file in formatted viewer panel with live reload)
editor [open] <path> (open file in read-only code viewer with syntax highlighting)
editor diff <path> (open file diff against git HEAD)

browser [--surface <id|ref|index> | <surface>] <subcommand> ...
browser open [url] (create browser split in caller's workspace; if surface supplied, behaves like navigate)
Expand Down
12 changes: 12 additions & 0 deletions GhosttyTabs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
6B524A0BA34FD46A771335AB /* WorkspaceUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 71F8ED91A4B55D34BE6A0668 /* WorkspaceUnitTests.swift */; };
734F49D37E543DD01C2F4FEF /* NotificationAndMenuBarTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2C075029771815DD5DA1332 /* NotificationAndMenuBarTests.swift */; };
84E00D47E4584162AE53BC8D /* xterm-ghostty in Resources */ = {isa = PBXBuildFile; fileRef = B2E7294509CC42FE9191870E /* xterm-ghostty */; };
ED10EDCA000000000000001A /* editor-monaco in Resources */ = {isa = PBXBuildFile; fileRef = ED10EDCA000000000000001B /* editor-monaco */; };
8C4BBF2DEF6DF93F395A9EE7 /* TerminalControllerSocketSecurityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 491751CE2321474474F27DCF /* TerminalControllerSocketSecurityTests.swift */; };
A5001001 /* cmuxApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001011 /* cmuxApp.swift */; };
A5001002 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001012 /* ContentView.swift */; };
Expand Down Expand Up @@ -91,6 +92,8 @@
A5001407 /* WorkspaceContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001417 /* WorkspaceContentView.swift */; };
A5001420 /* MarkdownPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001418 /* MarkdownPanel.swift */; };
A5001421 /* MarkdownPanelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001419 /* MarkdownPanelView.swift */; };
ED10EDCA000000000000002A /* EditorPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED10EDCA000000000000002B /* EditorPanel.swift */; };
ED10EDCA000000000000002C /* EditorPanelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED10EDCA000000000000002D /* EditorPanelView.swift */; };
A5001500 /* CmuxWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001510 /* CmuxWebView.swift */; };
A5001501 /* UITestRecorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001511 /* UITestRecorder.swift */; };
A5001521 /* PostHogAnalytics.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5001520 /* PostHogAnalytics.swift */; };
Expand Down Expand Up @@ -336,6 +339,8 @@
A5001417 /* WorkspaceContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkspaceContentView.swift; sourceTree = "<group>"; };
A5001418 /* MarkdownPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Panels/MarkdownPanel.swift; sourceTree = "<group>"; };
A5001419 /* MarkdownPanelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Panels/MarkdownPanelView.swift; sourceTree = "<group>"; };
ED10EDCA000000000000002B /* EditorPanel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Panels/EditorPanel.swift; sourceTree = "<group>"; };
ED10EDCA000000000000002D /* EditorPanelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Panels/EditorPanelView.swift; sourceTree = "<group>"; };
A5001510 /* CmuxWebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Panels/CmuxWebView.swift; sourceTree = "<group>"; };
A5001511 /* UITestRecorder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UITestRecorder.swift; sourceTree = "<group>"; };
A5001520 /* PostHogAnalytics.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogAnalytics.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -419,6 +424,7 @@
FE003002 /* SessionIndexView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionIndexView.swift; sourceTree = "<group>"; };
FE003003 /* RightSidebarPanelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RightSidebarPanelView.swift; sourceTree = "<group>"; };
IC000002 /* AppIcon.icon */ = {isa = PBXFileReference; lastKnownFileType = folder; path = AppIcon.icon; sourceTree = "<group>"; };
ED10EDCA000000000000001B /* editor-monaco */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "editor-monaco"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -479,6 +485,7 @@
DA7A10CA710E000000000002 /* InfoPlist.xcstrings */,
FEED0000000000000000F006 /* opencode-plugin.js */,
A5001622 /* cmux.sdef */,
ED10EDCA000000000000001B /* editor-monaco */,
);
path = Resources;
sourceTree = "<group>";
Expand Down Expand Up @@ -622,6 +629,8 @@
A5007423 /* BrowserWebAuthnSupport.swift */,
A5001418 /* MarkdownPanel.swift */,
A5001419 /* MarkdownPanelView.swift */,
ED10EDCA000000000000002B /* EditorPanel.swift */,
ED10EDCA000000000000002D /* EditorPanelView.swift */,
A5001510 /* CmuxWebView.swift */,
A5001415 /* PanelContentView.swift */,
A5001211 /* UpdateController.swift */,
Expand Down Expand Up @@ -899,6 +908,7 @@
DA7A10CA710E000000000004 /* InfoPlist.xcstrings in Resources */,
FEED0000000000000000F007 /* opencode-plugin.js in Resources */,
A5001623 /* cmux.sdef in Resources */,
ED10EDCA000000000000001A /* editor-monaco in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1007,6 +1017,8 @@
A5007422 /* BrowserWebAuthnSupport.swift in Sources */,
A5001420 /* MarkdownPanel.swift in Sources */,
A5001421 /* MarkdownPanelView.swift in Sources */,
ED10EDCA000000000000002A /* EditorPanel.swift in Sources */,
ED10EDCA000000000000002C /* EditorPanelView.swift in Sources */,
A5001500 /* CmuxWebView.swift in Sources */,
A5001405 /* PanelContentView.swift in Sources */,
A5001201 /* UpdateController.swift in Sources */,
Expand Down
Loading