fix(ui): Correct mouse click cursor positioning for wide characters (#13537)

This commit is contained in:
Sandy Tao
2025-11-21 07:56:31 +08:00
committed by GitHub
parent 78b10dccf1
commit 5982abeffb
2 changed files with 53 additions and 5 deletions

View File

@@ -963,6 +963,34 @@ describe('useTextBuffer', () => {
expect(state.cursor).toEqual([0, 1]);
expect(state.visualCursor).toEqual([0, 1]);
});
it('moveToVisualPosition: should correctly handle wide characters (Chinese)', () => {
const { result } = renderHook(() =>
useTextBuffer({
initialText: '你好', // 2 chars, width 4
viewport: { width: 10, height: 1 },
isValidPath: () => false,
}),
);
// '你' (width 2): visual 0-1. '好' (width 2): visual 2-3.
// Click on '你' (first half, x=0) -> index 0
act(() => result.current.moveToVisualPosition(0, 0));
expect(getBufferState(result).cursor).toEqual([0, 0]);
// Click on '你' (second half, x=1) -> index 1 (after first char)
act(() => result.current.moveToVisualPosition(0, 1));
expect(getBufferState(result).cursor).toEqual([0, 1]);
// Click on '好' (first half, x=2) -> index 1 (before second char)
act(() => result.current.moveToVisualPosition(0, 2));
expect(getBufferState(result).cursor).toEqual([0, 1]);
// Click on '好' (second half, x=3) -> index 2 (after second char)
act(() => result.current.moveToVisualPosition(0, 3));
expect(getBufferState(result).cursor).toEqual([0, 2]);
});
});
describe('handleInput', () => {