auto-complete.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /**
  80. * Styles for the autocomplete menu
  81. */
  82. const autocompleteTheme = EditorView.baseTheme({
  83. '.cm-tooltip.cm-tooltip-autocomplete': {
  84. // shift the tooltip, so the completion aligns with the text
  85. marginLeft: '-4px',
  86. },
  87. '&light .cm-tooltip.cm-tooltip-autocomplete, &light .cm-tooltip.cm-completionInfo':
  88. {
  89. border: '1px lightgray solid',
  90. background: '#fefefe',
  91. color: '#111',
  92. boxShadow: '2px 3px 5px rgb(0 0 0 / 20%)',
  93. },
  94. '&dark .cm-tooltip.cm-tooltip-autocomplete, &dark .cm-tooltip.cm-completionInfo':
  95. {
  96. border: '1px #484747 solid',
  97. boxShadow: '2px 3px 5px rgba(0, 0, 0, 0.51)',
  98. background: '#25282c',
  99. color: '#c1c1c1',
  100. },
  101. // match editor font family and font size, so the completion aligns with the text
  102. '.cm-tooltip.cm-tooltip-autocomplete > ul': {
  103. fontFamily: 'var(--source-font-family)',
  104. fontSize: 'var(--font-size)',
  105. },
  106. '.cm-tooltip.cm-tooltip-autocomplete li[role="option"]': {
  107. display: 'flex',
  108. justifyContent: 'space-between',
  109. lineHeight: 1.4, // increase the line height from default 1.2, for a larger target area
  110. outline: '1px solid transparent',
  111. },
  112. '&light .cm-tooltip.cm-tooltip-autocomplete li[role="option"]:hover': {
  113. outlineColor: '#abbffe',
  114. backgroundColor: 'rgba(233, 233, 253, 0.4)',
  115. },
  116. '&dark .cm-tooltip.cm-tooltip-autocomplete li[role="option"]:hover': {
  117. outlineColor: 'rgba(109, 150, 13, 0.8)',
  118. backgroundColor: 'rgba(58, 103, 78, 0.62)',
  119. },
  120. '.cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
  121. color: 'inherit',
  122. },
  123. '&light .cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
  124. background: '#cad6fa',
  125. },
  126. '&dark .cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
  127. background: '#3a674e',
  128. },
  129. '.cm-completionMatchedText': {
  130. textDecoration: 'none', // remove default underline,
  131. },
  132. '&light .cm-completionMatchedText': {
  133. color: '#2d69c7',
  134. },
  135. '&dark .cm-completionMatchedText': {
  136. color: '#93ca12',
  137. },
  138. '.ol-cm-completionType': {
  139. paddingLeft: '1em',
  140. paddingRight: 0,
  141. width: 'auto',
  142. fontSize: '90%',
  143. fontFamily: 'var(--source-font-family)',
  144. opacity: '0.5',
  145. },
  146. '.cm-completionInfo .ol-cm-symbolCompletionInfo': {
  147. margin: 0,
  148. whiteSpace: 'normal',
  149. display: 'flex',
  150. flexDirection: 'column',
  151. alignItems: 'center',
  152. textAlign: 'center',
  153. },
  154. '.cm-completionInfo .ol-cm-symbolCharacter': {
  155. fontSize: '32px',
  156. },
  157. })