Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
parent
0dea7233b6
commit
c632ec8b03
|
@ -32,6 +32,8 @@ export enum Command {
|
||||||
|
|
||||||
// Auto-completion
|
// Auto-completion
|
||||||
ACCEPT_SUGGESTION = 'acceptSuggestion',
|
ACCEPT_SUGGESTION = 'acceptSuggestion',
|
||||||
|
COMPLETION_UP = 'completionUp',
|
||||||
|
COMPLETION_DOWN = 'completionDown',
|
||||||
|
|
||||||
// Text input
|
// Text input
|
||||||
SUBMIT = 'submit',
|
SUBMIT = 'submit',
|
||||||
|
@ -121,6 +123,9 @@ export const defaultKeyBindings: KeyBindingConfig = {
|
||||||
// Auto-completion
|
// Auto-completion
|
||||||
// Original: key.name === 'tab' || (key.name === 'return' && !key.ctrl)
|
// Original: key.name === 'tab' || (key.name === 'return' && !key.ctrl)
|
||||||
[Command.ACCEPT_SUGGESTION]: [{ key: 'tab' }, { key: 'return', ctrl: false }],
|
[Command.ACCEPT_SUGGESTION]: [{ key: 'tab' }, { key: 'return', ctrl: false }],
|
||||||
|
// Completion navigation (arrow or Ctrl+P/N)
|
||||||
|
[Command.COMPLETION_UP]: [{ key: 'up' }, { key: 'p', ctrl: true }],
|
||||||
|
[Command.COMPLETION_DOWN]: [{ key: 'down' }, { key: 'n', ctrl: true }],
|
||||||
|
|
||||||
// Text input
|
// Text input
|
||||||
// Original: key.name === 'return' && !key.ctrl && !key.meta && !key.paste
|
// Original: key.name === 'return' && !key.ctrl && !key.meta && !key.paste
|
||||||
|
@ -157,15 +162,9 @@ export const defaultKeyBindings: KeyBindingConfig = {
|
||||||
// Original: key.ctrl && key.name === 'e'
|
// Original: key.ctrl && key.name === 'e'
|
||||||
[Command.TOGGLE_IDE_CONTEXT_DETAIL]: [{ key: 'e', ctrl: true }],
|
[Command.TOGGLE_IDE_CONTEXT_DETAIL]: [{ key: 'e', ctrl: true }],
|
||||||
// Original: key.ctrl && (key.name === 'c' || key.name === 'C')
|
// Original: key.ctrl && (key.name === 'c' || key.name === 'C')
|
||||||
[Command.QUIT]: [
|
[Command.QUIT]: [{ key: 'c', ctrl: true }],
|
||||||
{ key: 'c', ctrl: true },
|
|
||||||
{ key: 'C', ctrl: true },
|
|
||||||
],
|
|
||||||
// Original: key.ctrl && (key.name === 'd' || key.name === 'D')
|
// Original: key.ctrl && (key.name === 'd' || key.name === 'D')
|
||||||
[Command.EXIT]: [
|
[Command.EXIT]: [{ key: 'd', ctrl: true }],
|
||||||
{ key: 'd', ctrl: true },
|
|
||||||
{ key: 'D', ctrl: true },
|
|
||||||
],
|
|
||||||
// Original: key.ctrl && key.name === 's'
|
// Original: key.ctrl && key.name === 's'
|
||||||
[Command.SHOW_MORE_LINES]: [{ key: 's', ctrl: true }],
|
[Command.SHOW_MORE_LINES]: [{ key: 's', ctrl: true }],
|
||||||
|
|
||||||
|
|
|
@ -373,17 +373,11 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
|
||||||
|
|
||||||
if (completion.showSuggestions) {
|
if (completion.showSuggestions) {
|
||||||
if (completion.suggestions.length > 1) {
|
if (completion.suggestions.length > 1) {
|
||||||
if (
|
if (keyMatchers[Command.COMPLETION_UP](key)) {
|
||||||
keyMatchers[Command.NAVIGATION_UP](key) ||
|
|
||||||
keyMatchers[Command.HISTORY_UP](key)
|
|
||||||
) {
|
|
||||||
completion.navigateUp();
|
completion.navigateUp();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
if (keyMatchers[Command.COMPLETION_DOWN](key)) {
|
||||||
keyMatchers[Command.NAVIGATION_DOWN](key) ||
|
|
||||||
keyMatchers[Command.HISTORY_DOWN](key)
|
|
||||||
) {
|
|
||||||
completion.navigateDown();
|
completion.navigateDown();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,8 @@ describe('keyMatchers', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Original hard-coded logic (for comparison)
|
// Original hard-coded logic (for comparison)
|
||||||
const originalMatchers = {
|
const originalMatchers: Record<Command, (key: Key) => boolean> = {
|
||||||
|
[Command.RETURN]: (key: Key) => key.name === 'return',
|
||||||
[Command.HOME]: (key: Key) => key.ctrl && key.name === 'a',
|
[Command.HOME]: (key: Key) => key.ctrl && key.name === 'a',
|
||||||
[Command.END]: (key: Key) => key.ctrl && key.name === 'e',
|
[Command.END]: (key: Key) => key.ctrl && key.name === 'e',
|
||||||
[Command.KILL_LINE_RIGHT]: (key: Key) => key.ctrl && key.name === 'k',
|
[Command.KILL_LINE_RIGHT]: (key: Key) => key.ctrl && key.name === 'k',
|
||||||
|
@ -34,6 +35,10 @@ describe('keyMatchers', () => {
|
||||||
[Command.NAVIGATION_DOWN]: (key: Key) => key.name === 'down',
|
[Command.NAVIGATION_DOWN]: (key: Key) => key.name === 'down',
|
||||||
[Command.ACCEPT_SUGGESTION]: (key: Key) =>
|
[Command.ACCEPT_SUGGESTION]: (key: Key) =>
|
||||||
key.name === 'tab' || (key.name === 'return' && !key.ctrl),
|
key.name === 'tab' || (key.name === 'return' && !key.ctrl),
|
||||||
|
[Command.COMPLETION_UP]: (key: Key) =>
|
||||||
|
key.name === 'up' || (key.ctrl && key.name === 'p'),
|
||||||
|
[Command.COMPLETION_DOWN]: (key: Key) =>
|
||||||
|
key.name === 'down' || (key.ctrl && key.name === 'n'),
|
||||||
[Command.ESCAPE]: (key: Key) => key.name === 'escape',
|
[Command.ESCAPE]: (key: Key) => key.name === 'escape',
|
||||||
[Command.SUBMIT]: (key: Key) =>
|
[Command.SUBMIT]: (key: Key) =>
|
||||||
key.name === 'return' && !key.ctrl && !key.meta && !key.paste,
|
key.name === 'return' && !key.ctrl && !key.meta && !key.paste,
|
||||||
|
@ -47,10 +52,8 @@ describe('keyMatchers', () => {
|
||||||
key.ctrl && key.name === 't',
|
key.ctrl && key.name === 't',
|
||||||
[Command.TOGGLE_IDE_CONTEXT_DETAIL]: (key: Key) =>
|
[Command.TOGGLE_IDE_CONTEXT_DETAIL]: (key: Key) =>
|
||||||
key.ctrl && key.name === 'e',
|
key.ctrl && key.name === 'e',
|
||||||
[Command.QUIT]: (key: Key) =>
|
[Command.QUIT]: (key: Key) => key.ctrl && key.name === 'c',
|
||||||
key.ctrl && (key.name === 'c' || key.name === 'C'),
|
[Command.EXIT]: (key: Key) => key.ctrl && key.name === 'd',
|
||||||
[Command.EXIT]: (key: Key) =>
|
|
||||||
key.ctrl && (key.name === 'd' || key.name === 'D'),
|
|
||||||
[Command.SHOW_MORE_LINES]: (key: Key) => key.ctrl && key.name === 's',
|
[Command.SHOW_MORE_LINES]: (key: Key) => key.ctrl && key.name === 's',
|
||||||
[Command.REVERSE_SEARCH]: (key: Key) => key.ctrl && key.name === 'r',
|
[Command.REVERSE_SEARCH]: (key: Key) => key.ctrl && key.name === 'r',
|
||||||
[Command.SUBMIT_REVERSE_SEARCH]: (key: Key) =>
|
[Command.SUBMIT_REVERSE_SEARCH]: (key: Key) =>
|
||||||
|
@ -62,6 +65,11 @@ describe('keyMatchers', () => {
|
||||||
// Test data for each command with positive and negative test cases
|
// Test data for each command with positive and negative test cases
|
||||||
const testCases = [
|
const testCases = [
|
||||||
// Basic bindings
|
// Basic bindings
|
||||||
|
{
|
||||||
|
command: Command.RETURN,
|
||||||
|
positive: [createKey('return')],
|
||||||
|
negative: [createKey('r')],
|
||||||
|
},
|
||||||
{
|
{
|
||||||
command: Command.ESCAPE,
|
command: Command.ESCAPE,
|
||||||
positive: [createKey('escape'), createKey('escape', { ctrl: true })],
|
positive: [createKey('escape'), createKey('escape', { ctrl: true })],
|
||||||
|
@ -140,6 +148,16 @@ describe('keyMatchers', () => {
|
||||||
positive: [createKey('tab'), createKey('return')],
|
positive: [createKey('tab'), createKey('return')],
|
||||||
negative: [createKey('return', { ctrl: true }), createKey('space')],
|
negative: [createKey('return', { ctrl: true }), createKey('space')],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
command: Command.COMPLETION_UP,
|
||||||
|
positive: [createKey('up'), createKey('p', { ctrl: true })],
|
||||||
|
negative: [createKey('p'), createKey('down')],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
command: Command.COMPLETION_DOWN,
|
||||||
|
positive: [createKey('down'), createKey('n', { ctrl: true })],
|
||||||
|
negative: [createKey('n'), createKey('up')],
|
||||||
|
},
|
||||||
|
|
||||||
// Text input
|
// Text input
|
||||||
{
|
{
|
||||||
|
@ -194,18 +212,12 @@ describe('keyMatchers', () => {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
command: Command.QUIT,
|
command: Command.QUIT,
|
||||||
positive: [
|
positive: [createKey('c', { ctrl: true })],
|
||||||
createKey('c', { ctrl: true }),
|
|
||||||
createKey('C', { ctrl: true }),
|
|
||||||
],
|
|
||||||
negative: [createKey('c'), createKey('d', { ctrl: true })],
|
negative: [createKey('c'), createKey('d', { ctrl: true })],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
command: Command.EXIT,
|
command: Command.EXIT,
|
||||||
positive: [
|
positive: [createKey('d', { ctrl: true })],
|
||||||
createKey('d', { ctrl: true }),
|
|
||||||
createKey('D', { ctrl: true }),
|
|
||||||
],
|
|
||||||
negative: [createKey('d'), createKey('c', { ctrl: true })],
|
negative: [createKey('d'), createKey('c', { ctrl: true })],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -321,18 +333,5 @@ describe('keyMatchers', () => {
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle case sensitivity', () => {
|
|
||||||
const config: KeyBindingConfig = {
|
|
||||||
...defaultKeyBindings,
|
|
||||||
[Command.QUIT]: [{ key: 'Q', ctrl: true }],
|
|
||||||
};
|
|
||||||
|
|
||||||
const matchers = createKeyMatchers(config);
|
|
||||||
expect(matchers[Command.QUIT](createKey('Q', { ctrl: true }))).toBe(true);
|
|
||||||
expect(matchers[Command.QUIT](createKey('q', { ctrl: true }))).toBe(
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue