auto-complete.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. autocompletion({
  27. icons: false,
  28. defaultKeymap: false,
  29. addToOptions: [
  30. // display the completion "type" at the end of the suggestion
  31. {
  32. render: completion => {
  33. const span = document.createElement('span')
  34. span.classList.add('ol-cm-completionType')
  35. if (completion.type) {
  36. span.textContent = completion.type
  37. }
  38. return span
  39. },
  40. position: 400,
  41. },
  42. ],
  43. optionClass: (completion: Completion) => {
  44. return `ol-cm-completion-${completion.type}`
  45. },
  46. interactionDelay: 0,
  47. }),
  48. Prec.highest(
  49. keymap.of([
  50. { key: 'Escape', run: closeCompletion },
  51. { key: 'ArrowDown', run: moveCompletionSelection(true) },
  52. { key: 'ArrowUp', run: moveCompletionSelection(false) },
  53. { key: 'PageDown', run: moveCompletionSelection(true, 'page') },
  54. { key: 'PageUp', run: moveCompletionSelection(false, 'page') },
  55. { key: 'Enter', run: acceptCompletion },
  56. { key: 'Tab', run: acceptCompletion },
  57. ])
  58. ),
  59. // NOTE: must be lower precedence than Ctrl-Space and Alt-Space in references-search
  60. Prec.high(
  61. keymap.of([
  62. { key: 'Ctrl-Space', run: startCompletion },
  63. { key: 'Alt-Space', run: startCompletion },
  64. ])
  65. ),
  66. ],
  67. ]
  68. }
  69. const autocompleteTheme = EditorView.baseTheme({
  70. '.cm-tooltip.cm-tooltip-autocomplete': {
  71. // shift the tooltip, so the completion aligns with the text
  72. marginLeft: '-4px',
  73. },
  74. '&light .cm-tooltip.cm-tooltip-autocomplete, &light .cm-tooltip.cm-completionInfo':
  75. {
  76. border: '1px lightgray solid',
  77. background: '#fefefe',
  78. color: '#111',
  79. boxShadow: '2px 3px 5px rgb(0 0 0 / 20%)',
  80. },
  81. '&dark .cm-tooltip.cm-tooltip-autocomplete, &dark .cm-tooltip.cm-completionInfo':
  82. {
  83. border: '1px #484747 solid',
  84. boxShadow: '2px 3px 5px rgba(0, 0, 0, 0.51)',
  85. background: '#25282c',
  86. color: '#c1c1c1',
  87. },
  88. // match editor font family and font size, so the completion aligns with the text
  89. '.cm-tooltip.cm-tooltip-autocomplete > ul': {
  90. fontFamily: 'var(--source-font-family)',
  91. fontSize: 'var(--font-size)',
  92. },
  93. '.cm-tooltip.cm-tooltip-autocomplete li[role="option"]': {
  94. display: 'flex',
  95. justifyContent: 'space-between',
  96. lineHeight: 1.4, // increase the line height from default 1.2, for a larger target area
  97. outline: '1px solid transparent',
  98. },
  99. '&light .cm-tooltip.cm-tooltip-autocomplete li[role="option"]:hover': {
  100. outlineColor: '#abbffe',
  101. backgroundColor: 'rgba(233, 233, 253, 0.4)',
  102. },
  103. '&dark .cm-tooltip.cm-tooltip-autocomplete li[role="option"]:hover': {
  104. outlineColor: 'rgba(109, 150, 13, 0.8)',
  105. backgroundColor: 'rgba(58, 103, 78, 0.62)',
  106. },
  107. '.cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
  108. color: 'inherit',
  109. },
  110. '&light .cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
  111. background: '#cad6fa',
  112. },
  113. '&dark .cm-tooltip.cm-tooltip-autocomplete ul li[aria-selected]': {
  114. background: '#3a674e',
  115. },
  116. '.cm-completionMatchedText': {
  117. textDecoration: 'none', // remove default underline,
  118. fontWeight: 'bold',
  119. },
  120. '&light .cm-completionMatchedText': {
  121. color: '#2d69c7',
  122. },
  123. '&dark .cm-completionMatchedText': {
  124. color: '#93ca12',
  125. },
  126. '.ol-cm-completionType': {
  127. paddingLeft: '1em',
  128. paddingRight: 0,
  129. width: 'auto',
  130. fontSize: '90%',
  131. fontFamily: 'var(--source-font-family)',
  132. opacity: '0.5',
  133. },
  134. '.cm-completionInfo .ol-cm-symbolCompletionInfo': {
  135. margin: 0,
  136. whiteSpace: 'normal',
  137. display: 'flex',
  138. flexDirection: 'column',
  139. alignItems: 'center',
  140. textAlign: 'center',
  141. },
  142. '.cm-completionInfo .ol-cm-symbolCharacter': {
  143. fontSize: '32px',
  144. },
  145. })