left4me/l4d2web/scripts/editor-src/autocomplete.js
mwiegand ca6a7aa74c
refactor(editor): use shared rankVocab in autocomplete
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-17 17:37:54 +02:00

28 lines
860 B
JavaScript

import { autocompletion } from "@codemirror/autocomplete";
import { rankVocab } from "./vocab-rank.js";
const WORD_RE = /[A-Za-z0-9_]{2,}/;
export function vocabCompletions(vocab) {
// vocab: { cvars: [{name, desc?}, …], commands: [{name, desc?}, …] }
return (context) => {
const word = context.matchBefore(WORD_RE);
if (!word || (word.from === word.to && !context.explicit)) return null;
const ranked = rankVocab(word.text, vocab);
const options = ranked.map(e => ({
label: e.name,
info: e.desc || e.kind,
type: e.kind === "command" ? "function" : "variable",
}));
return { from: word.from, options, validFor: WORD_RE };
};
}
export function autocompleteExtension(vocab) {
return autocompletion({
override: [vocabCompletions(vocab)],
activateOnTyping: true,
maxRenderedOptions: 8,
});
}