Files
apes-sidebar/src/client/path-line.ts
T
ymac 2c5b7621dd fix(editor): 正文里 [path](path:131) 的链接去掉行号后缀再打开 (#826)
DSH 的 markdown 链接语法只认 GitHub 的 `#L131` 片段:目的地没有 `#` 时
`parseFileLink` 把整串当文件名返回。于是模型按编译器诊断的通用写法输出的
`[a/b.c](a/b.c:131)` 变成一次对 `b.c:131` 的文件打开,编辑器读不到、右侧栏
红字报「文件不存在」。

宿主源码不可改,纠正放在插件唯一把文件地址变成合成记录的那一步
(native/index.ts 的 fileParamsOf):尾部行号规格(`:行` / `:起-止` /
`:行:列`)在这里切掉,tab 标题按切完的文件名给(宿主用同一个字符串推导标题,
否则芯片上会写 `b.c:131`)。切完之后读、写、"打开方式"、去重、viewer 匹配
拿到的都是真实路径,所以没有需要重试或记住的状态。

切分规则与宿主的 `#L` 语法对齐:1 起、无前导零、锚在串尾(`data:2024.csv`
和盘符 `C:` 因此原样通过),并拒绝空前缀、只剩一个字符的前缀(`C:131`)和
倒序区间(`a.c:20-12`)。

取舍:从聊天链接打开一个真名以 `:数字` 结尾的文件会丢掉后缀。文件树 / 搜索 /
插件自有流程的路径不经过这一步(只有 `openResource` 的地址会),而 `:` 在
Windows 上本就不是合法文件名字符。

测试:新增 tests/path-line.spec.ts(9 例,含全部「必须原样通过」的反例);
tests/native-surface.spec.ts 补 fileParamsOf 四例 + editor 类型对带行号地址的
标题。typecheck / lint 全绿;全量 1503 passed,tests/locales.spec.ts 的 ja
词典缺 key 在干净 origin/main 上同样失败(既有问题,不在本 PR 范围)。
2026-10-01 22:05:56 +08:00

73 lines
3.2 KiB
TypeScript

/**
* The `path:line` suffix a file reference can carry, split back off.
*
* DSH's markdown link grammar (`parseFileLink` in
* `packages/client/ui-primitives/src/markdown/file-link.ts`) understands ONE
* line syntax: the GitHub-style fragment `path#L131` / `path#L131-L200`. A
* destination without a `#` is taken as the file name VERBATIM, so the other
* convention models actually write — `path:131`, the one compiler
* diagnostics, `grep -n` and every terminal use — reaches the tab as a file
* called `CMakeLists.txt:131`. The editor then reads a path that does not
* exist and the sidebar says so (#826).
*
* The host owns that grammar and this plugin may not patch it, so the
* correction happens where the plugin already turns a file ADDRESS into a tab
* record: {@link fileParamsOf} in `native/index.ts`. Everything downstream
* (the tab title, the viewer match, the read, the save, "open with") then sees
* a real path, so there is nothing left to retry and nothing to remember.
*
* The trade is explicit: a file whose NAME really ends in `:<digits>` opened
* FROM A CHAT LINK loses its suffix. Paths that come from the file tree, the
* search box or the sidebar's own flows never pass through this module, and
* `:` is not even a legal filename character on Windows.
*/
/** One line reference split off a path. */
export interface LineSpec {
/** The path with the spec removed — the file the reference points at. */
readonly path: string
/** 1-based first line. */
readonly line: number
/** 1-based last line; equal to {@link line} unless the spec was a range. */
readonly end: number
/** 1-based column, when the spec carried one (`path:12:5`). */
readonly column: number | undefined
}
/**
* A trailing line spec: `:<line>`, `:<line>-<end>` or `:<line>:<column>`.
*
* Line numbers are 1-based with no leading zero, exactly as the host's own
* fragment rule (`^L([1-9]\d*)`) — so `version:0` and a bare `:0` are file
* names, not line references. The regex is anchored at the END of the string,
* which is what keeps `data:2024.csv` and `C:` intact.
*/
const TRAILING_LINE_SPEC = /:([1-9]\d{0,8})(?:-([1-9]\d{0,8}))?(?::([1-9]\d{0,8}))?$/u
/**
* Split a trailing `path:line` spec off a path.
*
* Rejected on purpose, because stripping them would break a path that is
* already a valid file name: a spec with nothing in front of it (`:131`), a
* one-character remainder (`C:131`, where `C` could only be a drive letter),
* and an inverted range (`a.c:20-12`) — the host rejects that one too.
* @param path - a path as a caller holds it, in either separator spelling.
* @returns the path without the spec plus the line it named, or `undefined`
* when the path carries no trailing line spec.
*/
export function splitTrailingLineSpec(path: string): LineSpec | undefined {
const match = TRAILING_LINE_SPEC.exec(path)
if (match === null) return undefined
const head = path.slice(0, match.index)
if (head.length < 2) return undefined
const line = Number(match[1])
const end = match[2] === undefined ? line : Number(match[2])
if (end < line) return undefined
return {
path: head,
line,
end,
column: match[3] === undefined ? undefined : Number(match[3]),
}
}