Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(terminal): Shift+Cmd click opens a file link after a click elsewhere (#1176) (#1235)

eneskircadddce54bd93a
diff --git a/src/renderer/terminal/file-links.shift-anchor.test.ts b/src/renderer/terminal/file-links.shift-anchor.test.ts
new file mode 100644
index 000000000..6eead769f
--- /dev/null
+++ b/src/renderer/terminal/file-links.shift-anchor.test.ts
@@ -0,0 +1,94 @@
+// @vitest-environment jsdom
+// Issue #1176 — "Cmd+Shift+click does nothing until I Cmd+click the same filename first."
+//
+// With mouse reporting OFF (no tmux, tmux disabled, the Windows session host) xterm's own
+// SelectionService handles the press, and a Shift press EXTENDS the selection from the last click's
+// anchor (`_handleIncrementalClick`) — an anchor every plain click leaves behind, invisible because a
+// click selects nothing. So a Shift+Cmd click on a link created a selection from wherever the user
+// last clicked to the link, and the provider's "a selection means this was a selection gesture"
+// guard aborted the open in silence. A Cmd-click on the same cell first moved the anchor there, so
+// the extension was empty and the next Shift+Cmd click opened — exactly the report. This drives a
+// REAL xterm so the anchor behaviour is xterm's, not a stub's.
+import { afterEach, describe, expect, it } from 'vitest'
+import { Terminal, type ILink } from '@xterm/xterm'
+import { createFileLinkProvider } from './file-links'
+
+const CW = 10
+const CH = 20
+const LINE = 'wrote out/report.csv and more'
+
+let host: HTMLDivElement | undefined
+afterEach(() => host?.remove())
+
+async function setup() {
+ ;(window as unknown as { matchMedia?: unknown }).matchMedia ??= () => ({
+ matches: false,
+ addListener() {},
+ removeListener() {},
+ addEventListener() {},
+ removeEventListener() {}
+ })
+ // jsdom has no layout: give xterm's DOM char measurement a size, and the screen a rect.
+ Object.defineProperty(HTMLElement.prototype, 'offsetWidth', { configurable: true, get: () => 32 * CW })
+ Object.defineProperty(HTMLElement.prototype, 'offsetHeight', { configurable: true, get: () => CH })
+ host = document.createElement('div')
+ document.body.appendChild(host)
+ const term = new Terminal({ cols: 40, rows: 5 })
+ term.open(host)
+ await new Promise<void>((r) => term.write(LINE + '\r\n', r))
+ const screen = host.querySelector('.xterm-screen') as HTMLElement
+ screen.getBoundingClientRect = () =>
+ ({ left: 0, top: 0, right: 400, bottom: 100, width: 400, height: 100, x: 0, y: 0, toJSON() {} }) as DOMRect
+ const calls: string[] = []
+ const provider = createFileLinkProvider(term, {
+ getCwd: () => '/p',
+ lookup: async (abs) => ({ exists: abs === '/p/out/report.csv', dir: false }),
+ convention: () => ({}),
+ activate: (abs) => calls.push(`open ${abs}`),
+ openWithSystem: (abs) => calls.push(`system ${abs}`)
+ })
+ const links = await new Promise<ILink[]>((resolve) => provider.provideLinks(1, (l) => resolve(l ?? [])))
+ const link = links.find((l) => l.text === 'out/report.csv')!
+ const at = (col: number, o: MouseEventInit = {}): MouseEventInit => ({
+ clientX: col * CW + 2,
+ clientY: 5,
+ button: 0,
+ bubbles: true,
+ cancelable: true,
+ detail: 1,
+ ...o
+ })
+ /** One click as xterm sees it: press on the screen, release on the document, then the link's
+ * activate (xterm's linkifier calls it on a click that started and ended on the link). */
+ const click = (col: number, mods: MouseEventInit = {}, onLink = false) => {
+ screen.dispatchEvent(new MouseEvent('mousedown', at(col, mods)))
+ document.dispatchEvent(new MouseEvent('mouseup', at(col, mods)))
+ if (onLink) link.activate(new MouseEvent('mouseup', at(col, mods)), link.text)
+ }
+ return { term, link, calls, click }
+}
+
+describe('Shift+Cmd click on a file link with mouse reporting off (#1176)', () => {
+ it('xterm itself turns the Shift press into a selection from the last click', async () => {
+ const { term, click } = await setup()
+ click(26) // a plain click somewhere else on the line
+ expect(term.hasSelection()).toBe(false)
+ click(8, { shiftKey: true, metaKey: true })
+ expect(term.hasSelection()).toBe(true) // the stale anchor, extended to the link
+ })
+
+ it('opens with the system app after a plain click elsewhere, and leaves no selection', async () => {
+ const { term, calls, click } = await setup()
+ click(26)
+ click(8, { shiftKey: true, metaKey: true }, true)
+ expect(calls).toEqual(['system /p/out/report.csv'])
+ expect(term.hasSelection()).toBe(false)
+ })
+
+ it('still treats a Shift+Cmd click that extends a REAL selection as a selection gesture', async () => {
+ const { term, calls, click } = await setup()
+ term.select(22, 0, 4) // the user had selected "and " before the click
+ click(8, { shiftKey: true, metaKey: true }, true)
+ expect(calls).toEqual([])
+ })
+})
diff --git a/src/renderer/terminal/file-links.ts b/src/renderer/terminal/file-links.ts
index 225a3cdbb..ec5e5d938 100644
--- a/src/renderer/terminal/file-links.ts
+++ b/src/renderer/terminal/file-links.ts
@@ -714,8 +714,27 @@ function fileCandidates(
return out
}
+/**
+ * Whether the terminal HAD a selection when the current press began — read in a capture-phase
+ * `mousedown` on `term.element`, i.e. before xterm's SelectionService turns a Shift press into an
+ * extension of the last click's anchor. Without an element (or before any press) it answers the
+ * live selection, the old rule. The listener lives and dies with the terminal's element.
+ */
+function trackSelectionAtPress(term: Terminal): () => boolean {
+ let atPress: boolean | null = null
+ term.element?.addEventListener(
+ 'mousedown',
+ (ev) => {
+ if (ev.button === 0) atPress = term.hasSelection()
+ },
+ { capture: true }
+ )
+ return () => atPress ?? term.hasSelection()
+}
+
/** xterm link provider for file paths. Register once per terminal with a reachable filesystem. */
export function createFileLinkProvider(term: Terminal, deps: FileLinkDeps): ILinkProvider {
+ const selectedAtPress = trackSelectionAtPress(term)
return {
provideLinks(bufferLineNumber: number, callback: (links: ILink[] | undefined) => void): void {
// Resolve the paragraph CONTAINING the hovered row (not just one starting at it), so
@@ -752,9 +771,12 @@ export function createFileLinkProvider(term: Terminal, deps: FileLinkDeps): ILin
if (intent === 'none') return
sink?.leave()
if (intent === 'system') {
- // Shift also extends an xterm selection: a Shift+Cmd click that left one behind
- // was a selection gesture, not an open.
- if (term.hasSelection()) return
+ // Shift also extends an xterm selection. Extending a selection the user HAD is a
+ // selection gesture, not an open. But xterm extends from the last click's anchor
+ // even when nothing was selected (every plain click leaves one, invisibly), so the
+ // selection the press itself made is not evidence (issue #1176): drop it and open.
+ if (selectedAtPress()) return
+ term.clearSelection()
;(deps.openWithSystem ?? deps.activate)(abs, dir)
return
}