shortcuts.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. import { sendMB } from '@/infrastructure/event-tracking'
  27. const toggleReviewPanel = () => {
  28. window.dispatchEvent(new Event('ui.toggle-review-panel'))
  29. return true
  30. }
  31. const addNewCommentFromKbdShortcut = () => {
  32. sendMB('add-comment', {
  33. location: 'shortcut',
  34. })
  35. window.dispatchEvent(new Event('add-new-review-comment'))
  36. return true
  37. }
  38. const toggleTrackChangesFromKbdShortcut = () => {
  39. window.dispatchEvent(new Event('toggle-track-changes'))
  40. return true
  41. }
  42. /**
  43. * Custom key bindings for motion, transformation, selection, history, etc.
  44. */
  45. export const shortcuts = Prec.high(
  46. keymap.of([
  47. {
  48. key: 'Tab',
  49. run: indentMore, // note: not using indentWithTab as the user may want to insert tab spaces within a line
  50. },
  51. {
  52. key: 'Shift-Tab',
  53. run: indentLess, // note: not using indentWithTab as the user may want to insert tab spaces within a line
  54. },
  55. {
  56. key: 'Mod-y',
  57. preventDefault: true,
  58. run: redo,
  59. },
  60. {
  61. key: 'Mod-Shift-z',
  62. preventDefault: true,
  63. run: redo,
  64. },
  65. // defaultKeymap maps Mod-/ to toggleLineComment, but
  66. // w3c-keyname has a hard-coded mapping of Shift+key => character
  67. // which uses a US keyboard layout, so we need to add more mappings.
  68. // Mod-/, but Spanish, Portuguese, German and Swedish keyboard layouts have / at Shift+7
  69. // (keyCode 55, mapped with Shift to &)
  70. {
  71. key: 'Mod-&',
  72. preventDefault: true,
  73. run: toggleLineComment,
  74. },
  75. // Mod-/, but German keyboard layouts have / at Cmd+Shift+ß
  76. // Mod-/, but Czech keyboard layouts have / at Shift-ú
  77. // (keyCode 191, mapped with Shift to ?)
  78. {
  79. key: 'Mod-?',
  80. preventDefault: true,
  81. run: toggleLineComment,
  82. },
  83. // German keyboard layouts map 0xBF to #,
  84. // so VS Code on Windows/Linux uses Ctrl-# to toggle line comments.
  85. // This is an additional, undocumented shortcut for compatibility.
  86. {
  87. key: 'Ctrl-#',
  88. preventDefault: true,
  89. run: toggleLineComment,
  90. },
  91. {
  92. key: 'Ctrl-u',
  93. preventDefault: true,
  94. run: changeCase(true), // uppercase
  95. },
  96. {
  97. key: 'Ctrl-Shift-u',
  98. preventDefault: true,
  99. run: changeCase(false), // lowercase
  100. },
  101. {
  102. key: 'Mod-d',
  103. preventDefault: true,
  104. run(view) {
  105. emitShortcutEvent(view, 'delete-line')
  106. return deleteLine(view)
  107. },
  108. },
  109. {
  110. key: 'Mod-j',
  111. preventDefault: true,
  112. run: toggleReviewPanel,
  113. },
  114. {
  115. key: 'Mod-Shift-c',
  116. preventDefault: true,
  117. run: addNewCommentFromKbdShortcut,
  118. },
  119. {
  120. key: 'Mod-Shift-a',
  121. preventDefault: true,
  122. run: toggleTrackChangesFromKbdShortcut,
  123. },
  124. {
  125. key: 'Cmd-Alt-ArrowUp',
  126. preventDefault: true,
  127. run: cloneSelectionVertically(false, true, 'cmd'),
  128. },
  129. {
  130. key: 'Cmd-Alt-ArrowDown',
  131. preventDefault: true,
  132. run: cloneSelectionVertically(true, true, 'cmd'),
  133. },
  134. {
  135. key: 'Cmd-Alt-Shift-ArrowUp',
  136. preventDefault: true,
  137. run: cloneSelectionVertically(false, false, 'cmd'),
  138. },
  139. {
  140. key: 'Cmd-Alt-Shift-ArrowDown',
  141. preventDefault: true,
  142. run: cloneSelectionVertically(true, false, 'cmd'),
  143. },
  144. // Duplicates of the above commands,
  145. // allowing Ctrl instead of Command (but still tracking the events separately).
  146. // Note: both Ctrl and Commmand versions need to work on macOS, for backwards compatibility,
  147. // so the duplicates shouldn't simply be combined to use `Mod-`.
  148. {
  149. key: 'Ctrl-Alt-ArrowUp',
  150. preventDefault: true,
  151. run: cloneSelectionVertically(false, true, 'ctrl'),
  152. },
  153. {
  154. key: 'Ctrl-Alt-ArrowDown',
  155. preventDefault: true,
  156. run: cloneSelectionVertically(true, true, 'ctrl'),
  157. },
  158. {
  159. key: 'Ctrl-Alt-Shift-ArrowUp',
  160. preventDefault: true,
  161. run: cloneSelectionVertically(false, false, 'ctrl'),
  162. },
  163. {
  164. key: 'Ctrl-Alt-Shift-ArrowDown',
  165. preventDefault: true,
  166. run: cloneSelectionVertically(true, false, 'ctrl'),
  167. },
  168. {
  169. key: 'Ctrl-Alt-ArrowLeft',
  170. preventDefault: true,
  171. run(view) {
  172. emitShortcutEvent(view, 'select-prev-occurrence')
  173. return selectPrevOccurrence(view)
  174. },
  175. },
  176. {
  177. key: 'Ctrl-Alt-ArrowRight',
  178. preventDefault: true,
  179. run(view) {
  180. emitShortcutEvent(view, 'select-next-occurrence')
  181. return selectNextOccurrence(view)
  182. },
  183. },
  184. {
  185. key: 'Mod-Shift-d',
  186. run: duplicateSelection,
  187. },
  188. {
  189. win: 'Alt-ArrowLeft',
  190. linux: 'Alt-ArrowLeft',
  191. run: cursorLineBoundaryBackward,
  192. shift: selectLineBoundaryBackward,
  193. preventDefault: true,
  194. },
  195. {
  196. win: 'Alt-ArrowRight',
  197. linux: 'Alt-ArrowRight',
  198. run: cursorLineBoundaryForward,
  199. shift: selectLineBoundaryForward,
  200. preventDefault: true,
  201. },
  202. {
  203. mac: 'Ctrl-ArrowLeft',
  204. run: cursorSyntaxLeft,
  205. shift: selectSyntaxLeft,
  206. },
  207. {
  208. mac: 'Ctrl-ArrowRight',
  209. run: cursorSyntaxRight,
  210. shift: selectSyntaxRight,
  211. },
  212. {
  213. mac: 'Cmd-Backspace',
  214. run: deleteToVisualLineStart,
  215. },
  216. {
  217. mac: 'Cmd-Delete',
  218. run: deleteToVisualLineEnd,
  219. },
  220. ])
  221. )