The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 13:30 UTC
perf(web): trim terminal link delimiters in linear time (#34397)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Lakshmi Tanmay committed
Nov 15, 2026 at 01:06 UTC
6ea5af70adba6b1cc4c23261f002e2e64cf5b30f
2 files changed
+22
-8
apps/web/src/terminal-links.test.ts
+14
@@ -124,6 +124,20 @@ describe("extractTerminalLinks", () => {
124
]);
125
});
126
127
+ it.each([
128
+ ["https://example.com/a(b)c)", "https://example.com/a(b)c"],
129
+ ["https://example.com/(docs)", "https://example.com/(docs)"],
130
+ ["https://example.com/docs)}])", "https://example.com/docs)"],
131
+ ["https://example.com/docs):", "https://example.com/docs):"],
132
+ ["/tmp/a[b]c]", "/tmp/a[b]c"],
133
+ ["/tmp/docs)}])", "/tmp/docs)"],
134
+ ["/tmp/docs):", "/tmp/docs"],
135
+ ])("trims only unbalanced closing delimiters from %s", (raw, text) => {
136
+ expect(extractTerminalLinks(`see ${raw}`)).toEqual([
137
+ { kind: isTerminalUrl(text) ? "url" : "path", text, start: 4, end: 4 + text.length },
138
+ ]);
139
+ });
140
+
141
it.each([
142
"src/żółć.ts:12:7",
143
"żółć/main.ts:12",
apps/web/src/terminal-links.ts
+8
-8
@@ -39,7 +39,6 @@ const URL_PATTERN = /https?:\/\/[^\s"'`<>]+/giu;
39
const POWERSHELL_PROMPT_PATH_PATTERN = /^PS ((?:[A-Za-z]:[\\/]|\\\\|\/)[^>\r\n]*)>/u;
40
// Bare paths start only at a segment boundary: hover detection runs this on
41
// every frame, so separator-free output must not be rescanned at every suffix.
42
-// For the same reason `]` needs its `[`: trimming a long run of `]` is quadratic.
42
// Parenthesized path segments need a following slash to distinguish compiler locations.
43
const FILE_PATH_PATTERN =
44
/(?:~[\\/]|\.{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;
@@ -59,25 +58,26 @@ const SCHEME_URL_PATTERN = /(?<![A-Za-z\d+.-])[+-]?[A-Za-z][A-Za-z\d+.-]+:\/\/[^
58
const SCP_REMOTE_PATTERN = /(?<![\w.@-])[\w.-]+@[\w.-]+:[^\s"'`<>]+/gu;
59
60
function trimClosingDelimiters(value: string, kind: TerminalLinkKind): string {
62
- let output = value.replace(
61
+ const output = value.replace(
62
kind === "path" ? TRAILING_PATH_PUNCTUATION_PATTERN : TRAILING_PUNCTUATION_PATTERN,
63
"",
64
);
65
if (output.length === 0) return output;
66
+ let end = output.length;
67
68
const trimUnbalanced = (open: string, close: string) => {
69
- while (output.endsWith(close)) {
70
- const opens = output.split(open).length - 1;
71
- const closes = output.split(close).length - 1;
72
- if (opens >= closes) return;
73
- output = output.slice(0, -1);
69
+ if (output[end - 1] !== close) return;
70
+ let excess = output.split(close).length - output.split(open).length;
71
+ while (excess > 0 && output[end - 1] === close) {
72
+ end -= 1;
73
+ excess -= 1;
74
}
75
};
76
77
trimUnbalanced("(", ")");
78
trimUnbalanced("[", "]");
79
trimUnbalanced("{", "}");
80
- return output;
80
+ return output.slice(0, end);
81
}
82
83
function overlaps(a: { start: number; end: number }, b: { start: number; end: number }): boolean {