fix(explorer): separate completed directory references

This commit is contained in:
wang shi zhuo committed 2026-09-07 18:52:18 +08:00
1 parent cb271e0d5a
commit 1ad9ac7506
3 files changed
+33 -7

No files matched your search

+5 -3
View File
@@ -59,7 +59,7 @@ interface SpliceResult {
*/
function spliceInsert(draft: string, text: string, caret: DraftCaret | null): SpliceResult {
if (caret === null || draft === '') {
const next = draft.trim() === '' ? text : `${draft} ${text}`
const next = draft.trim() === '' ? text : `${draft}${/\s$/.test(draft) ? '' : ' '}${text}`
return { draft: next, caretAfter: next.length }
}
const prefix = draft.slice(0, caret.start)
@@ -69,7 +69,7 @@ function spliceInsert(draft: string, text: string, caret: DraftCaret | null): Sp
// (or the string edges) — mirrors how typing in the middle of a sentence
// behaves.
const left = prefix === '' || /\s$/.test(prefix) ? '' : ' '
const right = suffix === '' || /^\s/.test(suffix) ? '' : ' '
const right = suffix === '' || /^\s/.test(suffix) || /\s$/.test(text) ? '' : ' '
return {
draft: `${prefix}${left}${text}${right}${suffix}`,
caretAfter: prefix.length + left.length + text.length,
@@ -245,7 +245,9 @@ export function insertWorkspaceReference(ctx: Context, sessionId: string, path:
return false
}
if (!isDir && insertFileReference(ctx, sessionId, relative)) return true
return appendToDraft(ctx, sessionId, mention)
// The host adds space after a file chip, but not before it. Complete
// directory inserts need their own separator for the next @ click.
return appendToDraft(ctx, sessionId, isDir ? `${mention} ` : mention)
} catch (error) {
console.warn('[dsh-better-sidebar] workspace-reference insert failed:', error)
return false
+9
View File
@@ -92,6 +92,15 @@ describe('insertAtCaret', () => {
expect(insertAtCaret(' ', 'X', null)).toBe('X')
})
it('keeps a directory separator when appending another reference', () => {
expect(insertAtCaret('@src/ ', '@notes.md', null)).toBe('@src/ @notes.md')
expect(insertAtCaret('@src/\n', '@notes.md', null)).toBe('@src/\n@notes.md')
})
it('uses the inserted directory separator before a following word', () => {
expect(insertAtCaret('hello world', '@src/ ', { start: 6, end: 6 })).toBe('hello @src/ world')
})
it('inserts at the caret in the middle of a sentence with one space each side', () => {
expect(insertAtCaret('hello world', 'CODE', { start: 5, end: 5 })).toBe('hello CODE world')
})
+19 -4
View File
@@ -74,7 +74,7 @@ describe('Sidebar workspace references', () => {
const view = mount(cwd, { root: '/work' })
try {
view.insert('/work/my dir', true)
expect(view.draft()).toBe('before @"my dir/"')
expect(view.draft()).toBe('before @"my dir/" ')
expect(view.emit).not.toHaveBeenCalled()
} finally { view.unmount() }
})
@@ -91,6 +91,21 @@ describe('Sidebar workspace references', () => {
} finally { view.unmount() }
})
it.each(['src', 'my dir', 'docs/my dir'])('separates a directory %s from the next file chip', directory => {
const view = mount('/work', { root: '/work', chip: true })
try {
view.insert(`/work/${directory}`, true)
const beforeFile = view.draft()
expect(beforeFile).toMatch(/\s$/)
view.insert('/work/notes.md', false)
expect(view.emit).toHaveBeenCalledWith('slash/input-insert-reference', {
reference: { source: 'reference', ref: '@notes.md', label: 'notes.md', appearance: 'file', clipboardText: '@notes.md' },
span: { draftRev: 2, start: beforeFile.length, end: beforeFile.length },
})
expect(view.setDraft).toHaveBeenCalledTimes(1)
} finally { view.unmount() }
})
it('keeps quoted file text when the chip event is not handled', () => {
const view = mount('/work', { root: '/work' })
try {
@@ -103,9 +118,9 @@ describe('Sidebar workspace references', () => {
const view = mount('C:\\Work\\src', { root: 'C:\\Work\\' })
try {
view.insert('c:/WORK/my dir\\nested', true)
expect(view.draft()).toBe('before @"my dir/nested/"')
expect(view.draft()).toBe('before @"my dir/nested/" ')
view.insert('c:/work', true)
expect(view.draft()).toBe('before @"my dir/nested/" @./')
expect(view.draft()).toBe('before @"my dir/nested/" @./ ')
} finally { view.unmount() }
})
@@ -125,7 +140,7 @@ describe('Sidebar workspace references', () => {
// Resolve the host snapshot later: the next click must see fresh data.
Object.assign(options, { root: '/work', phase: 'ready', state: 'idle', workspaceService: true })
view.insert('/work/src', true)
expect(view.draft()).toBe('before @src/')
expect(view.draft()).toBe('before @src/ ')
} finally { view.unmount() }
})