shortcuts.ts 5.3 KB

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