diff --git a/src/renderer/src/components/terminal-pane/terminal-link-handlers.test.ts b/src/renderer/src/components/terminal-pane/terminal-link-handlers.test.ts index 4e0aeb7b8..d767824bf 100644 --- a/src/renderer/src/components/terminal-pane/terminal-link-handlers.test.ts +++ b/src/renderer/src/components/terminal-pane/terminal-link-handlers.test.ts @@ -215,6 +215,31 @@ describe('handleOscLink', () => { expect.objectContaining({ filePath: '/tmp/test.txt' }) ) }) + + it('opens relative OSC file links against the terminal cwd', async () => { + setPlatform('Macintosh') + + handleOscLink( + 'docs/README.md', + { metaKey: true, ctrlKey: false }, + { + ...deps, + startupCwd: '/tmp/project' + } + ) + + await new Promise((resolve) => setTimeout(resolve, 0)) + + expect(authorizeExternalPathMock).toHaveBeenCalledWith({ + targetPath: '/tmp/project/docs/README.md' + }) + expect(openFileMock).toHaveBeenCalledWith( + expect.objectContaining({ + filePath: '/tmp/project/docs/README.md', + relativePath: 'project/docs/README.md' + }) + ) + }) }) describe('createFilePathLinkProvider range bounds', () => { diff --git a/src/renderer/src/components/terminal-pane/terminal-link-handlers.ts b/src/renderer/src/components/terminal-pane/terminal-link-handlers.ts index adf95fcf8..13af7f80b 100644 --- a/src/renderer/src/components/terminal-pane/terminal-link-handlers.ts +++ b/src/renderer/src/components/terminal-pane/terminal-link-handlers.ts @@ -4,6 +4,7 @@ import { extractTerminalFileLinks, isPathInsideWorktree, resolveTerminalFileLink, + resolveTerminalFileLinkText, toWorktreeRelativePath } from '@/lib/terminal-links' import { useAppStore } from '@/store' @@ -233,7 +234,8 @@ export function isTerminalLinkActivation( export function handleOscLink( rawText: string, event: TerminalLinkEvent | undefined, - deps: Pick + deps: Pick & + Partial> ): void { if (!isTerminalLinkActivation(event)) { return @@ -254,6 +256,10 @@ export function handleOscLink( try { parsed = new URL(rawText) } catch { + const resolved = resolveTerminalFileLinkText(rawText, deps.startupCwd || deps.worktreePath) + if (resolved) { + openDetectedFilePath(resolved.absolutePath, resolved.line, resolved.column, deps) + } return } diff --git a/src/renderer/src/lib/terminal-links.test.ts b/src/renderer/src/lib/terminal-links.test.ts index c8a0525a9..acc16b497 100644 --- a/src/renderer/src/lib/terminal-links.test.ts +++ b/src/renderer/src/lib/terminal-links.test.ts @@ -3,6 +3,7 @@ import { extractTerminalFileLinks, isPathInsideWorktree, resolveTerminalFileLink, + resolveTerminalFileLinkText, toWorktreeRelativePath } from './terminal-links' @@ -72,4 +73,24 @@ describe('terminal path helpers', () => { column: 3 }) }) + + it('resolves exact repo-relative OSC hyperlink text', () => { + expect(resolveTerminalFileLinkText('docs/README.md', '/repo')).toEqual({ + absolutePath: '/repo/docs/README.md', + line: null, + column: null + }) + }) + + it('keeps line and column suffixes from exact OSC hyperlink text', () => { + expect(resolveTerminalFileLinkText('docs/README.md:12:3', '/repo')).toEqual({ + absolutePath: '/repo/docs/README.md', + line: 12, + column: 3 + }) + }) + + it('does not resolve partial text as an OSC hyperlink target', () => { + expect(resolveTerminalFileLinkText('open docs/README.md', '/repo')).toBeNull() + }) }) diff --git a/src/renderer/src/lib/terminal-links.ts b/src/renderer/src/lib/terminal-links.ts index 676e3abaa..55a113b7a 100644 --- a/src/renderer/src/lib/terminal-links.ts +++ b/src/renderer/src/lib/terminal-links.ts @@ -332,6 +332,15 @@ export function resolveTerminalFileLink( } } +export function resolveTerminalFileLinkText( + linkText: string, + cwd: string +): ResolvedTerminalFileLink | null { + const links = extractTerminalFileLinks(linkText) + const exactLink = links.find((link) => link.startIndex === 0 && link.endIndex === linkText.length) + return exactLink ? resolveTerminalFileLink(exactLink, cwd) : null +} + export function isPathInsideWorktree(filePath: string, worktreePath: string): boolean { const normalizedFile = normalizeAbsolutePath(filePath) const normalizedWorktree = normalizeAbsolutePath(worktreePath)