Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 36 additions & 25 deletions packages/cli/src/gemini.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ import { computeTerminalTitle } from './utils/windowTitle.js';

import { SessionStatsProvider } from './ui/contexts/SessionContext.js';
import { VimModeProvider } from './ui/contexts/VimModeContext.js';
import { KeyMatchersProvider } from './ui/hooks/useKeyMatchers.js';
import { loadKeyMatchers } from './ui/key/keyMatchers.js';
import { KeypressProvider } from './ui/contexts/KeypressContext.js';
import { useKittyKeyboardProtocol } from './ui/hooks/useKittyKeyboardProtocol.js';
import {
Expand Down Expand Up @@ -208,6 +210,11 @@ export async function startInteractiveUI(
});
}

const { matchers, errors } = await loadKeyMatchers();
errors.forEach((error) => {
coreEvents.emitFeedback('warning', error);
});

const version = await getVersion();
setWindowTitle(basename(workspaceRoot), settings);

Expand All @@ -230,35 +237,39 @@ export async function startInteractiveUI(

return (
<SettingsContext.Provider value={settings}>
<KeypressProvider
config={config}
debugKeystrokeLogging={settings.merged.general.debugKeystrokeLogging}
>
<MouseProvider
mouseEventsEnabled={mouseEventsEnabled}
<KeyMatchersProvider value={matchers}>
<KeypressProvider
config={config}
debugKeystrokeLogging={
settings.merged.general.debugKeystrokeLogging
}
>
<TerminalProvider>
<ScrollProvider>
<OverflowProvider>
<SessionStatsProvider>
<VimModeProvider>
<AppContainer
config={config}
startupWarnings={startupWarnings}
version={version}
resumedSessionData={resumedSessionData}
initializationResult={initializationResult}
/>
</VimModeProvider>
</SessionStatsProvider>
</OverflowProvider>
</ScrollProvider>
</TerminalProvider>
</MouseProvider>
</KeypressProvider>
<MouseProvider
mouseEventsEnabled={mouseEventsEnabled}
debugKeystrokeLogging={
settings.merged.general.debugKeystrokeLogging
}
>
<TerminalProvider>
<ScrollProvider>
<OverflowProvider>
<SessionStatsProvider>
<VimModeProvider>
<AppContainer
config={config}
startupWarnings={startupWarnings}
version={version}
resumedSessionData={resumedSessionData}
initializationResult={initializationResult}
/>
</VimModeProvider>
</SessionStatsProvider>
</OverflowProvider>
</ScrollProvider>
</TerminalProvider>
</MouseProvider>
</KeypressProvider>
</KeyMatchersProvider>
</SettingsContext.Provider>
);
};
Expand Down
17 changes: 0 additions & 17 deletions packages/cli/src/ui/hooks/useKeyMatchers.ts

This file was deleted.

33 changes: 33 additions & 0 deletions packages/cli/src/ui/hooks/useKeyMatchers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import type React from 'react';
import { createContext, useContext } from 'react';
import type { KeyMatchers } from '../key/keyMatchers.js';
import { defaultKeyMatchers } from '../key/keyMatchers.js';

export const KeyMatchersContext =
createContext<KeyMatchers>(defaultKeyMatchers);

export const KeyMatchersProvider = ({
children,
value,
}: {
children: React.ReactNode;
value: KeyMatchers;
}): React.JSX.Element => (
<KeyMatchersContext.Provider value={value}>
{children}
</KeyMatchersContext.Provider>
);

/**
* Hook to retrieve the currently active key matchers.
* Defaults to defaultKeyMatchers if no provider is present, allowing tests to run without explicit wrappers.
*/
export function useKeyMatchers(): KeyMatchers {
return useContext(KeyMatchersContext);
}
124 changes: 101 additions & 23 deletions packages/cli/src/ui/key/keyBindings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { describe, it, expect } from 'vitest';
import type { KeyBindingConfig } from './keyBindings.js';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import * as os from 'node:os';
import * as path from 'node:path';
import * as fs from 'node:fs/promises';
import { Storage } from '@google/gemini-cli-core';
import {
Command,
commandCategories,
commandDescriptions,
defaultKeyBindings,
defaultKeyBindingConfig,
KeyBinding,
loadCustomKeybindings,
} from './keyBindings.js';

describe('KeyBinding', () => {
Expand Down Expand Up @@ -104,26 +108,11 @@ describe('KeyBinding', () => {
});

describe('keyBindings config', () => {
describe('defaultKeyBindings', () => {
it('should have bindings for all commands', () => {
const commands = Object.values(Command);

for (const command of commands) {
expect(defaultKeyBindings[command]).toBeDefined();
expect(Array.isArray(defaultKeyBindings[command])).toBe(true);
expect(defaultKeyBindings[command]?.length).toBeGreaterThan(0);
}
});

it('should export all required types', () => {
// Basic type checks
expect(typeof Command.HOME).toBe('string');
expect(typeof Command.END).toBe('string');

// Config should be readonly
const config: KeyBindingConfig = defaultKeyBindings;
expect(config[Command.HOME]).toBeDefined();
});
it('should have bindings for all commands', () => {
for (const command of Object.values(Command)) {
expect(defaultKeyBindingConfig.has(command)).toBe(true);
expect(defaultKeyBindingConfig.get(command)?.length).toBeGreaterThan(0);
}
});

describe('command metadata', () => {
Expand Down Expand Up @@ -157,3 +146,92 @@ describe('keyBindings config', () => {
});
});
});

describe('loadCustomKeybindings', () => {
let tempDir: string;
let tempFilePath: string;

beforeEach(async () => {
tempDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'gemini-keybindings-test-'),
);
tempFilePath = path.join(tempDir, 'keybindings.json');
vi.spyOn(Storage, 'getUserKeybindingsPath').mockReturnValue(tempFilePath);
});

afterEach(async () => {
await fs.rm(tempDir, { recursive: true, force: true });
vi.restoreAllMocks();
});

it('returns default bindings when file does not exist', async () => {
// We don't write the file.
const { config, errors } = await loadCustomKeybindings();

expect(errors).toHaveLength(0);
expect(config.get(Command.RETURN)).toEqual([new KeyBinding('enter')]);
});

it('merges valid custom bindings, prepending them to defaults', async () => {
const customJson = JSON.stringify([
{ command: Command.RETURN, key: 'ctrl+a' },
]);
await fs.writeFile(tempFilePath, customJson, 'utf8');

const { config, errors } = await loadCustomKeybindings();

expect(errors).toHaveLength(0);
expect(config.get(Command.RETURN)).toEqual([
new KeyBinding('ctrl+a'),
new KeyBinding('enter'),
]);
});

it('handles JSON with comments', async () => {
const customJson = `
[
// This is a comment
{ "command": "${Command.QUIT}", "key": "ctrl+x" }
]
`;
await fs.writeFile(tempFilePath, customJson, 'utf8');

const { config, errors } = await loadCustomKeybindings();

expect(errors).toHaveLength(0);
expect(config.get(Command.QUIT)).toEqual([
new KeyBinding('ctrl+x'),
new KeyBinding('ctrl+c'),
]);
});

it('returns validation errors for invalid schema', async () => {
const invalidJson = JSON.stringify([{ command: 'unknown', key: 'a' }]);
await fs.writeFile(tempFilePath, invalidJson, 'utf8');

const { config, errors } = await loadCustomKeybindings();

expect(errors.length).toBeGreaterThan(0);

expect(errors[0]).toMatch(/error at 0.command: Invalid enum value/);
// Should still have defaults
expect(config.get(Command.RETURN)).toEqual([new KeyBinding('enter')]);
});

it('returns validation errors for invalid key patterns but loads valid ones', async () => {
const mixedJson = JSON.stringify([
{ command: Command.RETURN, key: 'super+a' }, // invalid
{ command: Command.QUIT, key: 'ctrl+y' }, // valid
]);
await fs.writeFile(tempFilePath, mixedJson, 'utf8');

const { config, errors } = await loadCustomKeybindings();

expect(errors.length).toBe(1);
expect(errors[0]).toMatch(/Invalid keybinding/);
expect(config.get(Command.QUIT)).toEqual([
new KeyBinding('ctrl+y'),
new KeyBinding('ctrl+c'),
]);
});
});
Loading
Loading