Fix relative Codex file links in terminal (#1779)
This commit is contained in:
@@ -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', () => {
|
||||
|
||||
@@ -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<LinkHandlerDeps, 'worktreeId' | 'worktreePath'>
|
||||
deps: Pick<LinkHandlerDeps, 'worktreeId' | 'worktreePath'> &
|
||||
Partial<Pick<LinkHandlerDeps, 'startupCwd'>>
|
||||
): 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
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user