import jsdoc from '../../doc.json'; import { autocompletion } from '@codemirror/autocomplete'; import { h } from './html'; const escapeHtml = (str) => { const div = document.createElement('div'); div.innerText = str; return div.innerHTML; }; const stripHtml = (html) => { const div = document.createElement('div'); div.innerHTML = html; return div.textContent || div.innerText || ''; }; const getDocLabel = (doc) => doc.name || doc.longname; const buildParamsList = (params) => params?.length ? `

Parameters

` : ''; const buildExamples = (examples) => examples?.length ? `

Examples

${examples .map( (example) => `
${escapeHtml(example)}
`, ) .join('')}
` : ''; export const Autocomplete = ({ doc, label }) => h`

${label || getDocLabel(doc)}

${doc.description ? `

${doc.description}

` : ''} ${buildParamsList(doc.params)} ${buildExamples(doc.examples)}
`[0]; const isValidDoc = (doc) => { const label = getDocLabel(doc); return label && !label.startsWith('_') && !['package'].includes(doc.kind); }; const hasExcludedTags = (doc) => ['superdirtOnly', 'noAutocomplete'].some((tag) => doc.tags?.find((t) => t.originalTitle === tag)); const jsdocCompletions = jsdoc.docs .filter((doc) => isValidDoc(doc) && !hasExcludedTags(doc)) // https://codemirror.net/docs/ref/#autocomplete.Completion .map((doc) => ({ label: getDocLabel(doc), // detail: 'xxx', // An optional short piece of information to show (with a different style) after the label. info: () => Autocomplete({ doc }), type: 'function', // https://codemirror.net/docs/ref/#autocomplete.Completion.type })); export const strudelAutocomplete = (context) => { const word = context.matchBefore(/\w*/); if (word.from === word.to && !context.explicit) return null; return { from: word.from, options: jsdocCompletions, /* options: [ { label: 'match', type: 'keyword' }, { label: 'hello', type: 'variable', info: '(World)' }, { label: 'magic', type: 'text', apply: '⠁⭒*.✩.*⭒⠁', detail: 'macro' }, ], */ }; }; export const isAutoCompletionEnabled = (enabled) => enabled ? [autocompletion({ override: [strudelAutocomplete] })] : [];