diff --git a/apps/web/tests/reference-composer.e2e.ts b/apps/web/tests/reference-composer.e2e.ts index 0931c525f4..47cdb171ed 100644 --- a/apps/web/tests/reference-composer.e2e.ts +++ b/apps/web/tests/reference-composer.e2e.ts @@ -311,6 +311,23 @@ describe.skipIf(MODE === 'record')('web e2e: file and session references through expect(tripwire.warnings).toEqual([]) }) + it('keeps a file path typed by hand in typed order', async () => { + onTestFailed(() => saveFailureShot(page, 'web-e2e-reference-typed-path')) + const input = page.locator('[data-composer-input]').first() + + // No menu pick: every character of the path arrives as its own key press. + await writeComposerDraft(page, input, '@folderx/nested/leaf.txt') + await expect.poll(() => input.textContent()).toBe('@folderx/nested/leaf.txt') + // The caret follows the last character, and the sentence's period stays outside the reference. + await page.keyboard.type('. Done') + await expect.poll(() => input.textContent()).toBe('@folderx/nested/leaf.txt. Done') + await expect.poll(() => input.locator('[data-composer-text-ref]').allTextContents()) + .toEqual(['@folderx/nested/leaf.txt']) + + expect(tripwire.pageErrors).toEqual([]) + expect(tripwire.warnings).toEqual([]) + }) + it('settles a folder as an atomic chip; Tab and the chevron drill instead', async () => { onTestFailed(() => saveFailureShot(page, 'web-e2e-reference-folder')) const input = page.locator('[data-composer-input]').first() diff --git a/packages/client/ui-conversation/src/client/input/editor/text-ref.ts b/packages/client/ui-conversation/src/client/input/editor/text-ref.ts index 5221b38a57..6a09b0c741 100644 --- a/packages/client/ui-conversation/src/client/input/editor/text-ref.ts +++ b/packages/client/ui-conversation/src/client/input/editor/text-ref.ts @@ -69,6 +69,27 @@ export class TextRefNode extends TextNode { } } +/** First reference range of a text, or null. */ +type TextRefMatch = (text: string) => { start: number; end: number } | null + +/** + * Extend a text-ref over the plain text directly after it when both together + * match one reference, such as `@src/a` followed by `.ts`. A selection point + * in the moved text stays beside the same character. + * @param node - plain text node the editor marked dirty. + * @param getMatch - the reference matcher. + */ +function $growTextRef(node: TextNode, getMatch: TextRefMatch): void { + const textRef = node.getPreviousSibling() + if (!(textRef instanceof TextRefNode)) return + const length = textRef.getTextContentSize() + const match = getMatch(textRef.getTextContent() + node.getTextContent()) + if (match === null || match.end <= length) return + if (match.end - length < node.getTextContentSize()) node.splitText(match.end - length) + // A split keeps the leading text under this node's key, possibly in a new writable copy. + textRef.mergeWithSibling(node.getLatest()) +} + /** * Register the plain-text reference entity transform. * @param editor - the shell-owned editor. @@ -79,11 +100,16 @@ export function registerTextRefDecoration( editor: LexicalEditor, lexiconOf: () => ReadonlyMap<'/' | '@', readonly string[]>, ): () => void { - const getMatch = (text: string): { start: number; end: number } | null => { + const getMatch: TextRefMatch = (text) => { const range = scanTextRefs(text, lexiconOf())[0] return range === undefined ? null : { start: range.start, end: range.end } } return mergeRegister( + // The entity transforms below also extend a text-ref over following text, but collapse the + // selection to the text-ref's previous end first, so the next typed character lands before + // the moved text (https://github.com/facebook/lexical/issues/3054). Transforms of one node + // type run in registration order; this one leaves them nothing to extend. + editor.registerNodeTransform(TextNode, (node) => { $growTextRef(node, getMatch) }), ...registerLexicalTextEntity( editor, getMatch, diff --git a/packages/client/ui-conversation/tests/draft-document.client.spec.ts b/packages/client/ui-conversation/tests/draft-document.client.spec.ts index 8e50e1ad3a..957fdd9f1e 100644 --- a/packages/client/ui-conversation/tests/draft-document.client.spec.ts +++ b/packages/client/ui-conversation/tests/draft-document.client.spec.ts @@ -14,6 +14,7 @@ import { scanTextRefs } from '../src/client/input/decorations.ts' import { ReferenceChipNode } from '../src/client/input/editor/chip-node.tsx' import { CommandChipNode } from '../src/client/input/editor/command-chip-node.tsx' import { QuoteNode } from '../src/client/input/editor/QuoteNode.tsx' +import { $selectDetectSpan } from '../src/client/input/editor/span-map.ts' import { TextRefNode } from '../src/client/input/editor/text-ref.ts' import { SessionInputShell, type SessionInputDeps } from '../src/client/input/facade.ts' @@ -856,3 +857,54 @@ describe('plain-text reference catalogs', () => { expect(slices('drill @"open dir/。')).toEqual(['@"open dir/']) }) }) + +describe('path references typed one character at a time', () => { + /** Insert each character at the live selection as its own edit, the way key presses arrive. */ + function typeText(shell: SessionInputShell, text: string): void { + for (const char of text) shell.paste(char) + } + + it.each([ + ['@src/index.ts', ['@src/index.ts']], + ['@vite.config.ts', ['@vite.config.ts']], + ['@out/v1.2/readme', ['@out/v1.2/readme']], + ['@out/w1,x', ['@out/w1,x']], + ['看下 @src/app.test.ts 有问题', ['@src/app.test.ts']], + ])('keeps %s in typed order with the caret after the last character', (text, references) => { + const shell = makeShell() + typeText(shell, text) + expect(shell.snapshot.draft).toBe(text) + expect(shell.caretSpan()).toEqual({ start: text.length, end: text.length }) + expect(textReferences(shell)).toEqual(references) + }) + + it('leaves punctuation that ends a sentence outside the reference', () => { + const shell = makeShell() + typeText(shell, '@src/a.ts. Next') + expect(shell.snapshot.draft).toBe('@src/a.ts. Next') + expect(shell.caretSpan()).toEqual({ start: 15, end: 15 }) + expect(textReferences(shell)).toEqual(['@src/a.ts']) + }) + + it('extends a reference over the start of a longer run and keeps the caret after the typed character', () => { + const shell = makeShell() + typeText(shell, '@src/a. x') + expect(textReferences(shell)).toEqual(['@src/a']) + shell.editor.update(() => { $selectDetectSpan({ start: 7, end: 7 }) }, { discrete: true }) + typeText(shell, 't') + expect(shell.snapshot.draft).toBe('@src/a.t x') + expect(shell.caretSpan()).toEqual({ start: 8, end: 8 }) + expect(textReferences(shell)).toEqual(['@src/a.t']) + }) + + it('leaves the text after a named reference in place when its catalog drops the name', async () => { + const lexicon = createSnapshotStore>(new Map([['@', ['alice']]])) + const provider = triggerProvider(lexicon) + const shell = makeShell({ inputTriggers: () => provider }) + typeText(shell, '@alice.') + expect(textReferences(shell)).toEqual(['@alice']) + lexicon.set(new Map()) + await vi.waitFor(() => { expect(textReferences(shell)).toEqual([]) }) + expect(shell.snapshot.draft).toBe('@alice.') + }) +})