宿主的两处 token 读取都只认「引号闭合」的目录写法:
`FOLDER_REF_RE`(dsh-client-ui-conversation)的引号分支是 `@"[^"\n]*\/`,
消息气泡的 `@"[^"\n]+"` 也只在闭合时才把 token 判成 folder;而插件目录分支
原先直接拼 `@${rel}/`,含空格目录 `@my dir/` 在补全扫描里完全不命中、在气泡里
退化成只覆盖 `my` 的 file 芯片——文件分支经 fileMention 加引号,目录分支没有,
同一套引号规则被写了两遍,只有一份对。
把「构造 mention 字面量」抽成 conversation-draft.ts 的共用纯函数
`mentionFor(path, kind)`(fileMention 并入,两个调用点:insertFileReference 与
referenceInChat 的目录分支),沿用原有的控制字符/引号拒绝守卫;目录的尾斜杠拼在
引号内侧(`@"my dir/"`)。宿主自己的选择器在补全途中发的是开引号形式
(`@"my dir/`),那是未完成的补全态、气泡里同样判成 file;本插件的动作插入的是
一条完成的引用,所以闭合引号。
行为不变:无空格目录仍是 `@docs/`、cwd 仍是 `@./`、无空格文件仍是 `@notes.md`,
文件分支的引号与 chip 路径逐字未动。宿主 FOLDER_REF_RE 仍能读到 `@"my dir/`
(闭合引号落在 token 之外),文件夹补全不受影响。
验证:pnpm typecheck(exit 0);pnpm exec vitest run --maxWorkers=3(151 files /
1678 passed / 13 skipped);npx eslint 四个改动文件(exit 0)。
tests/reference-in-chat.spec.ts 把目录分支回退到旧写法后 2/7 红(草稿变成
`@my dir/`、守卫路径重新写入不可表示路径)。guide §4.2 的 onReferenceFile 说明
同步引号形式。
Refs #479
43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { mentionFor } from '../src/client/conversation-draft.ts'
|
|
|
|
describe('mentionFor', () => {
|
|
it('formats a plain relative file path as an @file mention', () => {
|
|
expect(mentionFor('src/client/paths.ts', 'file')).toEqual({
|
|
mention: '@src/client/paths.ts',
|
|
label: 'paths.ts',
|
|
})
|
|
})
|
|
|
|
it('quotes a path containing whitespace and keeps the full basename', () => {
|
|
expect(mentionFor('docs/plan files/design notes.md', 'file')).toEqual({
|
|
mention: '@"docs/plan files/design notes.md"',
|
|
label: 'design notes.md',
|
|
})
|
|
})
|
|
|
|
it('trims a trailing separator before deriving the basename', () => {
|
|
expect(mentionFor('src/client/', 'file')).toEqual({
|
|
mention: '@src/client',
|
|
label: 'client',
|
|
})
|
|
})
|
|
|
|
it('rejects embedded quotes and control characters for both kinds', () => {
|
|
expect(mentionFor('src/a"b.ts', 'file')).toBeUndefined()
|
|
expect(mentionFor('src/a\u0000b.ts', 'file')).toBeUndefined()
|
|
expect(mentionFor('src/a"b', 'folder')).toBeUndefined()
|
|
})
|
|
|
|
it('keeps the trailing slash on a plain folder mention', () => {
|
|
expect(mentionFor('docs', 'folder')).toEqual({ mention: '@docs/', label: 'docs' })
|
|
expect(mentionFor('docs/', 'folder')).toEqual({ mention: '@docs/', label: 'docs' })
|
|
// The cwd itself stays the relative root spelling the composer expects.
|
|
expect(mentionFor('.', 'folder')).toEqual({ mention: '@./', label: '.' })
|
|
})
|
|
|
|
it('closes the quote around the trailing slash of a folder with whitespace', () => {
|
|
expect(mentionFor('my dir', 'folder')).toEqual({ mention: '@"my dir/"', label: 'my dir' })
|
|
})
|
|
})
|