shortcuts.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 {
  22. deleteToVisualLineEnd,
  23. deleteToVisualLineStart,
  24. } from './visual-line-selection'
  25. import { emitShortcutEvent } from '@/features/source-editor/extensions/toolbar/utils/analytics'
  26. const toggleReviewPanel = () => {
  27. window.dispatchEvent(new Event('ui.toggle-review-panel'))
  28. return true
  29. }
  30. const addNewCommentFromKbdShortcut = () => {
  31. window.dispatchEvent(new Event('add-new-review-comment'))
  32. return true
  33. }
  34. const toggleTrackChangesFromKbdShortcut = () => {
  35. window.dispatchEvent(new Event('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, // note: not using indentWithTab as the user may want to insert tab spaces within a line
  46. },
  47. {
  48. key: 'Shift-Tab',
  49. run: indentLess, // note: not using indentWithTab as the user may want to insert tab spaces within a line
  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(view) {
  101. emitShortcutEvent(view, 'delete-line')
  102. return deleteLine(view)
  103. },
  104. },
  105. {
  106. key: 'Mod-j',
  107. preventDefault: true,
  108. run: toggleReviewPanel,
  109. },
  110. {
  111. key: 'Mod-Shift-c',
  112. preventDefault: true,
  113. run: addNewCommentFromKbdShortcut,
  114. },
  115. {
  116. key: 'Mod-Shift-a',
  117. preventDefault: true,
  118. run: toggleTrackChangesFromKbdShortcut,
  119. },
  120. {
  121. key: 'Cmd-Alt-ArrowUp',
  122. preventDefault: true,
  123. run: cloneSelectionVertically(false, true, 'cmd'),
  124. },
  125. {
  126. key: 'Cmd-Alt-ArrowDown',
  127. preventDefault: true,
  128. run: cloneSelectionVertically(true, true, 'cmd'),
  129. },
  130. {
  131. key: 'Cmd-Alt-Shift-ArrowUp',
  132. preventDefault: true,
  133. run: cloneSelectionVertically(false, false, 'cmd'),
  134. },
  135. {
  136. key: 'Cmd-Alt-Shift-ArrowDown',
  137. preventDefault: true,
  138. run: cloneSelectionVertically(true, false, 'cmd'),
  139. },
  140. // Duplicates of the above commands,
  141. // allowing Ctrl instead of Command (but still tracking the events separately).
  142. // Note: both Ctrl and Commmand versions need to work on macOS, for backwards compatibility,
  143. // so the duplicates shouldn't simply be combined to use `Mod-`.
  144. {
  145. key: 'Ctrl-Alt-ArrowUp',
  146. preventDefault: true,
  147. run: cloneSelectionVertically(false, true, 'ctrl'),
  148. },
  149. {
  150. key: 'Ctrl-Alt-ArrowDown',
  151. preventDefault: true,
  152. run: cloneSelectionVertically(true, true, 'ctrl'),
  153. },
  154. {
  155. key: 'Ctrl-Alt-Shift-ArrowUp',
  156. preventDefault: true,
  157. run: cloneSelectionVertically(false, false, 'ctrl'),
  158. },
  159. {
  160. key: 'Ctrl-Alt-Shift-ArrowDown',
  161. preventDefault: true,
  162. run: cloneSelectionVertically(true, false, 'ctrl'),
  163. },
  164. {
  165. key: 'Ctrl-Alt-ArrowLeft',
  166. preventDefault: true,
  167. run(view) {
  168. emitShortcutEvent(view, 'select-prev-occurrence')
  169. return selectPrevOccurrence(view)
  170. },
  171. },
  172. {
  173. key: 'Ctrl-Alt-ArrowRight',
  174. preventDefault: true,
  175. run(view) {
  176. emitShortcutEvent(view, 'select-next-occurrence')
  177. return selectNextOccurrence(view)
  178. },
  179. },
  180. {
  181. key: 'Mod-Shift-d',
  182. run: duplicateSelection,
  183. },
  184. {
  185. win: 'Alt-ArrowLeft',
  186. linux: 'Alt-ArrowLeft',
  187. run: cursorLineBoundaryBackward,
  188. shift: selectLineBoundaryBackward,
  189. preventDefault: true,
  190. },
  191. {
  192. win: 'Alt-ArrowRight',
  193. linux: 'Alt-ArrowRight',
  194. run: cursorLineBoundaryForward,
  195. shift: selectLineBoundaryForward,
  196. preventDefault: true,
  197. },
  198. {
  199. mac: 'Ctrl-ArrowLeft',
  200. run: cursorSyntaxLeft,
  201. shift: selectSyntaxLeft,
  202. },
  203. {
  204. mac: 'Ctrl-ArrowRight',
  205. run: cursorSyntaxRight,
  206. shift: selectSyntaxRight,
  207. },
  208. {
  209. mac: 'Cmd-Backspace',
  210. run: deleteToVisualLineStart,
  211. },
  212. {
  213. mac: 'Cmd-Delete',
  214. run: deleteToVisualLineEnd,
  215. },
  216. ])
  217. )