mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
fix(terminal): open file:// links in the editor (#3996)
Compiler output such as Kotlin's "file:///develop/app/src/A.kt:15:56 Unresolved reference" was linked as a URL and handed to the system opener: in a remote window it failed with "Not allowed to open url", and locally it opened the file in another app. A file:// URL is now a file location with its line and column, opened in the editor on the machine the terminal runs on, as the Run console already does. Refs #3447
This commit is contained in:
@@ -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)) {
|
||||
|
||||
@@ -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)");
|
||||
|
||||
Reference in New Issue
Block a user