shortcuts.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { type KeyBinding, 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. export const shortcuts = () => {
  27. const toggleReviewPanel = () => {
  28. dispatchEditorEvent('toggle-review-panel')
  29. return true
  30. }
  31. const addNewCommentFromKbdShortcut = () => {
  32. dispatchEditorEvent('add-new-comment')
  33. return true
  34. }
  35. const toggleTrackChangesFromKbdShortcut = () => {
  36. dispatchEditorEvent('toggle-track-changes')
  37. return true
  38. }
  39. const keyBindings: KeyBinding[] = [
  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. {
  59. key: 'Mod-Shift-/',
  60. preventDefault: true,
  61. run: toggleLineComment,
  62. },
  63. {
  64. key: 'Mod-ß',
  65. preventDefault: true,
  66. run: toggleLineComment,
  67. },
  68. {
  69. key: 'Ctrl-#',
  70. preventDefault: true,
  71. run: toggleLineComment,
  72. },
  73. {
  74. key: 'Ctrl-u',
  75. preventDefault: true,
  76. run: changeCase(true), // uppercase
  77. },
  78. {
  79. key: 'Ctrl-Shift-u',
  80. preventDefault: true,
  81. run: changeCase(false), // lowercase
  82. },
  83. {
  84. key: 'Mod-d',
  85. preventDefault: true,
  86. run: deleteLine,
  87. },
  88. {
  89. key: 'Mod-j',
  90. preventDefault: true,
  91. run: toggleReviewPanel,
  92. },
  93. {
  94. key: 'Mod-Shift-c',
  95. preventDefault: true,
  96. run: addNewCommentFromKbdShortcut,
  97. },
  98. {
  99. key: 'Mod-Shift-a',
  100. preventDefault: true,
  101. run: toggleTrackChangesFromKbdShortcut,
  102. },
  103. {
  104. key: 'Mod-Alt-ArrowUp',
  105. preventDefault: true,
  106. run: cloneSelectionVertically(false, true),
  107. },
  108. {
  109. key: 'Mod-Alt-ArrowDown',
  110. preventDefault: true,
  111. run: cloneSelectionVertically(true, true),
  112. },
  113. {
  114. key: 'Mod-Alt-Shift-ArrowUp',
  115. preventDefault: true,
  116. run: cloneSelectionVertically(false, false),
  117. },
  118. {
  119. key: 'Mod-Alt-Shift-ArrowDown',
  120. preventDefault: true,
  121. run: cloneSelectionVertically(true, false),
  122. },
  123. // duplicates of the above commands, allowing Ctrl on macOS for backwards compatibility
  124. {
  125. mac: 'Ctrl-Alt-ArrowUp',
  126. preventDefault: true,
  127. run: cloneSelectionVertically(false, true),
  128. },
  129. {
  130. mac: 'Ctrl-Alt-ArrowDown',
  131. preventDefault: true,
  132. run: cloneSelectionVertically(true, true),
  133. },
  134. {
  135. mac: 'Ctrl-Alt-Shift-ArrowUp',
  136. preventDefault: true,
  137. run: cloneSelectionVertically(false, false),
  138. },
  139. {
  140. mac: 'Ctrl-Alt-Shift-ArrowDown',
  141. preventDefault: true,
  142. run: cloneSelectionVertically(true, false),
  143. },
  144. {
  145. key: 'Ctrl-Alt-ArrowLeft',
  146. preventDefault: true,
  147. run: selectOccurrence(false),
  148. },
  149. {
  150. key: 'Ctrl-Alt-ArrowRight',
  151. preventDefault: true,
  152. run: selectOccurrence(true),
  153. },
  154. {
  155. key: 'Mod-Shift-d',
  156. run: duplicateSelection,
  157. },
  158. {
  159. win: 'Alt-ArrowLeft',
  160. linux: 'Alt-ArrowLeft',
  161. run: cursorLineBoundaryBackward,
  162. shift: selectLineBoundaryBackward,
  163. preventDefault: true,
  164. },
  165. {
  166. win: 'Alt-ArrowRight',
  167. linux: 'Alt-ArrowRight',
  168. run: cursorLineBoundaryForward,
  169. shift: selectLineBoundaryForward,
  170. preventDefault: true,
  171. },
  172. {
  173. mac: 'Ctrl-ArrowLeft',
  174. run: cursorSyntaxLeft,
  175. shift: selectSyntaxLeft,
  176. },
  177. {
  178. mac: 'Ctrl-ArrowRight',
  179. run: cursorSyntaxRight,
  180. shift: selectSyntaxRight,
  181. },
  182. {
  183. mac: 'Cmd-Backspace',
  184. run: deleteToVisualLineStart,
  185. },
  186. {
  187. mac: 'Cmd-Delete',
  188. run: deleteToVisualLineEnd,
  189. },
  190. ]
  191. return Prec.high(keymap.of(keyBindings))
  192. }