Add synonyms to autocomplete
This commit is contained in:
parent
c199b51645
commit
709b654ed3
5 changed files with 42 additions and 15 deletions
|
|
@ -54,11 +54,12 @@ const buildExamples = (examples) =>
|
||||||
`
|
`
|
||||||
: '';
|
: '';
|
||||||
|
|
||||||
export const Autocomplete = ({ doc, label }) =>
|
export const Autocomplete = (doc) =>
|
||||||
h`
|
h`
|
||||||
<div class="autocomplete-info-container">
|
<div class="autocomplete-info-container">
|
||||||
<div class="autocomplete-info-tooltip">
|
<div class="autocomplete-info-tooltip">
|
||||||
<h3 class="autocomplete-info-function-name">${label || getDocLabel(doc)}</h3>
|
<h3 class="autocomplete-info-function-name">${getDocLabel(doc)}</h3>
|
||||||
|
${doc.synonyms_text ? `<div class="autocomplete-info-function-synonyms">Synonyms: ${doc.synonyms_text}</div>` : ''}
|
||||||
${doc.description ? `<div class="autocomplete-info-function-description">${doc.description}</div>` : ''}
|
${doc.description ? `<div class="autocomplete-info-function-description">${doc.description}</div>` : ''}
|
||||||
${buildParamsList(doc.params)}
|
${buildParamsList(doc.params)}
|
||||||
${buildExamples(doc.examples)}
|
${buildExamples(doc.examples)}
|
||||||
|
|
@ -74,19 +75,36 @@ const isValidDoc = (doc) => {
|
||||||
const hasExcludedTags = (doc) =>
|
const hasExcludedTags = (doc) =>
|
||||||
['superdirtOnly', 'noAutocomplete'].some((tag) => doc.tags?.find((t) => t.originalTitle === tag));
|
['superdirtOnly', 'noAutocomplete'].some((tag) => doc.tags?.find((t) => t.originalTitle === tag));
|
||||||
|
|
||||||
|
export const getSynonymDoc = (doc, synonym) => {
|
||||||
|
const synonyms = doc.synonyms || [];
|
||||||
|
const docLabel = getDocLabel(doc);
|
||||||
|
// Swap `doc.name` in for `s` in the list of synonyms
|
||||||
|
const synonymsWithDoc = [docLabel, ...synonyms].filter((x) => x && x !== synonym);
|
||||||
|
return {
|
||||||
|
...doc,
|
||||||
|
name: synonym,
|
||||||
|
longname: synonym,
|
||||||
|
synonyms: synonymsWithDoc,
|
||||||
|
synonyms_text: synonymsWithDoc.join(', '),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const jsdocCompletions = (() => {
|
const jsdocCompletions = (() => {
|
||||||
const seen = new Set(); // avoid repetition
|
const seen = new Set(); // avoid repetition
|
||||||
const completions = [];
|
const completions = [];
|
||||||
for (const doc of jsdoc.docs) {
|
for (const doc of jsdoc.docs) {
|
||||||
if (!isValidDoc(doc) || hasExcludedTags(doc)) continue;
|
if (!isValidDoc(doc) || hasExcludedTags(doc)) continue;
|
||||||
let labels = [getDocLabel(doc), ...(doc.synonyms || [])];
|
const docLabel = getDocLabel(doc);
|
||||||
|
// Remove duplicates
|
||||||
|
const synonyms = doc.synonyms || [];
|
||||||
|
let labels = [docLabel, ...synonyms];
|
||||||
for (const label of labels) {
|
for (const label of labels) {
|
||||||
// https://codemirror.net/docs/ref/#autocomplete.Completion
|
// https://codemirror.net/docs/ref/#autocomplete.Completion
|
||||||
if (label && !seen.has(label)) {
|
if (label && !seen.has(label)) {
|
||||||
seen.add(label);
|
seen.add(label);
|
||||||
completions.push({
|
completions.push({
|
||||||
label,
|
label,
|
||||||
info: () => Autocomplete({ doc, label }),
|
info: () => Autocomplete(getSynonymDoc(doc, label)),
|
||||||
type: 'function', // https://codemirror.net/docs/ref/#autocomplete.Completion.type
|
type: 'function', // https://codemirror.net/docs/ref/#autocomplete.Completion.type
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { hoverTooltip } from '@codemirror/view';
|
import { hoverTooltip } from '@codemirror/view';
|
||||||
import jsdoc from '../../doc.json';
|
import jsdoc from '../../doc.json';
|
||||||
import { Autocomplete } from './autocomplete.mjs';
|
import { Autocomplete, getSynonymDoc } from './autocomplete.mjs';
|
||||||
|
|
||||||
const getDocLabel = (doc) => doc.name || doc.longname;
|
const getDocLabel = (doc) => doc.name || doc.longname;
|
||||||
|
|
||||||
|
|
@ -52,10 +52,11 @@ export const strudelTooltip = hoverTooltip(
|
||||||
let entry = jsdoc.docs.filter((doc) => getDocLabel(doc) === word)[0];
|
let entry = jsdoc.docs.filter((doc) => getDocLabel(doc) === word)[0];
|
||||||
if (!entry) {
|
if (!entry) {
|
||||||
// Try for synonyms
|
// Try for synonyms
|
||||||
entry = jsdoc.docs.filter((doc) => doc.synonyms && doc.synonyms.includes(word))[0];
|
const doc = jsdoc.docs.filter((doc) => doc.synonyms && doc.synonyms.includes(word))[0];
|
||||||
if (!entry) {
|
if (!doc) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
entry = getSynonymDoc(doc, word);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -66,7 +67,7 @@ export const strudelTooltip = hoverTooltip(
|
||||||
create(view) {
|
create(view) {
|
||||||
let dom = document.createElement('div');
|
let dom = document.createElement('div');
|
||||||
dom.className = 'strudel-tooltip';
|
dom.className = 'strudel-tooltip';
|
||||||
const ac = Autocomplete({ doc: entry, label: word });
|
const ac = Autocomplete(entry);
|
||||||
dom.appendChild(ac);
|
dom.appendChild(ac);
|
||||||
return { dom };
|
return { dom };
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1246,7 +1246,8 @@ export const silence = gap(1);
|
||||||
/* Like silence, but with a 'steps' (relative duration) of 0 */
|
/* Like silence, but with a 'steps' (relative duration) of 0 */
|
||||||
export const nothing = gap(0);
|
export const nothing = gap(0);
|
||||||
|
|
||||||
/** A discrete value that repeats once per cycle.
|
/**
|
||||||
|
* A discrete value that repeats once per cycle.
|
||||||
*
|
*
|
||||||
* @returns {Pattern}
|
* @returns {Pattern}
|
||||||
* @example
|
* @example
|
||||||
|
|
@ -1299,7 +1300,8 @@ export function sequenceP(pats) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The given items are played at the same time at the same length.
|
/**
|
||||||
|
* The given items are played at the same time at the same length.
|
||||||
*
|
*
|
||||||
* @return {Pattern}
|
* @return {Pattern}
|
||||||
* @synonyms polyrhythm, pr
|
* @synonyms polyrhythm, pr
|
||||||
|
|
@ -1382,11 +1384,11 @@ export function stackBy(by, ...pats) {
|
||||||
.setSteps(steps);
|
.setSteps(steps);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Concatenation: combines a list of patterns, switching between them successively, one per cycle:
|
/**
|
||||||
*
|
* Concatenation: combines a list of patterns, switching between them successively, one per cycle.
|
||||||
* synonyms: `cat`
|
|
||||||
*
|
*
|
||||||
* @return {Pattern}
|
* @return {Pattern}
|
||||||
|
* @synonyms cat
|
||||||
* @example
|
* @example
|
||||||
* slowcat("e5", "b4", ["d5", "c5"])
|
* slowcat("e5", "b4", ["d5", "c5"])
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -112,6 +112,13 @@
|
||||||
margin: 0 0 8px 0;
|
margin: 0 0 8px 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.autocomplete-info-function-synonyms {
|
||||||
|
margin: 0 0 12px 0;
|
||||||
|
color: var(--foreground);
|
||||||
|
line-height: 1.5;
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
|
||||||
.autocomplete-info-function-description {
|
.autocomplete-info-function-description {
|
||||||
margin: 0 0 12px 0;
|
margin: 0 0 12px 0;
|
||||||
color: var(--foreground);
|
color: var(--foreground);
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,7 @@ const availableFunctions = (() => {
|
||||||
if (!s || seen.has(s)) continue;
|
if (!s || seen.has(s)) continue;
|
||||||
seen.add(s);
|
seen.add(s);
|
||||||
// Swap `doc.name` in for `s` in the list of synonyms
|
// Swap `doc.name` in for `s` in the list of synonyms
|
||||||
const notS = synonyms.filter((x) => x && x !== s);
|
const synonymsWithDoc = [doc.name, ...synonyms].filter((x) => x && x !== s);
|
||||||
const synonymsWithDoc = Array.from(new Set([doc.name, ...notS]));
|
|
||||||
functions.push({
|
functions.push({
|
||||||
...doc,
|
...doc,
|
||||||
name: s, // update names for the synonym
|
name: s, // update names for the synonym
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue