Files
apes-sidebar/tests/path-line.spec.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

81 lines
3.2 KiB
TypeScript

/**
* Unit tests for the `path:line` spec splitter (src/client/path-line.ts).
*
* The rule under test is the one DSH's own markdown grammar cannot express:
* it reads a `#`-less link destination as a file name VERBATIM, so
* `[a/b.c](a/b.c:131)` addresses `b.c:131` (#826). Every case below is a
* spelling a model, a compiler diagnostic or a terminal emits — and a short
* list of ones that must survive untouched because they are already valid
* file names.
*/
import { describe, expect, it } from 'vitest'
import { splitTrailingLineSpec } from '../src/client/path-line.ts'
describe('splitTrailingLineSpec', () => {
it('splits a bare line spec off a relative path', () => {
expect(splitTrailingLineSpec('omlx/custom_kernels/bonsai/csrc/CMakeLists.txt:131')).toEqual({
path: 'omlx/custom_kernels/bonsai/csrc/CMakeLists.txt',
line: 131,
end: 131,
column: undefined,
})
})
it('splits a range spec, reporting both ends', () => {
expect(splitTrailingLineSpec('src/main.ts:42-56')).toMatchObject({
path: 'src/main.ts',
line: 42,
end: 56,
})
})
it('splits a line:column spec (the shape the LSP hover tool prints)', () => {
expect(splitTrailingLineSpec('src/main.ts:42:7')).toMatchObject({
path: 'src/main.ts',
line: 42,
end: 42,
column: 7,
})
})
it('splits a spec off an absolute POSIX path and a Windows path', () => {
expect(splitTrailingLineSpec('/work/pkg/a/b.txt:5')?.path).toBe('/work/pkg/a/b.txt')
expect(splitTrailingLineSpec('C:\\work\\pkg\\a\\b.txt:5')?.path).toBe('C:\\work\\pkg\\a\\b.txt')
expect(splitTrailingLineSpec('//server/share/a.txt:5')?.path).toBe('//server/share/a.txt')
})
it('splits a spec off an extensionless name', () => {
expect(splitTrailingLineSpec('Makefile:12')).toMatchObject({ path: 'Makefile', line: 12 })
})
it('leaves a path without a spec alone', () => {
expect(splitTrailingLineSpec('src/main.ts')).toBeUndefined()
expect(splitTrailingLineSpec('')).toBeUndefined()
})
it('leaves names that merely contain a colon alone', () => {
// A version-ish or namespaced name: the spec has to END the string.
expect(splitTrailingLineSpec('data:2024.csv')).toBeUndefined()
expect(splitTrailingLineSpec('schema:name.json')).toBeUndefined()
// A bare drive, with and without a spec — `C` is not a file name.
expect(splitTrailingLineSpec('C:')).toBeUndefined()
expect(splitTrailingLineSpec('C:131')).toBeUndefined()
// Nothing in front of the spec.
expect(splitTrailingLineSpec(':131')).toBeUndefined()
// Trailing colon, no digits.
expect(splitTrailingLineSpec('src/main.ts:')).toBeUndefined()
expect(splitTrailingLineSpec('src/main.ts:build')).toBeUndefined()
})
it('refuses a zero or zero-padded line, exactly like the host fragment rule', () => {
// The host's `#L` grammar is ^L([1-9]\d*) — 1-based, no leading zero.
expect(splitTrailingLineSpec('a.c:0')).toBeUndefined()
expect(splitTrailingLineSpec('a.c:012')).toBeUndefined()
expect(splitTrailingLineSpec('a.c:1')).toMatchObject({ line: 1 })
})
it('refuses an inverted range instead of silently reordering it', () => {
expect(splitTrailingLineSpec('a.c:20-12')).toBeUndefined()
})
})