Skip to content

Commit d256170

Browse files
fix: file links with line ranges open at their first line (#34006)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 44e894f commit d256170

9 files changed

Lines changed: 69 additions & 10 deletions

File tree

‎apps/mobile/src/lib/enrichedLinkPresentation.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ describe("Enriched link presentation", () => {
2020
"/tmp/report.md",
2121
"C:\\repo\\app.tsx",
2222
"app.tsx:12:3",
23+
"src/http.ts:42-64",
24+
"http.ts:42-64",
2325
]) {
2426
expect(ENRICHED_INLINE_FILE_LINK_REGEX.test(path), path).toBe(true);
2527
}
2628
for (const text of [
2729
"npm run dev",
2830
"port:3000",
31+
"port:3000-3010",
2932
"TODO:12",
3033
"example.com",
3134
"https://example.com/app.tsx",

‎apps/mobile/src/lib/enrichedLinkPresentation.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function enrichedLinkVariantPattern(url: string) {
9292
}
9393

9494
export const ENRICHED_INLINE_FILE_LINK_REGEX =
95-
/^(?!\S*:\/\/)(?:(?:\.{1,2}[\\/]|~[\\/]|\/(?:Users|home|tmp|var|etc|opt|mnt|Volumes|private|root|usr|bin|sbin|lib|lib64|srv|dev|proc|sys|run|boot|media|workspace|workspaces)\/|[A-Za-z]:[\\/])[^\s`]+|(?:[A-Za-z0-9._-]+[\\/])+[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+(?::\d+){0,2}|[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+:\d+(?::\d+)?)$/;
95+
/^(?!\S*:\/\/)(?:(?:\.{1,2}[\\/]|~[\\/]|\/(?:Users|home|tmp|var|etc|opt|mnt|Volumes|private|root|usr|bin|sbin|lib|lib64|srv|dev|proc|sys|run|boot|media|workspace|workspaces)\/|[A-Za-z]:[\\/])[^\s`]+|(?:[A-Za-z0-9._-]+[\\/])+[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+(?::\d+(?::\d+|-\d+)?)?|[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+:\d+(?::\d+|-\d+)?)$/;
9696

9797
export function enrichedSkillLinkRegex(skills?: ReadonlyArray<SelectableMarkdownSkill>) {
9898
const names = [...new Set(skills?.map((skill) => skill.name) ?? [])].filter(Boolean);

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ function useChatMarkdownState({
850850
path: fileLinkMeta.filePath,
851851
...(fileLinkMeta.line !== undefined ? { line: fileLinkMeta.line } : {}),
852852
...(fileLinkMeta.column !== undefined ? { column: fileLinkMeta.column } : {}),
853+
...(fileLinkMeta.endLine !== undefined ? { endLine: fileLinkMeta.endLine } : {}),
853854
},
854855
fileLinkParentSuffixByPath,
855856
)}

‎apps/web/src/markdown-links.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,23 @@ describe("resolveInlineCodeFileLinkMeta", () => {
9797
expect(resolveInlineCodeFileLinkMeta("AGENTS.md", "/Users/julius/project")).toBeNull();
9898
});
9999

100+
it("opens a line range at its first line and keeps the range for the label", () => {
101+
expect(resolveInlineCodeFileLinkMeta("src/http.ts:42-64", "/repo")).toMatchObject({
102+
targetPath: "/repo/src/http.ts:42",
103+
line: 42,
104+
endLine: 64,
105+
});
106+
});
107+
100108
it("does not treat arbitrary name:digits shapes as files", () => {
101109
expect(resolveInlineCodeFileLinkMeta("error:1", "/Users/julius/project")).toBeNull();
102110
expect(resolveInlineCodeFileLinkMeta("TODO:12", "/Users/julius/project")).toBeNull();
103111
expect(resolveInlineCodeFileLinkMeta("exit:0", "/Users/julius/project")).toBeNull();
104112
expect(resolveInlineCodeFileLinkMeta("port:3000", "/Users/julius/project")).toBeNull();
113+
expect(resolveInlineCodeFileLinkMeta("port:3000-3010", "/Users/julius/project")).toBeNull();
114+
expect(
115+
resolveInlineCodeFileLinkMeta("localhost:3000-3010", "/Users/julius/project"),
116+
).toBeNull();
105117
expect(resolveInlineCodeFileLinkMeta("http:80", "/Users/julius/project")).toBeNull();
106118
});
107119

‎apps/web/src/markdown-links.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface MarkdownFileLinkMeta {
2121
basename: string;
2222
line?: number;
2323
column?: number;
24+
endLine?: number;
2425
}
2526

2627
export function shouldOpenMarkdownFileLinkInEditor(
@@ -84,6 +85,7 @@ export function resolveMarkdownFileLinkMeta(
8485
const source = href ? parseMarkdownFileLink(href) : null;
8586
return {
8687
...buildFileLinkMetaFromTarget(targetPath, cwd),
88+
...(source?.endLine !== undefined ? { endLine: source.endLine } : {}),
8789
workspaceLookupPath:
8890
source !== null &&
8991
!isAbsolutePath(source.path) &&

‎packages/shared/src/fileLinks.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { describe, expect, it } from "vite-plus/test";
2-
import { parseFileUrlHref, splitFilePathPosition, resolvePathLinkTarget } from "./fileLinks.ts";
2+
import {
3+
fileLinkLabel,
4+
parseFileUrlHref,
5+
splitFilePathPosition,
6+
resolvePathLinkTarget,
7+
} from "./fileLinks.ts";
38

49
describe("parseFileUrlHref", () => {
510
it.each([
@@ -32,6 +37,9 @@ describe("splitFilePathPosition", () => {
3237
["src/main.ts", "#L18C2", { path: "src/main.ts", line: 18, column: 2 }],
3338
["src/main.ts:3", "#L18C2", { path: "src/main.ts", line: 3 }],
3439
["src/main.ts:0", "", { path: "src/main.ts" }],
40+
["src/main.ts:42-64", "", { path: "src/main.ts", line: 42, endLine: 64 }],
41+
["src/main.ts", "#L42-L64", { path: "src/main.ts", line: 42, endLine: 64 }],
42+
["src/main.ts:42-42", "", { path: "src/main.ts", line: 42 }],
3543
])("splits %s%s", (path, hash, expected) => {
3644
expect(splitFilePathPosition(path, hash)).toEqual(expected);
3745
});
@@ -74,3 +82,12 @@ describe("resolvePathLinkTarget", () => {
7482
}
7583
});
7684
});
85+
86+
describe("fileLinkLabel", () => {
87+
it.each([
88+
[{ path: "/repo/src/http.ts", line: 42, column: 7 }, "http.ts · L42:C7"],
89+
[{ path: "/repo/src/http.ts", line: 42, endLine: 64 }, "http.ts · L42–64"],
90+
])("labels %o", (file, label) => {
91+
expect(fileLinkLabel(file, new Map())).toBe(label);
92+
});
93+
});

‎packages/shared/src/fileLinks.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import {
55
stripSlashPrefixedWindowsDrive,
66
} from "./path.ts";
77

8-
const POSITION_SUFFIX_CAPTURE_PATTERN = /:(\d+)(?::(\d+))?$/;
8+
const POSITION_SUFFIX_CAPTURE_PATTERN = /:(\d+)(?::(\d+)|-(\d+))?$/;
99

10-
const POSITION_HASH_PATTERN = /^#L(\d+)(?:C(\d+))?$/i;
10+
const POSITION_HASH_PATTERN = /^#L(\d+)(?:C(\d+)|-L?(\d+))?$/i;
1111

1212
/** Keeps paths percent-encoded to avoid double decoding; non-localhost authorities become UNC shares. */
1313
export function parseFileUrlHref(
@@ -32,6 +32,8 @@ export interface FilePathPosition {
3232
readonly path: string;
3333
readonly line?: number;
3434
readonly column?: number;
35+
/** The last line of a range such as `:42-64`. Editors open at `line`. */
36+
readonly endLine?: number;
3537
}
3638

3739
export function splitFilePathPosition(path: string, hash = ""): FilePathPosition {
@@ -41,10 +43,12 @@ export function splitFilePathPosition(path: string, hash = ""): FilePathPosition
4143

4244
const line = Number.parseInt(match[1], 10);
4345
const column = match[2] === undefined ? undefined : Number.parseInt(match[2], 10);
46+
const endLine = match[3] === undefined ? undefined : Number.parseInt(match[3], 10);
4447
return {
4548
path: suffixMatch ? path.slice(0, -suffixMatch[0].length) : path,
4649
...(line > 0 ? { line } : {}),
4750
...(column !== undefined && column > 0 ? { column } : {}),
51+
...(line > 0 && endLine !== undefined && endLine > line ? { endLine } : {}),
4852
};
4953
}
5054

@@ -171,7 +175,9 @@ export function fileLinkLabel(
171175
return [
172176
fileBasename(file.path),
173177
suffix,
174-
file.line ? `L${file.line}${file.column ? `:C${file.column}` : ""}` : undefined,
178+
file.line
179+
? `L${file.line}${file.endLine ? `–${file.endLine}` : file.column ? `:C${file.column}` : ""}`
180+
: undefined,
175181
]
176182
.filter(Boolean)
177183
.join(" · ");

‎packages/shared/src/markdownLinks.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ describe("isMarkdownFileLinkLabel", () => {
1515
["validates the input", "/repo/src/example.ts:12", false],
1616
["example.ts:12", "/repo/src/example.ts:12", true],
1717
["example.ts:99", "/repo/src/example.ts:12", false],
18+
["example.ts:12-20", "/repo/src/example.ts:12-20", true],
19+
["example.ts:12-20", "/repo/src/example.ts:12", false],
1820
])("classifies %s for %s", (label, href, expected) => {
1921
expect(isMarkdownFileLinkLabel(label, href)).toBe(expected);
2022
});
@@ -25,6 +27,8 @@ describe("inlineCodeFilePathCandidate", () => {
2527
it.each([
2628
["src\\main.ts", "src/main.ts"],
2729
["allocator.cc:217", "allocator.cc:217"],
30+
["src/http.ts:42-64", "src/http.ts:42-64"],
31+
["http.ts:42-64", "http.ts:42-64"],
2832
["origin/main", null],
2933
["z-ai/glm-5.3", null],
3034
["share/man/ls.1", "share/man/ls.1"],
@@ -101,6 +105,19 @@ describe("parseMarkdownFileLink", () => {
101105
expect(parseMarkdownFileLink("/srv/app/Makefile")).toEqual({ path: "/srv/app/Makefile" });
102106
});
103107

108+
it("reads line ranges and opens them at their first line", () => {
109+
expect(parseMarkdownFileLink("/repo/src/http.ts:42-64")).toEqual({
110+
path: "/repo/src/http.ts",
111+
line: 42,
112+
endLine: 64,
113+
});
114+
expect(parseMarkdownFileLink("/repo/src/http.ts#L42-L64")).toEqual({
115+
path: "/repo/src/http.ts",
116+
line: 42,
117+
endLine: 64,
118+
});
119+
});
120+
104121
it("reads positions from suffixes and line anchors", () => {
105122
expect(parseMarkdownFileLink("/Users/julius/project/src/main.ts#L42C7")).toEqual({
106123
path: "/Users/julius/project/src/main.ts",

‎packages/shared/src/markdownLinks.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;
1818
// `(group)/[id]/+page.svelte`, `@scope/pkg`). The final segment is optional so an explicit
1919
// folder link may end in `/` (`patches/`, `docs/My Folder/`). A bare name with no slash still fails.
2020
const RELATIVE_FILE_PATH_PATTERN =
21-
/^(?:[\p{L}\p{M}\p{N}._$+@()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+@()[\]-]+)*\/)+(?:[\p{L}\p{M}\p{N}._$+@()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+@()[\]-]+)*(?::\d+){0,2})?$/u;
21+
/^(?:[\p{L}\p{M}\p{N}._$+@()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+@()[\]-]+)*\/)+(?:[\p{L}\p{M}\p{N}._$+@()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+@()[\]-]+)*(?::\d+(?::\d+|-\d+)?)?)?$/u;
2222

2323
// No `@` here: `[email protected]` is an address, not a file.
2424
const RELATIVE_FILE_NAME_PATTERN =
25-
/^[\p{L}\p{M}\p{N}._$+()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+()[\]-]+)*\.[\p{L}\p{M}\p{N}_-]+(?::\d+){0,2}$/u;
25+
/^[\p{L}\p{M}\p{N}._$+()[\]-]+(?: +[\p{L}\p{M}\p{N}._$+()[\]-]+)*\.[\p{L}\p{M}\p{N}_-]+(?::\d+(?::\d+|-\d+)?)?$/u;
2626

2727
const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/;
2828

29-
const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+)?$/;
29+
const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+|-\d+)?$/;
3030

31-
const POSITION_ONLY_PATTERN = /^\d+(?::\d+)?$/;
31+
const POSITION_ONLY_PATTERN = /^\d+(?::\d+|-\d+)?$/;
3232

3333
const INLINE_CODE_DISQUALIFIER_PATTERN = /[\s`]/;
3434

@@ -256,7 +256,8 @@ export function isMarkdownFileLinkLabel(label: string, href: string): boolean {
256256
const labelPosition = splitFilePathPosition(label.trim());
257257
if (
258258
(labelPosition.line !== undefined && labelPosition.line !== destination.line) ||
259-
(labelPosition.column !== undefined && labelPosition.column !== destination.column)
259+
(labelPosition.column !== undefined && labelPosition.column !== destination.column) ||
260+
(labelPosition.endLine !== undefined && labelPosition.endLine !== destination.endLine)
260261
) {
261262
return false;
262263
}

0 commit comments

Comments
 (0)