Make plain-text file:// links clickable in the terminal (#9467)
* Make plain-text file:// links clickable in the terminal Printed file:// URIs (e.g. a report path echoed by a tool or agent) were neither http links nor bare filesystem paths, so the terminal's URL and local-path detectors both skipped them and the link was dead. Orca already resolves and opens file:// URIs for OSC 8 hyperlinks. Reuse that exact resolver for plain-text URIs so a printed file:// behaves the same whether or not the emitter wrapped it in an escape sequence: - Promote the (dependency-pure) file-url target resolver into src/shared so the OSC path and the new plain-text path share one implementation. - Add a file:// detector that decodes the URI to a filesystem path and routes it through the existing file-link pipeline (existence probe + openDetectedFilePath), so line/col anchors, %20, Windows drive paths, html-in-browser, editor reveal, and SSH/runtime resolution all just work. Lines without file:// are unchanged: the pass short-circuits to the prior result, so only file://-bearing lines gain a link. - Add unit + integration coverage for detection, decoding, and no-double-link. * Harden plain-text file URI detection * Split terminal file link detection modules
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { resolveTerminalFileUrlTarget } from './terminal-file-url-target'
|
||||
|
||||
describe('resolveTerminalFileUrlTarget', () => {
|
||||
it('resolves UNC file URLs with line and column anchors', () => {
|
||||
expect(
|
||||
resolveTerminalFileUrlTarget(new URL('file://Server/Share/Repo/src/app.ts#L12C3'), {
|
||||
allowUncHost: true
|
||||
})
|
||||
).toEqual({
|
||||
filePath: '//server/Share/Repo/src/app.ts',
|
||||
line: 12,
|
||||
column: 3
|
||||
})
|
||||
})
|
||||
|
||||
it('returns null for malformed file URL escapes', () => {
|
||||
expect(resolveTerminalFileUrlTarget(new URL('file:///tmp/%E0%A4%A.txt'))).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,56 @@
|
||||
import { fileUriToFilesystemPath } from './file-uri-path'
|
||||
|
||||
export type TerminalFileUrlTarget = {
|
||||
filePath: string
|
||||
line: number | null
|
||||
column: number | null
|
||||
}
|
||||
|
||||
export type TerminalFileUrlTargetOptions = {
|
||||
allowUncHost?: boolean
|
||||
}
|
||||
|
||||
function parseFileUrlLineHash(hash: string): { line: number; column: number | null } | null {
|
||||
const trimmed = hash.startsWith('#') ? hash.slice(1) : hash
|
||||
const match = /^L(\d+)(?:C(\d+))?$/i.exec(trimmed)
|
||||
if (!match) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
line: Number(match[1]),
|
||||
column: match[2] ? Number(match[2]) : null
|
||||
}
|
||||
}
|
||||
|
||||
function parseFilePathTrailingLineTarget(filePath: string): TerminalFileUrlTarget | null {
|
||||
const match = /^(.*?)(?::(\d+))(?::(\d+))?$/.exec(filePath)
|
||||
if (!match || !match[1] || match[1].endsWith('/') || match[1].endsWith('\\')) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
filePath: match[1],
|
||||
line: Number(match[2]),
|
||||
column: match[3] ? Number(match[3]) : null
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveTerminalFileUrlTarget(
|
||||
parsed: URL,
|
||||
options: TerminalFileUrlTargetOptions = {}
|
||||
): TerminalFileUrlTarget | null {
|
||||
if (parsed.hostname && parsed.hostname !== 'localhost' && !options.allowUncHost) {
|
||||
return null
|
||||
}
|
||||
|
||||
const filePath = fileUriToFilesystemPath(parsed)
|
||||
if (!filePath) {
|
||||
return null
|
||||
}
|
||||
|
||||
const hashTarget = parseFileUrlLineHash(parsed.hash)
|
||||
if (hashTarget) {
|
||||
return { filePath, line: hashTarget.line, column: hashTarget.column }
|
||||
}
|
||||
|
||||
return parseFilePathTrailingLineTarget(filePath) ?? { filePath, line: null, column: null }
|
||||
}
|
||||
Reference in New Issue
Block a user