fix(ui-conversation): keep hand-typed path references in typed order

Typing a path one character at a time, such as @src/index.ts, produced
@src/indexs.t. The "." counts as punctuation outside the reference until
the next character arrives. The entity transform then extended the
reference over ".t" after collapsing the selection to the reference's
previous end, so later characters landed before the moved text.

A transform registered ahead of the entity transforms now makes that
extension with splitText and mergeWithSibling, which keep the selection
beside the moved characters.
This commit is contained in:
LegGasai committed 2026-11-20 11:02:53 +08:00
1 parent 9c60743a11
commit b7b18abef7
3 files changed
+96 -1

No files matched your search

+17
View File
@@ -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()
@@ -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,
@@ -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<ReadonlyMap<'/' | '@', readonly string[]>>(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.')
})
})