| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- import { keymap } from '@codemirror/view'
- import { Prec } from '@codemirror/state'
- import { indentMore } from '../commands/indent'
- import {
- indentLess,
- redo,
- deleteLine,
- toggleLineComment,
- cursorLineBoundaryBackward,
- selectLineBoundaryBackward,
- cursorLineBoundaryForward,
- selectLineBoundaryForward,
- cursorSyntaxLeft,
- selectSyntaxLeft,
- cursorSyntaxRight,
- selectSyntaxRight,
- } from '@codemirror/commands'
- import { changeCase, duplicateSelection } from '../commands/ranges'
- import { selectNextOccurrence, selectPrevOccurrence } from '../commands/select'
- import { cloneSelectionVertically } from '../commands/cursor'
- import { dispatchEditorEvent } from './changes/change-manager'
- import {
- deleteToVisualLineEnd,
- deleteToVisualLineStart,
- } from './visual-line-selection'
- const toggleReviewPanel = () => {
- dispatchEditorEvent('toggle-review-panel')
- return true
- }
- const addNewCommentFromKbdShortcut = () => {
- dispatchEditorEvent('add-new-comment')
- return true
- }
- const toggleTrackChangesFromKbdShortcut = () => {
- dispatchEditorEvent('toggle-track-changes')
- return true
- }
- /**
- * Custom key bindings for motion, transformation, selection, history, etc.
- */
- export const shortcuts = Prec.high(
- keymap.of([
- {
- key: 'Tab',
- run: indentMore,
- },
- {
- key: 'Shift-Tab',
- run: indentLess,
- },
- {
- key: 'Mod-y',
- preventDefault: true,
- run: redo,
- },
- {
- key: 'Mod-Shift-z',
- preventDefault: true,
- run: redo,
- },
- // defaultKeymap maps Mod-/ to toggleLineComment, but
- // w3c-keyname has a hard-coded mapping of Shift+key => character
- // which uses a US keyboard layout, so we need to add more mappings.
- // Mod-/, but Spanish, Portuguese, German and Swedish keyboard layouts have / at Shift+7
- // (keyCode 55, mapped with Shift to &)
- {
- key: 'Mod-&',
- preventDefault: true,
- run: toggleLineComment,
- },
- // Mod-/, but German keyboard layouts have / at Cmd+Shift+ß
- // Mod-/, but Czech keyboard layouts have / at Shift-ú
- // (keyCode 191, mapped with Shift to ?)
- {
- key: 'Mod-?',
- preventDefault: true,
- run: toggleLineComment,
- },
- // German keyboard layouts map 0xBF to #,
- // so VS Code on Windows/Linux uses Ctrl-# to toggle line comments.
- // This is an additional, undocumented shortcut for compatibility.
- {
- key: 'Ctrl-#',
- preventDefault: true,
- run: toggleLineComment,
- },
- {
- key: 'Ctrl-u',
- preventDefault: true,
- run: changeCase(true), // uppercase
- },
- {
- key: 'Ctrl-Shift-u',
- preventDefault: true,
- run: changeCase(false), // lowercase
- },
- {
- key: 'Mod-d',
- preventDefault: true,
- run: deleteLine,
- },
- {
- key: 'Mod-j',
- preventDefault: true,
- run: toggleReviewPanel,
- },
- {
- key: 'Mod-Shift-c',
- preventDefault: true,
- run: addNewCommentFromKbdShortcut,
- },
- {
- key: 'Mod-Shift-a',
- preventDefault: true,
- run: toggleTrackChangesFromKbdShortcut,
- },
- {
- key: 'Mod-Alt-ArrowUp',
- preventDefault: true,
- run: cloneSelectionVertically(false, true),
- },
- {
- key: 'Mod-Alt-ArrowDown',
- preventDefault: true,
- run: cloneSelectionVertically(true, true),
- },
- {
- key: 'Mod-Alt-Shift-ArrowUp',
- preventDefault: true,
- run: cloneSelectionVertically(false, false),
- },
- {
- key: 'Mod-Alt-Shift-ArrowDown',
- preventDefault: true,
- run: cloneSelectionVertically(true, false),
- },
- // duplicates of the above commands, allowing Ctrl on macOS for backwards compatibility
- {
- mac: 'Ctrl-Alt-ArrowUp',
- preventDefault: true,
- run: cloneSelectionVertically(false, true),
- },
- {
- mac: 'Ctrl-Alt-ArrowDown',
- preventDefault: true,
- run: cloneSelectionVertically(true, true),
- },
- {
- mac: 'Ctrl-Alt-Shift-ArrowUp',
- preventDefault: true,
- run: cloneSelectionVertically(false, false),
- },
- {
- mac: 'Ctrl-Alt-Shift-ArrowDown',
- preventDefault: true,
- run: cloneSelectionVertically(true, false),
- },
- {
- key: 'Ctrl-Alt-ArrowLeft',
- preventDefault: true,
- run: selectPrevOccurrence,
- },
- {
- key: 'Ctrl-Alt-ArrowRight',
- preventDefault: true,
- run: selectNextOccurrence,
- },
- {
- key: 'Mod-Shift-d',
- run: duplicateSelection,
- },
- {
- win: 'Alt-ArrowLeft',
- linux: 'Alt-ArrowLeft',
- run: cursorLineBoundaryBackward,
- shift: selectLineBoundaryBackward,
- preventDefault: true,
- },
- {
- win: 'Alt-ArrowRight',
- linux: 'Alt-ArrowRight',
- run: cursorLineBoundaryForward,
- shift: selectLineBoundaryForward,
- preventDefault: true,
- },
- {
- mac: 'Ctrl-ArrowLeft',
- run: cursorSyntaxLeft,
- shift: selectSyntaxLeft,
- },
- {
- mac: 'Ctrl-ArrowRight',
- run: cursorSyntaxRight,
- shift: selectSyntaxRight,
- },
- {
- mac: 'Cmd-Backspace',
- run: deleteToVisualLineStart,
- },
- {
- mac: 'Cmd-Delete',
- run: deleteToVisualLineEnd,
- },
- ])
- )
|