shortcuts.ts 5.6 KB

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