shortcuts.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { keymap } from '@codemirror/view'
  2. import { Prec } from '@codemirror/state'
  3. import { indentMore } from '../commands/indent'
  4. import {
  5. indentLess,
  6. redo,
  7. deleteLine,
  8. toggleLineComment,
  9. cursorLineBoundaryBackward,
  10. selectLineBoundaryBackward,
  11. cursorLineBoundaryForward,
  12. selectLineBoundaryForward,
  13. cursorSyntaxLeft,
  14. selectSyntaxLeft,
  15. cursorSyntaxRight,
  16. selectSyntaxRight,
  17. } from '@codemirror/commands'
  18. import { changeCase, duplicateSelection } from '../commands/ranges'
  19. import { selectNextOccurrence, selectPrevOccurrence } from '../commands/select'
  20. import { cloneSelectionVertically } from '../commands/cursor'
  21. import { dispatchEditorEvent } from './changes/change-manager'
  22. import {
  23. deleteToVisualLineEnd,
  24. deleteToVisualLineStart,
  25. } from './visual-line-selection'
  26. const toggleReviewPanel = () => {
  27. dispatchEditorEvent('toggle-review-panel')
  28. return true
  29. }
  30. const addNewCommentFromKbdShortcut = () => {
  31. dispatchEditorEvent('add-new-comment')
  32. return true
  33. }
  34. const toggleTrackChangesFromKbdShortcut = () => {
  35. dispatchEditorEvent('toggle-track-changes')
  36. return true
  37. }
  38. /**
  39. * Custom key bindings for motion, transformation, selection, history, etc.
  40. */
  41. export const shortcuts = Prec.high(
  42. keymap.of([
  43. {
  44. key: 'Tab',
  45. run: indentMore,
  46. },
  47. {
  48. key: 'Shift-Tab',
  49. run: indentLess,
  50. },
  51. {
  52. key: 'Mod-y',
  53. preventDefault: true,
  54. run: redo,
  55. },
  56. {
  57. key: 'Mod-Shift-z',
  58. preventDefault: true,
  59. run: redo,
  60. },
  61. // defaultKeymap maps Mod-/ to toggleLineComment, but
  62. // w3c-keyname has a hard-coded mapping of Shift+key => character
  63. // which uses a US keyboard layout, so we need to add more mappings.
  64. // Mod-/, but Spanish, Portuguese, German and Swedish keyboard layouts have / at Shift+7
  65. // (keyCode 55, mapped with Shift to &)
  66. {
  67. key: 'Mod-&',
  68. preventDefault: true,
  69. run: toggleLineComment,
  70. },
  71. // Mod-/, but German keyboard layouts have / at Cmd+Shift+ß
  72. // Mod-/, but Czech keyboard layouts have / at Shift-ú
  73. // (keyCode 191, mapped with Shift to ?)
  74. {
  75. key: 'Mod-?',
  76. preventDefault: true,
  77. run: toggleLineComment,
  78. },
  79. // German keyboard layouts map 0xBF to #,
  80. // so VS Code on Windows/Linux uses Ctrl-# to toggle line comments.
  81. // This is an additional, undocumented shortcut for compatibility.
  82. {
  83. key: 'Ctrl-#',
  84. preventDefault: true,
  85. run: toggleLineComment,
  86. },
  87. {
  88. key: 'Ctrl-u',
  89. preventDefault: true,
  90. run: changeCase(true), // uppercase
  91. },
  92. {
  93. key: 'Ctrl-Shift-u',
  94. preventDefault: true,
  95. run: changeCase(false), // lowercase
  96. },
  97. {
  98. key: 'Mod-d',
  99. preventDefault: true,
  100. run: deleteLine,
  101. },
  102. {
  103. key: 'Mod-j',
  104. preventDefault: true,
  105. run: toggleReviewPanel,
  106. },
  107. {
  108. key: 'Mod-Shift-c',
  109. preventDefault: true,
  110. run: addNewCommentFromKbdShortcut,
  111. },
  112. {
  113. key: 'Mod-Shift-a',
  114. preventDefault: true,
  115. run: toggleTrackChangesFromKbdShortcut,
  116. },
  117. {
  118. key: 'Mod-Alt-ArrowUp',
  119. preventDefault: true,
  120. run: cloneSelectionVertically(false, true),
  121. },
  122. {
  123. key: 'Mod-Alt-ArrowDown',
  124. preventDefault: true,
  125. run: cloneSelectionVertically(true, true),
  126. },
  127. {
  128. key: 'Mod-Alt-Shift-ArrowUp',
  129. preventDefault: true,
  130. run: cloneSelectionVertically(false, false),
  131. },
  132. {
  133. key: 'Mod-Alt-Shift-ArrowDown',
  134. preventDefault: true,
  135. run: cloneSelectionVertically(true, false),
  136. },
  137. // duplicates of the above commands, allowing Ctrl on macOS for backwards compatibility
  138. {
  139. mac: 'Ctrl-Alt-ArrowUp',
  140. preventDefault: true,
  141. run: cloneSelectionVertically(false, true),
  142. },
  143. {
  144. mac: 'Ctrl-Alt-ArrowDown',
  145. preventDefault: true,
  146. run: cloneSelectionVertically(true, true),
  147. },
  148. {
  149. mac: 'Ctrl-Alt-Shift-ArrowUp',
  150. preventDefault: true,
  151. run: cloneSelectionVertically(false, false),
  152. },
  153. {
  154. mac: 'Ctrl-Alt-Shift-ArrowDown',
  155. preventDefault: true,
  156. run: cloneSelectionVertically(true, false),
  157. },
  158. {
  159. key: 'Ctrl-Alt-ArrowLeft',
  160. preventDefault: true,
  161. run: selectPrevOccurrence,
  162. },
  163. {
  164. key: 'Ctrl-Alt-ArrowRight',
  165. preventDefault: true,
  166. run: selectNextOccurrence,
  167. },
  168. {
  169. key: 'Mod-Shift-d',
  170. run: duplicateSelection,
  171. },
  172. {
  173. win: 'Alt-ArrowLeft',
  174. linux: 'Alt-ArrowLeft',
  175. run: cursorLineBoundaryBackward,
  176. shift: selectLineBoundaryBackward,
  177. preventDefault: true,
  178. },
  179. {
  180. win: 'Alt-ArrowRight',
  181. linux: 'Alt-ArrowRight',
  182. run: cursorLineBoundaryForward,
  183. shift: selectLineBoundaryForward,
  184. preventDefault: true,
  185. },
  186. {
  187. mac: 'Ctrl-ArrowLeft',
  188. run: cursorSyntaxLeft,
  189. shift: selectSyntaxLeft,
  190. },
  191. {
  192. mac: 'Ctrl-ArrowRight',
  193. run: cursorSyntaxRight,
  194. shift: selectSyntaxRight,
  195. },
  196. {
  197. mac: 'Cmd-Backspace',
  198. run: deleteToVisualLineStart,
  199. },
  200. {
  201. mac: 'Cmd-Delete',
  202. run: deleteToVisualLineEnd,
  203. },
  204. ])
  205. )