From 1ad9ac75068261ffcf4e595d7e2d71dfde7808bb Mon Sep 17 00:00:00 2001 From: wang shi zhuo <147490150+bulingbuling688@users.noreply.github.com> Date: Mon, 7 Sep 2026 18:52:18 +0800 Subject: [PATCH] fix(explorer): separate completed directory references --- src/client/conversation-draft.ts | 8 +++++--- tests/conversation-draft.spec.ts | 9 +++++++++ tests/sidebar-workspace-reference.spec.tsx | 23 ++++++++++++++++++---- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/client/conversation-draft.ts b/src/client/conversation-draft.ts index 23c30688..9936cf1b 100644 --- a/src/client/conversation-draft.ts +++ b/src/client/conversation-draft.ts @@ -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 diff --git a/tests/conversation-draft.spec.ts b/tests/conversation-draft.spec.ts index 3953c7d6..aaf88164 100644 --- a/tests/conversation-draft.spec.ts +++ b/tests/conversation-draft.spec.ts @@ -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') }) diff --git a/tests/sidebar-workspace-reference.spec.tsx b/tests/sidebar-workspace-reference.spec.tsx index 6e41402d..0b5bcc51 100644 --- a/tests/sidebar-workspace-reference.spec.tsx +++ b/tests/sidebar-workspace-reference.spec.tsx @@ -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() } })