auto-complete.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import {
  2. acceptCompletion,
  3. autocompletion,
  4. closeCompletion,
  5. moveCompletionSelection,
  6. startCompletion,
  7. Completion,
  8. } from '@codemirror/autocomplete'
  9. import { EditorView, keymap } from '@codemirror/view'
  10. import { Compartment, Prec, TransactionSpec } from '@codemirror/state'
  11. const autoCompleteConf = new Compartment()
  12. export const autoComplete = ({ autoComplete }: { autoComplete: boolean }) =>
  13. autoCompleteConf.of(createAutoComplete(autoComplete))
  14. export const setAutoComplete = (autoComplete: boolean): TransactionSpec => {
  15. return {
  16. effects: autoCompleteConf.reconfigure(createAutoComplete(autoComplete)),
  17. }
  18. }
  19. const createAutoComplete = (enabled: boolean) => {
  20. if (!enabled) {
  21. return []
  22. }
  23. return [
  24. [
  25. autocompleteTheme,
  26. /**
  27. * A built-in extension which provides the autocomplete feature,
  28. * configured with a custom render function and
  29. * a zero interaction delay (so that keypresses are handled after the autocomplete is opened).
  30. */
  31. autocompletion({
  32. icons: false,
  33. defaultKeymap: false,
  34. addToOptions: [
  35. // display the completion "type" at the end of the suggestion
  36. {
  37. render: completion => {
  38. const span = document.createElement('span')
  39. span.classList.add('ol-cm-completionType')
  40. if (completion.type) {
  41. span.textContent = completion.type
  42. }
  43. return span
  44. },
  45. position: 400,
  46. },
  47. ],
  48. optionClass: (completion: Completion) => {
  49. return `ol-cm-completion-${completion.type}`
  50. },
  51. interactionDelay: 0,
  52. }),
  53. /**
  54. * A keymap which adds Tab for accepting a completion and Ctrl-Space for opening autocomplete.
  55. */
  56. Prec.highest(
  57. keymap.of([
  58. { key: 'Escape', run: closeCompletion },
  59. { key: 'ArrowDown', run: moveCompletionSelection(true) },
  60. { key: 'ArrowUp', run: moveCompletionSelection(false) },
  61. { key: 'PageDown', run: moveCompletionSelection(true, 'page') },
  62. { key: 'PageUp', run: moveCompletionSelection(false, 'page') },
  63. { key: 'Enter', run: acceptCompletion },
  64. { key: 'Tab', run: acceptCompletion },
  65. ])
  66. ),
  67. /**
  68. * A keymap which positions Ctrl-Space and Alt-Space below the corresponding bindings for advanced reference search.
  69. */
  70. Prec.high(
  71. keymap.of([
  72. { key: 'Ctrl-Space', run: startCompletion },
  73. { key: 'Alt-Space', run: startCompletion },
  74. ])
  75. ),
  76. ],
  77. ]
  78. }
  79. const AUTOCOMPLETE_LINE_HEIGHT = 1.4
  80. /**
  81. * Styles for the autocomplete menu
  82. */
  83. const autocompleteTheme = EditorView.baseTheme({
  84. '.cm-tooltip.cm-tooltip-autocomplete': {
  85. // shift the tooltip, so the completion aligns with the text
  86. marginLeft: '-4px',
  87. },
  88. '&light .cm-tooltip.cm-tooltip-autocomplete, &light .cm-tooltip.cm-completionInfo':
  89. {
  90. border: '1px lightgray solid',
  91. background: '#fefefe',
  92. color: '#111',
  93. boxShadow: '2px 3px 5px rgb(0 0 0 / 20%)',
  94. },
  95. '&dark .cm-tooltip.cm-tooltip-autocomplete, &dark .cm-tooltip.cm-completionInfo':
  96. {
  97. border: '1px #484747 solid',
  98. boxShadow: '2px 3px 5px rgba(0, 0, 0, 0.51)',
  99. background: '#25282c',
  100. color: '#c1c1c1',
  101. },
  102. // match editor font family and font size, so the completion aligns with the text
  103. '.cm-tooltip.cm-tooltip-autocomplete > ul': {
  104. fontFamily: 'var(--source-font-family)',
  105. fontSize: 'var(--font-size)',
  106. },
  107. '.cm-tooltip.cm-tooltip-autocomplete li[role="option"]': {
  108. display: 'flex',
  109. justifyContent: 'space-between',
  110. lineHeight: AUTOCOMPLETE_LINE_HEIGHT, // increase the line height from default 1.2, for a larger target area
  111. outline: '1px solid transparent',
  112. },
  113. '.cm-tooltip .cm-completionDetail': {
  114. flex: '1 0 auto',
  115. fontSize: 'calc(var(--font-size) * 1.4)',
  116. lineHeight: `calc(var(--font-size) * ${AUTOCOMPLETE_LINE_HEIGHT})`,
  117. overflow: 'hidden',
  118. // By default CodeMirror styles the details as italic
  119. fontStyle: 'normal !important',
  120. // We use this element for the symbol palette, so change the font to the
  121. // symbol palette font
  122. fontFamily: "'Stix Two Math', serif",
  123. },
  124. '&light .cm-tooltip.cm-tooltip-autocomplete li[role="option"]:hover': {
  125. outlineColor: '#abbffe',
  126. backgroundColor: 'rgba(233, 233, 253, 0.4)',
  127. },
  128. '&dark .cm-tooltip.cm-tooltip-autocomplete li[role="option"]:hover': {
  129. outlineColor: 'rgba(109, 150, 13, 0.8)',
  130. backgroundColor: 'rgba(58, 103, 78, 0.62)',
  131. },
  132. '.cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
  133. color: 'inherit',
  134. },
  135. '&light .cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
  136. background: '#cad6fa',
  137. },
  138. '&dark .cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
  139. background: '#3a674e',
  140. },
  141. '.cm-completionMatchedText': {
  142. textDecoration: 'none', // remove default underline,
  143. },
  144. '&light .cm-completionMatchedText': {
  145. color: '#2d69c7',
  146. },
  147. '&dark .cm-completionMatchedText': {
  148. color: '#93ca12',
  149. },
  150. '.ol-cm-completionType': {
  151. paddingLeft: '1em',
  152. paddingRight: 0,
  153. width: 'auto',
  154. fontSize: '90%',
  155. fontFamily: 'var(--source-font-family)',
  156. opacity: '0.5',
  157. },
  158. '.cm-completionInfo .ol-cm-symbolCompletionInfo': {
  159. margin: 0,
  160. whiteSpace: 'normal',
  161. display: 'flex',
  162. flexDirection: 'column',
  163. alignItems: 'center',
  164. textAlign: 'center',
  165. },
  166. '.cm-completionInfo .ol-cm-symbolCharacter': {
  167. fontSize: '32px',
  168. },
  169. })