-
Notifications
You must be signed in to change notification settings - Fork 942
feat(tui): open undo selector on double-Esc #1220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fd46246
feat(tui): open undo selector on double-Esc
liruifengv 1b5f43f
fix(tui): disarm double-Esc undo on any intervening key
liruifengv 16acaa0
Merge branch 'main' into feat/double-esc-undo
liruifengv cf67e5a
Merge branch 'main' into feat/double-esc-undo
liruifengv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| Add a double-Esc shortcut to open the undo selector. Press Esc twice while idle to undo. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
apps/kimi-code/test/tui/controllers/editor-keyboard.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { DOUBLE_ESC_WINDOW_MS } from '#/tui/constant/kimi-tui'; | ||
| import { | ||
| EditorKeyboardController, | ||
| type EditorKeyboardHost, | ||
| } from '#/tui/controllers/editor-keyboard'; | ||
| import type { ImageAttachmentStore } from '#/tui/utils/image-attachment-store'; | ||
|
|
||
| interface Harness { | ||
| readonly host: EditorKeyboardHost; | ||
| readonly editor: Record<string, ((...args: never[]) => unknown) | undefined>; | ||
| readonly openUndoSelector: ReturnType<typeof vi.fn>; | ||
| readonly cancelRunningShellCommand: ReturnType<typeof vi.fn>; | ||
| } | ||
|
|
||
| function createHarness(options: { streamingPhase?: string; isCompacting?: boolean } = {}): Harness { | ||
| const editor: Record<string, ((...args: never[]) => unknown) | undefined> = {}; | ||
| const openUndoSelector = vi.fn(); | ||
| const cancelRunningShellCommand = vi.fn(); | ||
| const session = { cancel: vi.fn(async () => {}) }; | ||
|
|
||
| const host = { | ||
| state: { | ||
| editor, | ||
| activeDialog: null, | ||
| appState: { | ||
| streamingPhase: options.streamingPhase ?? 'idle', | ||
| isCompacting: options.isCompacting ?? false, | ||
| }, | ||
| footer: { setTransientHint: vi.fn() }, | ||
| ui: { requestRender: vi.fn() }, | ||
| }, | ||
| session, | ||
| btwPanelController: { closeOrCancel: vi.fn(() => false) }, | ||
| openUndoSelector, | ||
| cancelRunningShellCommand, | ||
| } as unknown as EditorKeyboardHost; | ||
|
|
||
| const controller = new EditorKeyboardController( | ||
| host, | ||
| undefined as unknown as ImageAttachmentStore, | ||
| ); | ||
| controller.install(); | ||
|
|
||
| return { host, editor, openUndoSelector, cancelRunningShellCommand }; | ||
| } | ||
|
|
||
| function pressEscape(editor: Harness['editor']): void { | ||
| const handler = editor['onEscape']; | ||
| if (handler === undefined) throw new Error('onEscape handler not installed'); | ||
| (handler as () => void)(); | ||
| } | ||
|
|
||
| function pressNonEscape(editor: Harness['editor']): void { | ||
| const handler = editor['onNonEscapeInput']; | ||
| if (handler === undefined) throw new Error('onNonEscapeInput handler not installed'); | ||
| (handler as () => void)(); | ||
| } | ||
|
|
||
| describe('EditorKeyboardController double-Esc undo', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it('opens the undo selector when Esc is pressed twice within the window while idle', () => { | ||
| const { editor, openUndoSelector } = createHarness(); | ||
|
|
||
| pressEscape(editor); | ||
| expect(openUndoSelector).not.toHaveBeenCalled(); | ||
|
|
||
| pressEscape(editor); | ||
| expect(openUndoSelector).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('does nothing for a single Esc while idle', () => { | ||
| const { editor, openUndoSelector } = createHarness(); | ||
|
|
||
| pressEscape(editor); | ||
|
|
||
| expect(openUndoSelector).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not trigger when the second Esc arrives after the window expires', () => { | ||
| const { editor, openUndoSelector } = createHarness(); | ||
|
|
||
| pressEscape(editor); | ||
| vi.advanceTimersByTime(DOUBLE_ESC_WINDOW_MS + 1); | ||
| pressEscape(editor); | ||
|
|
||
| expect(openUndoSelector).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not trigger when another key is pressed between the two Esc presses', () => { | ||
| const { editor, openUndoSelector } = createHarness(); | ||
|
|
||
| pressEscape(editor); | ||
| pressNonEscape(editor); | ||
| pressEscape(editor); | ||
|
|
||
| expect(openUndoSelector).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not trigger undo while streaming; Esc cancels the stream instead', () => { | ||
| const { editor, host, openUndoSelector, cancelRunningShellCommand } = createHarness({ | ||
| streamingPhase: 'waiting', | ||
| }); | ||
|
|
||
| pressEscape(editor); | ||
| pressEscape(editor); | ||
|
|
||
| expect(openUndoSelector).not.toHaveBeenCalled(); | ||
| expect(cancelRunningShellCommand).toHaveBeenCalled(); | ||
| const session = host.session as unknown as { cancel: ReturnType<typeof vi.fn> }; | ||
| expect(session.cancel).toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.