index.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {
  2. EditorView,
  3. rectangularSelection,
  4. tooltips,
  5. crosshairCursor,
  6. dropCursor,
  7. highlightActiveLineGutter,
  8. } from '@codemirror/view'
  9. import { EditorState, Extension } from '@codemirror/state'
  10. import { foldGutter, indentOnInput, indentUnit } from '@codemirror/language'
  11. import { history } from '@codemirror/commands'
  12. import { language } from './language'
  13. import { lineWrappingIndentation } from './line-wrapping-indentation'
  14. import { theme } from './theme'
  15. import { realtime } from './realtime'
  16. import { cursorPosition } from './cursor-position'
  17. import { scrollPosition } from './scroll-position'
  18. import { annotations } from './annotations'
  19. import { cursorHighlights } from './cursor-highlights'
  20. import { autoComplete } from './auto-complete'
  21. import { editable } from './editable'
  22. import { autoPair } from './auto-pair'
  23. import { phrases } from './phrases'
  24. import { spelling } from './spelling'
  25. import { symbolPalette } from './symbol-palette'
  26. import { trackChanges } from './track-changes'
  27. import { search } from './search'
  28. import { filterCharacters } from './filter-characters'
  29. import { keybindings } from './keybindings'
  30. import { bracketMatching, bracketSelection } from './bracket-matching'
  31. import { verticalOverflow } from './vertical-overflow'
  32. import { exceptionLogger } from './exception-logger'
  33. import { thirdPartyExtensions } from './third-party-extensions'
  34. import { lineNumbers } from './line-numbers'
  35. import { highlightActiveLine } from './highlight-active-line'
  36. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  37. import { emptyLineFiller } from './empty-line-filler'
  38. import { goToLinePanel } from './go-to-line'
  39. import { drawSelection } from './draw-selection'
  40. import { visual } from './visual/visual'
  41. import { inlineBackground } from './inline-background'
  42. import { indentationMarkers } from './indentation-markers'
  43. import { codemirrorDevTools } from '../languages/latex/codemirror-dev-tools'
  44. import { keymaps } from './keymaps'
  45. import { shortcuts } from './shortcuts'
  46. import { effectListeners } from './effect-listeners'
  47. import { highlightSpecialChars } from './highlight-special-chars'
  48. import { toolbarPanel } from './toolbar/toolbar-panel'
  49. import { geometryChangeEvent } from './geometry-change-event'
  50. import { isSplitTestEnabled } from '../../../utils/splitTestUtils'
  51. import { completionLogger } from './completion-logger'
  52. import { shortcutLogger } from './shortcut-logger'
  53. const moduleExtensions: Array<() => Extension> = importOverleafModules(
  54. 'sourceEditorExtensions'
  55. ).map((item: { import: { extension: Extension } }) => item.import.extension)
  56. export const createExtensions = (options: Record<string, any>): Extension[] => [
  57. lineNumbers(),
  58. highlightSpecialChars(options.visual.visual),
  59. // The built-in extension that manages the history stack,
  60. // configured to increase the maximum delay between adjacent grouped edits
  61. history({ newGroupDelay: 250 }),
  62. // The built-in extension that displays buttons for folding code in a gutter element,
  63. // configured with custom openText and closeText symbols.
  64. foldGutter({
  65. openText: '▾',
  66. closedText: '▸',
  67. }),
  68. drawSelection(),
  69. // A built-in facet that is set to true to allow multiple selections.
  70. // This makes the editor more like a code editor than Google Docs or Microsoft Word,
  71. // which only have single selections.
  72. EditorState.allowMultipleSelections.of(true),
  73. // A built-in extension that enables soft line wrapping.
  74. EditorView.lineWrapping,
  75. // A built-in extension that re-indents input if the language defines an indentOnInput field in its language data.
  76. indentOnInput(),
  77. lineWrappingIndentation(options.visual.visual),
  78. indentationMarkers(options.visual.visual),
  79. bracketMatching(),
  80. bracketSelection(),
  81. // A built-in extension that enables rectangular selections, created by dragging a new selection while holding down Alt.
  82. rectangularSelection(),
  83. // A built-in extension that turns the pointer into a crosshair while Alt is pressed.
  84. crosshairCursor(),
  85. // A built-in extension that shows where dragged content will be dropped.
  86. dropCursor(),
  87. // A built-in extension that is used for configuring tooltip behaviour,
  88. // configured so that the tooltip parent is the document body,
  89. // to avoid cutting off tooltips which overflow the editor.
  90. tooltips({
  91. parent: document.body,
  92. }),
  93. keymaps,
  94. goToLinePanel(),
  95. filterCharacters(),
  96. // NOTE: `autoComplete` needs to be before `keybindings` so that arrow key handling
  97. // in the autocomplete pop-up takes precedence over Vim/Emacs key bindings
  98. autoComplete(options.settings),
  99. // NOTE: `keybindings` needs to be before `language` so that Vim/Emacs bindings take
  100. // precedence over language-specific keyboard shortcuts
  101. keybindings(),
  102. // NOTE: `annotations` needs to be before `language`
  103. annotations(),
  104. language(options.currentDoc, options.metadata, options.settings),
  105. indentUnit.of(' '), // 4 spaces
  106. theme(options.theme),
  107. realtime(options.currentDoc, options.handleError),
  108. cursorPosition(options.currentDoc),
  109. scrollPosition(options.currentDoc),
  110. cursorHighlights(),
  111. autoPair(options.settings),
  112. editable(),
  113. search(),
  114. phrases(options.phrases),
  115. spelling(options.spelling),
  116. shortcuts,
  117. symbolPalette(),
  118. // NOTE: `emptyLineFiller` needs to be before `trackChanges`,
  119. // so the decorations are added in the correct order.
  120. emptyLineFiller(),
  121. trackChanges(options.currentDoc, options.changeManager),
  122. visual(options.currentDoc, options.visual),
  123. isSplitTestEnabled('source-editor-toolbar') ? toolbarPanel() : [],
  124. verticalOverflow(),
  125. highlightActiveLine(options.visual.visual),
  126. // The built-in extension that highlights the active line in the gutter.
  127. highlightActiveLineGutter(),
  128. inlineBackground(options.visual.visual),
  129. completionLogger,
  130. shortcutLogger,
  131. codemirrorDevTools(),
  132. exceptionLogger(),
  133. // CodeMirror extensions provided by modules
  134. moduleExtensions.map(extension => extension()),
  135. thirdPartyExtensions(),
  136. effectListeners(),
  137. geometryChangeEvent(options.reactReviewPanel),
  138. ]