-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add unified surface selection context API #10022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
austinywang
wants to merge
17
commits into
main
Choose a base branch
from
issue-10020-surface-selection-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5269a5a
Test unified surface selection reads
austinywang 932782b
Implement unified surface selection reads
austinywang 2f6ba70
Harden surface selection contracts
austinywang 3caaad1
Use modern SwiftUI change callback
austinywang 64428da
Fix read-selection RPC example
austinywang 8c48965
Mount WebKit selection behavior fixture
austinywang 8f87781
Retain WebKit selections across focus handoff
austinywang 47144ca
Track WebKit selection in the page world
austinywang 6389dd0
Preserve WebKit selections across native focus
austinywang 8af3343
Treat WebKit focus reconciliation as observational
austinywang d8d907f
Exercise WebKit selection callbacks in page world
austinywang e977f30
Probe WebKit selection tracker state
austinywang 79ee7df
Make WebKit selection reads snapshot-only
austinywang 4dbee45
Preserve browser navigation lifecycle in selection test
austinywang 5e76a32
Use local URL for browser selection fixture
austinywang a6e6b68
Merge remote-tracking branch 'origin/main' into issue-10020-surface-s…
austinywang 3cf798d
Stabilize browser portal layout assertions
austinywang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| import Foundation | ||
|
|
||
| extension CMUXCLI { | ||
| func runSurfaceSelectionCommand( | ||
| commandName: String, | ||
| commandArgs: [String], | ||
| client: SocketClient, | ||
| jsonOutput: Bool, | ||
| windowOverride: String?, | ||
| includeContextInPlainOutput: Bool | ||
| ) throws { | ||
| let (workspaceOption, remainingAfterWorkspace) = parseOption( | ||
| commandArgs, | ||
| name: "--workspace" | ||
| ) | ||
| let (surfaceOption, remainingAfterSurface) = parseOption( | ||
| remainingAfterWorkspace, | ||
| name: "--surface" | ||
| ) | ||
| let (windowOption, trailing) = parseOption( | ||
| remainingAfterSurface, | ||
| name: "--window" | ||
| ) | ||
| guard trailing.isEmpty else { | ||
| throw CLIError(message: String( | ||
| format: String( | ||
| localized: "cli.readSelection.error.unexpectedArguments", | ||
| defaultValue: "%@: unexpected arguments: %@" | ||
| ), | ||
| commandName, | ||
| trailing.joined(separator: " ") | ||
| )) | ||
| } | ||
|
|
||
| let windowRaw = windowOption ?? windowOverride | ||
| let workspaceRaw = workspaceOption | ||
| ?? Self.callerWorkspaceForSurfaceHandle(surfaceOption, windowRaw: windowRaw) | ||
| let surfaceRaw = surfaceOption | ||
| ?? (workspaceOption == nil && windowRaw == nil | ||
| ? ProcessInfo.processInfo.environment["CMUX_SURFACE_ID"] | ||
| : nil) | ||
|
|
||
| var params: [String: Any] = [:] | ||
| let windowID = try normalizeWindowHandle(windowRaw, client: client) | ||
| if let windowID { | ||
| params["window_id"] = windowID | ||
| } | ||
| let workspaceID = try normalizeWorkspaceHandle( | ||
| workspaceRaw, | ||
| client: client, | ||
| windowHandle: windowID | ||
| ) | ||
| if let workspaceID { | ||
| params["workspace_id"] = workspaceID | ||
| } | ||
| let surfaceID = try normalizeSurfaceHandle( | ||
| surfaceRaw, | ||
| client: client, | ||
| workspaceHandle: workspaceID, | ||
| windowHandle: windowID | ||
| ) | ||
| if let surfaceID { | ||
| params["surface_id"] = surfaceID | ||
| } | ||
|
|
||
| let payload = try client.sendV2( | ||
| method: "surface.read_selection", | ||
| params: params | ||
| ) | ||
| if jsonOutput { | ||
| print(jsonString(payload)) | ||
| return | ||
| } | ||
| guard (payload["has_selection"] as? Bool) == true else { | ||
| throw CLIError(message: String( | ||
| format: String( | ||
| localized: "cli.readSelection.error.noActiveSelection", | ||
| defaultValue: "%@: no active selection" | ||
| ), | ||
| commandName | ||
| )) | ||
| } | ||
|
|
||
| let text = (payload["text"] as? String) ?? "" | ||
| guard includeContextInPlainOutput else { | ||
| print(text) | ||
| return | ||
| } | ||
|
|
||
| let metadata = surfaceSelectionMetadataLines(payload) | ||
| if !metadata.isEmpty { | ||
| print(metadata.joined(separator: "\n")) | ||
| print("") | ||
| } | ||
| print(text) | ||
| } | ||
|
|
||
| private func surfaceSelectionMetadataLines( | ||
| _ payload: [String: Any] | ||
| ) -> [String] { | ||
| var lines: [String] = [] | ||
| if let kind = payload["kind"] as? String, !kind.isEmpty { | ||
| lines.append(String( | ||
| format: String( | ||
| localized: "cli.readSelection.output.kind", | ||
| defaultValue: "Kind: %@" | ||
| ), | ||
| kind | ||
| )) | ||
| } | ||
| if let filePath = payload["file_path"] as? String, !filePath.isEmpty { | ||
| lines.append(String( | ||
| format: String( | ||
| localized: "cli.readSelection.output.file", | ||
| defaultValue: "File: %@" | ||
| ), | ||
| filePath | ||
| )) | ||
| } | ||
| if let range = payload["line_range"] as? [String: Any], | ||
| let start = surfaceSelectionLineNumber(range["start"]), | ||
| let end = surfaceSelectionLineNumber(range["end"]) { | ||
| if start == end { | ||
| lines.append(String( | ||
| format: String( | ||
| localized: "cli.readSelection.output.line", | ||
| defaultValue: "Line: %lld" | ||
| ), | ||
| Int64(start) | ||
| )) | ||
| } else { | ||
| lines.append(String( | ||
| format: String( | ||
| localized: "cli.readSelection.output.lines", | ||
| defaultValue: "Lines: %lld-%lld" | ||
| ), | ||
| Int64(start), | ||
| Int64(end) | ||
| )) | ||
| } | ||
| } | ||
| if let url = payload["url"] as? String, !url.isEmpty { | ||
| lines.append(String( | ||
| format: String( | ||
| localized: "cli.readSelection.output.url", | ||
| defaultValue: "URL: %@" | ||
| ), | ||
| url | ||
| )) | ||
| } | ||
| return lines | ||
| } | ||
|
|
||
| private func surfaceSelectionLineNumber(_ value: Any?) -> Int? { | ||
| if let value = value as? Int { | ||
| return value | ||
| } | ||
| return (value as? NSNumber)?.intValue | ||
| } | ||
|
|
||
| static var readSelectionHelp: String { | ||
| String(localized: "cli.help.readSelection", defaultValue: """ | ||
| Usage: cmux read-selection [flags] | ||
|
|
||
| Read the active selection from any selectable surface. Plain output includes source context; --json returns the complete response. | ||
|
|
||
| Flags: | ||
| --workspace <id|ref|index> Target workspace (default: $CMUX_WORKSPACE_ID) | ||
| --surface <id|ref|index> Target surface (default: $CMUX_SURFACE_ID) | ||
| --window <id|ref|index> Window context for workspace/surface refs and indexes | ||
|
|
||
| Example: | ||
| cmux read-selection --surface surface:2 | ||
| cmux read-selection --surface surface:2 --json | ||
| """) | ||
| } | ||
|
|
||
| static var readScreenHelp: String { | ||
| String(localized: "cli.help.readScreen", defaultValue: """ | ||
| Usage: cmux read-screen [flags] | ||
|
|
||
| Read terminal text from a surface as plain text. | ||
|
|
||
| Flags: | ||
| --workspace <id|ref|index> Target workspace (default: $CMUX_WORKSPACE_ID) | ||
| --surface <id|ref|index> Target surface (default: $CMUX_SURFACE_ID) | ||
| --window <id|ref|index> Window context for workspace/surface refs and indexes | ||
| --scrollback Include scrollback (not just visible viewport) | ||
| --lines <n> Limit to the last n lines (implies --scrollback) | ||
| --selection Read only the active selection; cannot be combined with --scrollback or --lines | ||
|
|
||
| Example: | ||
| cmux read-screen | ||
| cmux read-screen --surface surface:2 --scrollback --lines 200 | ||
| cmux read-screen --surface surface:2 --selection | ||
| """) | ||
| } | ||
|
|
||
| static var readSelectionUsageLine: String { | ||
| String( | ||
| localized: "cli.usage.readSelection", | ||
| defaultValue: "read-selection [--workspace <id|ref|index>] [--surface <id|ref|index>] [--window <id|ref|index>]" | ||
| ) | ||
| } | ||
|
|
||
| static var readScreenUsageLine: String { | ||
| String( | ||
| localized: "cli.usage.readScreen", | ||
| defaultValue: "read-screen [--workspace <id|ref|index>] [--surface <id|ref|index>] [--window <id|ref|index>] [--scrollback] [--lines <n>] [--selection]" | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.