Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/web/src/terminal-links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ describe("extractTerminalLinks", () => {
]);
});

it.each([
Comment thread
maria-rcks marked this conversation as resolved.
Resolved
["https://example.com/a(b)c)", "https://example.com/a(b)c"],
["https://example.com/(docs)", "https://example.com/(docs)"],
["https://example.com/docs)}])", "https://example.com/docs)"],
["https://example.com/docs):", "https://example.com/docs):"],
["/tmp/a[b]c]", "/tmp/a[b]c"],
["/tmp/docs)}])", "/tmp/docs)"],
["/tmp/docs):", "/tmp/docs"],
])("trims only unbalanced closing delimiters from %s", (raw, text) => {
expect(extractTerminalLinks(`see ${raw}`)).toEqual([
{ kind: isTerminalUrl(text) ? "url" : "path", text, start: 4, end: 4 + text.length },
]);
});

it.each([
"src/żółć.ts:12:7",
"żółć/main.ts:12",
Expand Down
16 changes: 8 additions & 8 deletions apps/web/src/terminal-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const URL_PATTERN = /https?:\/\/[^\s"'`<>]+/giu;
const POWERSHELL_PROMPT_PATH_PATTERN = /^PS ((?:[A-Za-z]:[\\/]|\\\\|\/)[^>\r\n]*)>/u;
// Bare paths start only at a segment boundary: hover detection runs this on
// every frame, so separator-free output must not be rescanned at every suffix.
// For the same reason `]` needs its `[`: trimming a long run of `]` is quadratic.
// Parenthesized path segments need a following slash to distinguish compiler locations.
const FILE_PATH_PATTERN =
/(?:~[\\/]|\.{1,2}[\\/]|\/|[A-Za-z]:[\\/]|\\\\)[^\s"'`<>]+|(?<![\p{L}\p{M}\p{N}._-])[\p{L}\p{M}\p{N}._-]+(?:[\\/](?:[\p{L}\p{M}\p{N}._$+@-]|\[{1,2}[\p{L}\p{M}\p{N}._-]+\]{1,2}|\([\p{L}\p{M}\p{N}._-]+\)(?=[\\/]))+)+(?:(?::\d+){1,2}|\(\d+,\d+\)(?=:\s+(?:error|warning) TS\d+:))?/gu;
Expand All @@ -59,25 +58,26 @@ const SCHEME_URL_PATTERN = /(?<![A-Za-z\d+.-])[+-]?[A-Za-z][A-Za-z\d+.-]+:\/\/[^
const SCP_REMOTE_PATTERN = /(?<![\w.@-])[\w.-]+@[\w.-]+:[^\s"'`<>]+/gu;

function trimClosingDelimiters(value: string, kind: TerminalLinkKind): string {
let output = value.replace(
const output = value.replace(
kind === "path" ? TRAILING_PATH_PUNCTUATION_PATTERN : TRAILING_PUNCTUATION_PATTERN,
"",
);
if (output.length === 0) return output;
let end = output.length;

const trimUnbalanced = (open: string, close: string) => {
while (output.endsWith(close)) {
const opens = output.split(open).length - 1;
const closes = output.split(close).length - 1;
if (opens >= closes) return;
output = output.slice(0, -1);
if (output[end - 1] !== close) return;
let excess = output.split(close).length - output.split(open).length;
while (excess > 0 && output[end - 1] === close) {
end -= 1;
excess -= 1;
}
};

trimUnbalanced("(", ")");
trimUnbalanced("[", "]");
trimUnbalanced("{", "}");
return output;
return output.slice(0, end);
}

function overlaps(a: { start: number; end: number }, b: { start: number; end: number }): boolean {
Expand Down
Loading