From a7e45d47cda0b2b4f6325e8700434e7b2a2660ed Mon Sep 17 00:00:00 2001 From: Jacob Richman Date: Mon, 16 Jun 2025 22:21:22 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20bug=20where=20single=20line=20inserts=20w?= =?UTF-8?q?ere=20deleting=20all=20text=20after=20the=20in=E2=80=A6=20(#111?= =?UTF-8?q?4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/ui/components/shared/text-buffer.test.ts | 15 +++++++++++++++ .../cli/src/ui/components/shared/text-buffer.ts | 4 +++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/ui/components/shared/text-buffer.test.ts b/packages/cli/src/ui/components/shared/text-buffer.test.ts index c5295fc1..218ed1c3 100644 --- a/packages/cli/src/ui/components/shared/text-buffer.test.ts +++ b/packages/cli/src/ui/components/shared/text-buffer.test.ts @@ -173,6 +173,21 @@ describe('useTextBuffer', () => { expect(state.visualCursor).toEqual([0, 2]); }); + it('insert: should insert text in the middle of a line', () => { + const { result } = renderHook(() => + useTextBuffer({ + initialText: 'abc', + viewport, + isValidPath: () => false, + }), + ); + act(() => result.current.move('right')); + act(() => result.current.insert('-NEW-')); + const state = getBufferState(result); + expect(state.text).toBe('a-NEW-bc'); + expect(state.cursor).toEqual([0, 6]); + }); + it('newline: should create a new line and move cursor', () => { const { result } = renderHook(() => useTextBuffer({ diff --git a/packages/cli/src/ui/components/shared/text-buffer.ts b/packages/cli/src/ui/components/shared/text-buffer.ts index ef21d00a..6b025dd5 100644 --- a/packages/cli/src/ui/components/shared/text-buffer.ts +++ b/packages/cli/src/ui/components/shared/text-buffer.ts @@ -635,9 +635,9 @@ export function useTextBuffer({ const lineContent = currentLine(newCursorRow); const before = cpSlice(lineContent, 0, newCursorCol); const after = cpSlice(lineContent, newCursorCol); - newLines[newCursorRow] = before + parts[0]; if (parts.length > 1) { + newLines[newCursorRow] = before + parts[0]; const remainingParts = parts.slice(1); const lastPartOriginal = remainingParts.pop() ?? ''; newLines.splice(newCursorRow + 1, 0, ...remainingParts); @@ -649,6 +649,8 @@ export function useTextBuffer({ newCursorRow = newCursorRow + parts.length - 1; newCursorCol = cpLen(lastPartOriginal); } else { + newLines[newCursorRow] = before + parts[0] + after; + newCursorCol = cpLen(before) + cpLen(parts[0]); } } else if (op.type === 'backspace') {