shortcuts.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 { selectOccurrence } 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. export const shortcuts = Prec.high(
  39. keymap.of([
  40. {
  41. key: 'Tab',
  42. run: indentMore,
  43. },
  44. {
  45. key: 'Shift-Tab',
  46. run: indentLess,
  47. },
  48. {
  49. key: 'Mod-y',
  50. preventDefault: true,
  51. run: redo,
  52. },
  53. {
  54. key: 'Mod-Shift-z',
  55. preventDefault: true,
  56. run: redo,
  57. },
  58. // defaultKeymap maps Mod-/ to toggleLineComment, but
  59. // w3c-keyname has a hard-coded mapping of Shift+key => character
  60. // which uses a US keyboard layout, so we need to add more mappings.
  61. // Mod-/, but Spanish, Portuguese, German and Swedish keyboard layouts have / at Shift+7
  62. // (keyCode 55, mapped with Shift to &)
  63. {
  64. key: 'Mod-&',
  65. preventDefault: true,
  66. run: toggleLineComment,
  67. },
  68. // Mod-/, but German keyboard layouts have / at Cmd+Shift+ß
  69. // Mod-/, but Czech keyboard layouts have / at Shift-ú
  70. // (keyCode 191, mapped with Shift to ?)
  71. {
  72. key: 'Mod-?',
  73. preventDefault: true,
  74. run: toggleLineComment,
  75. },
  76. // German keyboard layouts map 0xBF to #,
  77. // so VS Code on Windows/Linux uses Ctrl-# to toggle line comments.
  78. // This is an additional, undocumented shortcut for compatibility.
  79. {
  80. key: 'Ctrl-#',
  81. preventDefault: true,
  82. run: toggleLineComment,
  83. },
  84. {
  85. key: 'Ctrl-u',
  86. preventDefault: true,
  87. run: changeCase(true), // uppercase
  88. },
  89. {
  90. key: 'Ctrl-Shift-u',
  91. preventDefault: true,
  92. run: changeCase(false), // lowercase
  93. },
  94. {
  95. key: 'Mod-d',
  96. preventDefault: true,
  97. run: deleteLine,
  98. },
  99. {
  100. key: 'Mod-j',
  101. preventDefault: true,
  102. run: toggleReviewPanel,
  103. },
  104. {
  105. key: 'Mod-Shift-c',
  106. preventDefault: true,
  107. run: addNewCommentFromKbdShortcut,
  108. },
  109. {
  110. key: 'Mod-Shift-a',
  111. preventDefault: true,
  112. run: toggleTrackChangesFromKbdShortcut,
  113. },
  114. {
  115. key: 'Mod-Alt-ArrowUp',
  116. preventDefault: true,
  117. run: cloneSelectionVertically(false, true),
  118. },
  119. {
  120. key: 'Mod-Alt-ArrowDown',
  121. preventDefault: true,
  122. run: cloneSelectionVertically(true, true),
  123. },
  124. {
  125. key: 'Mod-Alt-Shift-ArrowUp',
  126. preventDefault: true,
  127. run: cloneSelectionVertically(false, false),
  128. },
  129. {
  130. key: 'Mod-Alt-Shift-ArrowDown',
  131. preventDefault: true,
  132. run: cloneSelectionVertically(true, false),
  133. },
  134. // duplicates of the above commands, allowing Ctrl on macOS for backwards compatibility
  135. {
  136. mac: 'Ctrl-Alt-ArrowUp',
  137. preventDefault: true,
  138. run: cloneSelectionVertically(false, true),
  139. },
  140. {
  141. mac: 'Ctrl-Alt-ArrowDown',
  142. preventDefault: true,
  143. run: cloneSelectionVertically(true, true),
  144. },
  145. {
  146. mac: 'Ctrl-Alt-Shift-ArrowUp',
  147. preventDefault: true,
  148. run: cloneSelectionVertically(false, false),
  149. },
  150. {
  151. mac: 'Ctrl-Alt-Shift-ArrowDown',
  152. preventDefault: true,
  153. run: cloneSelectionVertically(true, false),
  154. },
  155. {
  156. key: 'Ctrl-Alt-ArrowLeft',
  157. preventDefault: true,
  158. run: selectOccurrence(false),
  159. },
  160. {
  161. key: 'Ctrl-Alt-ArrowRight',
  162. preventDefault: true,
  163. run: selectOccurrence(true),
  164. },
  165. {
  166. key: 'Mod-Shift-d',
  167. run: duplicateSelection,
  168. },
  169. {
  170. win: 'Alt-ArrowLeft',
  171. linux: 'Alt-ArrowLeft',
  172. run: cursorLineBoundaryBackward,
  173. shift: selectLineBoundaryBackward,
  174. preventDefault: true,
  175. },
  176. {
  177. win: 'Alt-ArrowRight',
  178. linux: 'Alt-ArrowRight',
  179. run: cursorLineBoundaryForward,
  180. shift: selectLineBoundaryForward,
  181. preventDefault: true,
  182. },
  183. {
  184. mac: 'Ctrl-ArrowLeft',
  185. run: cursorSyntaxLeft,
  186. shift: selectSyntaxLeft,
  187. },
  188. {
  189. mac: 'Ctrl-ArrowRight',
  190. run: cursorSyntaxRight,
  191. shift: selectSyntaxRight,
  192. },
  193. {
  194. mac: 'Cmd-Backspace',
  195. run: deleteToVisualLineStart,
  196. },
  197. {
  198. mac: 'Cmd-Delete',
  199. run: deleteToVisualLineEnd,
  200. },
  201. ])
  202. )