-
Notifications
You must be signed in to change notification settings - Fork 0
Mirror VS Code workbench shortcuts in terminal #112
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
3 commits
Select commit
Hold shift + click to select a range
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { vscodeWorkbenchCommandForKeydown } from './vscode-keybindings'; | ||
|
|
||
| function keydown(init: Partial<KeyboardEventInit> & { key: string }): KeyboardEvent { | ||
| return new KeyboardEvent('keydown', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| ...init, | ||
| }); | ||
| } | ||
|
|
||
| describe('vscodeWorkbenchCommandForKeydown', () => { | ||
| it('maps Windows/Linux VS Code workbench chords', () => { | ||
| const opts = { isMac: false }; | ||
|
|
||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'p', code: 'KeyP', ctrlKey: true }), opts)).toBe('workbench.action.quickOpen'); | ||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'P', code: 'KeyP', ctrlKey: true, shiftKey: true }), opts)).toBe('workbench.action.showCommands'); | ||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'b', code: 'KeyB', ctrlKey: true }), opts)).toBe('workbench.action.toggleSidebarVisibility'); | ||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'F1', code: 'F1' }), opts)).toBe('workbench.action.showCommands'); | ||
| }); | ||
|
|
||
| it('uses Cmd as the platform modifier on macOS', () => { | ||
| const opts = { isMac: true }; | ||
|
|
||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'p', code: 'KeyP', metaKey: true }), opts)).toBe('workbench.action.quickOpen'); | ||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'P', code: 'KeyP', metaKey: true, shiftKey: true }), opts)).toBe('workbench.action.showCommands'); | ||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'b', code: 'KeyB', metaKey: true }), opts)).toBe('workbench.action.toggleSidebarVisibility'); | ||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'p', code: 'KeyP', ctrlKey: true }), opts)).toBe(null); | ||
| }); | ||
|
|
||
| it('keeps unrelated terminal control chords in xterm only', () => { | ||
| const opts = { isMac: false }; | ||
|
|
||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'r', code: 'KeyR', ctrlKey: true }), opts)).toBe(null); | ||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'c', code: 'KeyC', ctrlKey: true }), opts)).toBe(null); | ||
| expect(vscodeWorkbenchCommandForKeydown(keydown({ key: 'p', code: 'KeyP', ctrlKey: true, altKey: true }), opts)).toBe(null); | ||
| }); | ||
|
|
||
| it('only maps keydown events', () => { | ||
| const event = new KeyboardEvent('keyup', { key: 'p', code: 'KeyP', ctrlKey: true }); | ||
| expect(vscodeWorkbenchCommandForKeydown(event, { isMac: false })).toBe(null); | ||
| }); | ||
| }); | ||
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,47 @@ | ||
| type KeyboardEventLike = Pick< | ||
| KeyboardEvent, | ||
| 'altKey' | 'code' | 'ctrlKey' | 'isComposing' | 'key' | 'metaKey' | 'shiftKey' | 'type' | ||
| >; | ||
|
|
||
| /** The workbench commands the VS Code host is allowed to run on the webview's behalf. */ | ||
| export const VSCODE_WORKBENCH_COMMANDS = [ | ||
| 'workbench.action.quickOpen', | ||
| 'workbench.action.showCommands', | ||
| 'workbench.action.toggleSidebarVisibility', | ||
| ] as const; | ||
|
|
||
| export type VSCodeWorkbenchCommand = (typeof VSCODE_WORKBENCH_COMMANDS)[number]; | ||
|
|
||
| /** | ||
| * Xterm keyboard handling changes when foreground apps enable enhanced | ||
| * keyboard protocols, which makes VS Code workbench chords inconsistent. For | ||
| * the allowlisted chords, Dormouse lets xterm keep processing the key and also | ||
| * asks the VS Code host to run the matching workbench command. | ||
| */ | ||
| export function vscodeWorkbenchCommandForKeydown( | ||
| event: KeyboardEventLike, | ||
| options: { isMac: boolean }, | ||
| ): VSCodeWorkbenchCommand | null { | ||
| if (event.type !== 'keydown') return null; | ||
| if (event.isComposing) return null; | ||
|
|
||
| if (!event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey && event.key === 'F1') { | ||
| return 'workbench.action.showCommands'; | ||
| } | ||
|
|
||
| const platformMod = options.isMac ? event.metaKey : event.ctrlKey; | ||
| if (!platformMod || event.altKey) return null; | ||
|
|
||
| const key = event.key.toLowerCase(); | ||
| const isP = key === 'p' || event.code === 'KeyP'; | ||
| if (isP) { | ||
| return event.shiftKey | ||
| ? 'workbench.action.showCommands' | ||
| : 'workbench.action.quickOpen'; | ||
| } | ||
|
|
||
| const isB = key === 'b' || event.code === 'KeyB'; | ||
| if (isB && !event.shiftKey) return 'workbench.action.toggleSidebarVisibility'; | ||
|
|
||
| return null; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pin the fix from e878eb8 with a regression test so the
!event.shiftKeyguard on the F1 branch isn't silently dropped later.