mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-13 05:12:55 -07:00
fix(cli): refine platform-specific undo/redo and smart bubbling for WSL (#26202)
This commit is contained in:
@@ -41,6 +41,7 @@ import {
|
||||
getTransformedImagePath,
|
||||
} from './text-buffer.js';
|
||||
import { cpLen } from '../../utils/textUtils.js';
|
||||
import { type Key } from '../../hooks/useKeypress.js';
|
||||
import { escapePath } from '@google/gemini-cli-core';
|
||||
|
||||
const defaultVisualLayout: VisualLayout = {
|
||||
@@ -1799,6 +1800,229 @@ describe('useTextBuffer', () => {
|
||||
expect(getBufferState(result).text).toBe('');
|
||||
});
|
||||
|
||||
it('should only handle Undo if there is something to undo', async () => {
|
||||
const { result } = await renderHook(() => useTextBuffer({ viewport }));
|
||||
|
||||
// Platform-specific undo key
|
||||
const undoKey: Key =
|
||||
process.platform === 'win32'
|
||||
? {
|
||||
name: 'z',
|
||||
ctrl: true,
|
||||
shift: false,
|
||||
alt: false,
|
||||
cmd: false,
|
||||
insertable: false,
|
||||
sequence: '\x1a',
|
||||
}
|
||||
: process.platform === 'darwin'
|
||||
? {
|
||||
name: 'z',
|
||||
ctrl: false,
|
||||
shift: false,
|
||||
alt: false,
|
||||
cmd: true,
|
||||
insertable: false,
|
||||
sequence: '\u001b[122;D',
|
||||
}
|
||||
: {
|
||||
name: 'z',
|
||||
ctrl: false,
|
||||
shift: false,
|
||||
alt: true,
|
||||
cmd: false,
|
||||
insertable: false,
|
||||
sequence: '\u001bz',
|
||||
};
|
||||
|
||||
// 1. Initial state: nothing to undo
|
||||
let handled = true;
|
||||
act(() => {
|
||||
handled = result.current.handleInput(undoKey);
|
||||
});
|
||||
expect(handled).toBe(false);
|
||||
|
||||
// 2. Insert something
|
||||
act(() => {
|
||||
result.current.handleInput({
|
||||
name: 'a',
|
||||
shift: false,
|
||||
alt: false,
|
||||
ctrl: false,
|
||||
cmd: false,
|
||||
insertable: true,
|
||||
sequence: 'a',
|
||||
});
|
||||
});
|
||||
expect(getBufferState(result).text).toBe('a');
|
||||
|
||||
// 3. Now undo should work
|
||||
act(() => {
|
||||
handled = result.current.handleInput(undoKey);
|
||||
});
|
||||
expect(handled).toBe(true);
|
||||
expect(getBufferState(result).text).toBe('');
|
||||
|
||||
// 4. Undo again: nothing left to undo
|
||||
act(() => {
|
||||
handled = result.current.handleInput(undoKey);
|
||||
});
|
||||
expect(handled).toBe(false);
|
||||
});
|
||||
|
||||
if (process.platform === 'linux') {
|
||||
it('should handle "Ctrl+Z" for smart bubbling on Linux/WSL', async () => {
|
||||
const { result } = await renderHook(() => useTextBuffer({ viewport }));
|
||||
|
||||
const ctrlZ: Key = {
|
||||
name: 'z',
|
||||
ctrl: true,
|
||||
shift: false,
|
||||
alt: false,
|
||||
cmd: false,
|
||||
insertable: false,
|
||||
sequence: '\x1a',
|
||||
};
|
||||
|
||||
// 1. Empty buffer: should NOT handle (bubble up to Suspend)
|
||||
let handled = true;
|
||||
act(() => {
|
||||
handled = result.current.handleInput(ctrlZ);
|
||||
});
|
||||
expect(handled).toBe(false);
|
||||
|
||||
// 2. Add text
|
||||
act(() => {
|
||||
result.current.handleInput({
|
||||
name: 'x',
|
||||
insertable: true,
|
||||
sequence: 'x',
|
||||
shift: false,
|
||||
alt: false,
|
||||
ctrl: false,
|
||||
cmd: false,
|
||||
});
|
||||
});
|
||||
|
||||
// 3. Has history: should handle (perform Undo)
|
||||
act(() => {
|
||||
handled = result.current.handleInput(ctrlZ);
|
||||
});
|
||||
expect(handled).toBe(true);
|
||||
expect(getBufferState(result).text).toBe('');
|
||||
|
||||
// 4. Empty again: should NOT handle
|
||||
act(() => {
|
||||
handled = result.current.handleInput(ctrlZ);
|
||||
});
|
||||
expect(handled).toBe(false);
|
||||
});
|
||||
}
|
||||
|
||||
it('should only handle Redo if there is something to redo', async () => {
|
||||
const { result } = await renderHook(() => useTextBuffer({ viewport }));
|
||||
|
||||
// Platform-specific redo key (first in list)
|
||||
const redoKey: Key =
|
||||
process.platform === 'win32'
|
||||
? {
|
||||
name: 'z',
|
||||
ctrl: true,
|
||||
shift: true,
|
||||
alt: false,
|
||||
cmd: false,
|
||||
insertable: false,
|
||||
sequence: '\x1a',
|
||||
}
|
||||
: process.platform === 'darwin'
|
||||
? {
|
||||
name: 'z',
|
||||
ctrl: false,
|
||||
shift: true,
|
||||
alt: false,
|
||||
cmd: true,
|
||||
insertable: false,
|
||||
sequence: '\u001b[122;2D',
|
||||
}
|
||||
: {
|
||||
name: 'z',
|
||||
ctrl: false,
|
||||
shift: true,
|
||||
alt: true,
|
||||
cmd: false,
|
||||
insertable: false,
|
||||
sequence: '\u001bZ',
|
||||
};
|
||||
|
||||
const undoKey: Key =
|
||||
process.platform === 'win32'
|
||||
? {
|
||||
name: 'z',
|
||||
ctrl: true,
|
||||
shift: false,
|
||||
alt: false,
|
||||
cmd: false,
|
||||
insertable: false,
|
||||
sequence: '\x1a',
|
||||
}
|
||||
: process.platform === 'darwin'
|
||||
? {
|
||||
name: 'z',
|
||||
ctrl: false,
|
||||
shift: false,
|
||||
alt: false,
|
||||
cmd: true,
|
||||
insertable: false,
|
||||
sequence: '\u001b[122;D',
|
||||
}
|
||||
: {
|
||||
name: 'z',
|
||||
ctrl: false,
|
||||
shift: false,
|
||||
alt: true,
|
||||
cmd: false,
|
||||
insertable: false,
|
||||
sequence: '\u001bz',
|
||||
};
|
||||
|
||||
// 1. Initial state: nothing to redo
|
||||
let handled = true;
|
||||
act(() => {
|
||||
handled = result.current.handleInput(redoKey);
|
||||
});
|
||||
expect(handled).toBe(false);
|
||||
|
||||
// 2. Insert and Undo
|
||||
act(() => {
|
||||
result.current.handleInput({
|
||||
name: 'a',
|
||||
shift: false,
|
||||
alt: false,
|
||||
ctrl: false,
|
||||
cmd: false,
|
||||
insertable: true,
|
||||
sequence: 'a',
|
||||
});
|
||||
});
|
||||
act(() => {
|
||||
result.current.handleInput(undoKey);
|
||||
});
|
||||
expect(getBufferState(result).text).toBe('');
|
||||
|
||||
// 3. Now redo should work
|
||||
act(() => {
|
||||
handled = result.current.handleInput(redoKey);
|
||||
});
|
||||
expect(handled).toBe(true);
|
||||
expect(getBufferState(result).text).toBe('a');
|
||||
|
||||
// 4. Redo again: nothing left to redo
|
||||
act(() => {
|
||||
handled = result.current.handleInput(redoKey);
|
||||
});
|
||||
expect(handled).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle multiple delete characters in one input', async () => {
|
||||
const { result } = await renderHook(() =>
|
||||
useTextBuffer({
|
||||
|
||||
@@ -2889,6 +2889,8 @@ export function useTextBuffer({
|
||||
transformationsByLine,
|
||||
pastedContent,
|
||||
expandedPaste,
|
||||
undoStack,
|
||||
redoStack,
|
||||
} = state;
|
||||
|
||||
const text = useMemo(() => lines.join('\n'), [lines]);
|
||||
@@ -3454,10 +3456,16 @@ export function useTextBuffer({
|
||||
return true;
|
||||
}
|
||||
if (keyMatchers[Command.UNDO](key)) {
|
||||
if (undoStack.length === 0) {
|
||||
return false;
|
||||
}
|
||||
undo();
|
||||
return true;
|
||||
}
|
||||
if (keyMatchers[Command.REDO](key)) {
|
||||
if (redoStack.length === 0) {
|
||||
return false;
|
||||
}
|
||||
redo();
|
||||
return true;
|
||||
}
|
||||
@@ -3486,6 +3494,8 @@ export function useTextBuffer({
|
||||
visualCursor,
|
||||
visualLines,
|
||||
keyMatchers,
|
||||
undoStack.length,
|
||||
redoStack.length,
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
@@ -108,6 +108,30 @@ describe('keyBindings config', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('should have platform-specific UNDO bindings', () => {
|
||||
const undoBindings = defaultKeyBindingConfig.get(Command.UNDO);
|
||||
if (process.platform === 'win32') {
|
||||
expect(undoBindings?.[0].name).toBe('z');
|
||||
expect(undoBindings?.[0].ctrl).toBe(true);
|
||||
} else if (process.platform === 'darwin') {
|
||||
expect(undoBindings?.[0].name).toBe('z');
|
||||
expect(undoBindings?.[0].cmd).toBe(true);
|
||||
} else {
|
||||
expect(undoBindings?.[0].name).toBe('z');
|
||||
expect(undoBindings?.[0].alt).toBe(true);
|
||||
// Ensure ctrl+z is also present for smart bubbling
|
||||
expect(undoBindings?.some((b) => b.name === 'z' && b.ctrl)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('should have platform-specific REDO bindings', () => {
|
||||
const redoBindings = defaultKeyBindingConfig.get(Command.REDO);
|
||||
// Ctrl+Shift+Z is now the universal primary to avoid conflict with YOLO (Ctrl+Y)
|
||||
expect(redoBindings?.[0].name).toBe('z');
|
||||
expect(redoBindings?.[0].shift).toBe(true);
|
||||
expect(redoBindings?.[0].ctrl).toBe(true);
|
||||
});
|
||||
|
||||
describe('command metadata', () => {
|
||||
const commandValues = Object.values(Command);
|
||||
|
||||
|
||||
@@ -312,15 +312,8 @@ export const defaultKeyBindingConfig: KeyBindingConfig = new Map([
|
||||
Command.DELETE_CHAR_RIGHT,
|
||||
[new KeyBinding('delete'), new KeyBinding('ctrl+d')],
|
||||
],
|
||||
[Command.UNDO, [new KeyBinding('cmd+z'), new KeyBinding('alt+z')]],
|
||||
[
|
||||
Command.REDO,
|
||||
[
|
||||
new KeyBinding('ctrl+shift+z'),
|
||||
new KeyBinding('cmd+shift+z'),
|
||||
new KeyBinding('alt+shift+z'),
|
||||
],
|
||||
],
|
||||
[Command.UNDO, getPlatformUndoBindings(process.platform)],
|
||||
[Command.REDO, getPlatformRedoBindings(process.platform)],
|
||||
|
||||
// Scrolling
|
||||
[Command.SCROLL_UP, [new KeyBinding('shift+up')]],
|
||||
@@ -782,3 +775,33 @@ export async function loadCustomKeybindings(): Promise<{
|
||||
|
||||
return { config, errors };
|
||||
}
|
||||
|
||||
export function getPlatformUndoBindings(
|
||||
platform: string,
|
||||
): readonly KeyBinding[] {
|
||||
if (platform === 'win32') {
|
||||
return [new KeyBinding('ctrl+z'), new KeyBinding('alt+z')];
|
||||
}
|
||||
if (platform === 'darwin') {
|
||||
return [new KeyBinding('cmd+z'), new KeyBinding('alt+z')];
|
||||
}
|
||||
// Linux / WSL: Promote Alt+Z to avoid Windows interception,
|
||||
// but keep Ctrl+Z for smart bubbling.
|
||||
return [
|
||||
new KeyBinding('alt+z'),
|
||||
new KeyBinding('cmd+z'),
|
||||
new KeyBinding('ctrl+z'),
|
||||
];
|
||||
}
|
||||
|
||||
export function getPlatformRedoBindings(
|
||||
_platform: string,
|
||||
): readonly KeyBinding[] {
|
||||
// Use a stable order for all platforms to minimize churn.
|
||||
// Ctrl+Shift+Z is the universal primary.
|
||||
return [
|
||||
new KeyBinding('ctrl+shift+z'),
|
||||
new KeyBinding('cmd+shift+z'),
|
||||
new KeyBinding('alt+shift+z'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -149,23 +149,44 @@ describe('keyMatchers', () => {
|
||||
{
|
||||
command: Command.UNDO,
|
||||
positive: [
|
||||
createKey('z', { shift: false, cmd: true }),
|
||||
createKey('z', { shift: false, alt: true }),
|
||||
...(process.platform === 'win32'
|
||||
? [createKey('z', { shift: false, ctrl: true })]
|
||||
: process.platform === 'darwin'
|
||||
? [createKey('z', { shift: false, cmd: true })]
|
||||
: [
|
||||
createKey('z', { shift: false, alt: true }),
|
||||
createKey('z', { shift: false, cmd: true }),
|
||||
createKey('z', { shift: false, ctrl: true }),
|
||||
]),
|
||||
...(process.platform !== 'linux'
|
||||
? [createKey('z', { shift: false, alt: true })]
|
||||
: []),
|
||||
],
|
||||
negative: [
|
||||
createKey('z'),
|
||||
createKey('z', { shift: true, cmd: true }),
|
||||
createKey('z', { shift: false, ctrl: true }),
|
||||
...(process.platform === 'darwin'
|
||||
? [createKey('z', { shift: false, ctrl: true })]
|
||||
: []),
|
||||
...(process.platform === 'win32'
|
||||
? [createKey('z', { shift: false, cmd: true })]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
{
|
||||
command: Command.REDO,
|
||||
positive: [
|
||||
createKey('z', { shift: true, cmd: true }),
|
||||
...(process.platform === 'win32'
|
||||
? []
|
||||
: [createKey('z', { shift: true, cmd: true })]),
|
||||
createKey('z', { shift: true, alt: true }),
|
||||
createKey('z', { shift: true, ctrl: true }),
|
||||
],
|
||||
negative: [createKey('z'), createKey('z', { shift: false, cmd: true })],
|
||||
negative: [
|
||||
createKey('z'),
|
||||
createKey('z', { shift: false, cmd: true }),
|
||||
createKey('y', { shift: false, ctrl: true }),
|
||||
],
|
||||
},
|
||||
|
||||
// Screen control
|
||||
|
||||
Reference in New Issue
Block a user