diff --git a/src/features/terminal/links.ts b/src/features/terminal/links.ts index 0d60c1934..7073ca2c9 100644 --- a/src/features/terminal/links.ts +++ b/src/features/terminal/links.ts @@ -34,11 +34,35 @@ export function trimUrl(url: string): string { } } +/** + * A `file://` URL (compiler output such as Kotlin's `file:///src/A.kt:15:56`) as a file location: + * it opens in the editor, on the machine the terminal runs on, like a plain path. + */ +function fileUrlTarget(url: string): { path: string; line?: number; col?: number } | null { + const m = /^file:\/\/[^/]*(\/.*?)(?::(\d+)(?::(\d+))?)?$/i.exec(url); + if (!m) return null; + let path: string; + try { + path = decodeURIComponent(m[1]!); + } catch { + return null; + } + // `file:///C:/x` is the Windows path `C:/x`. + if (/^\/[A-Za-z]:\//.test(path)) path = path.slice(1); + return { path, line: m[2] ? Number(m[2]) : undefined, col: m[3] ? Number(m[3]) : undefined }; +} + export function findTerminalLinks(text: string): TerminalLink[] { const links: TerminalLink[] = []; for (const m of text.matchAll(URL_RE)) { const url = trimUrl(m[0]); - links.push({ kind: "url", start: m.index, end: m.index + url.length, text: url, url }); + const end = m.index + url.length; + const file = fileUrlTarget(url); + links.push( + file + ? { kind: "file", start: m.index, end, text: url, ...file } + : { kind: "url", start: m.index, end, text: url, url }, + ); } const overlaps = (s: number, e: number) => links.some((l) => s < l.end && e > l.start); for (const m of text.matchAll(FILE_RE)) { diff --git a/src/features/terminal/terminal.test.ts b/src/features/terminal/terminal.test.ts index c7451a4e9..c572e5863 100644 --- a/src/features/terminal/terminal.test.ts +++ b/src/features/terminal/terminal.test.ts @@ -99,6 +99,19 @@ describe("terminal links", () => { expect(findTerminalLinks("12:30 http://localhost:3000").map((l) => l.kind)).toEqual(["url"]); }); + it("opens file:// URLs as file locations", () => { + const line = "[ERROR] file:///develop/mc/src/main/kotlin/top/A%20B.kt:15:56 Unresolved reference 'x'."; + expect(findTerminalLinks(line)).toEqual([ + expect.objectContaining({ kind: "file", path: "/develop/mc/src/main/kotlin/top/A B.kt", line: 15, col: 56 }), + ]); + expect(findTerminalLinks("see file:///C:/work/a.kt:3")).toEqual([ + expect.objectContaining({ kind: "file", path: "C:/work/a.kt", line: 3, col: undefined }), + ]); + expect(findTerminalLinks("file://host/srv/x.txt")).toEqual([ + expect.objectContaining({ kind: "file", path: "/srv/x.txt", line: undefined }), + ]); + }); + it("trims trailing punctuation from URLs", () => { expect(trimUrl("https://a.dev/x.")).toBe("https://a.dev/x"); expect(trimUrl("https://a.dev/(x)")).toBe("https://a.dev/(x)");