Add enumerator for MacrosParser

This commit is contained in:
Cohee
2025-01-11 19:04:36 +02:00
parent f462436450
commit 1dccb08cd6
2 changed files with 34 additions and 1 deletions

View File

@ -26,6 +26,12 @@ Handlebars.registerHelper('helperMissing', function () {
* @typedef {(nonce: string) => string} MacroFunction
*/
/**
* @typedef {Object} CustomMacro
* @property {string} key - Macro name (key)
* @property {string} description - Optional description of the macro
*/
export class MacrosParser {
/**
* A map of registered macros.
@ -33,12 +39,29 @@ export class MacrosParser {
*/
static #macros = new Map();
/**
* A map of macro descriptions.
* @type {Map<string, string>}
*/
static #descriptions = new Map();
/**
* Returns an iterator over all registered macros.
* @returns {IterableIterator<CustomMacro>}
*/
static [Symbol.iterator] = function* () {
for (const macro of this.#macros.values()) {
yield { key: macro, description: this.#descriptions.get(macro) };
}
};
/**
* Registers a global macro that can be used anywhere where substitution is allowed.
* @param {string} key Macro name (key)
* @param {string|MacroFunction} value A string or a function that returns a string
* @param {string} [description] Optional description of the macro
*/
static registerMacro(key, value) {
static registerMacro(key, value, description = '') {
if (typeof key !== 'string') {
throw new Error('Macro key must be a string');
}
@ -64,6 +87,10 @@ export class MacrosParser {
}
this.#macros.set(key, value);
if (typeof description === 'string' && description) {
this.#descriptions.set(key, description);
}
}
/**
@ -88,6 +115,8 @@ export class MacrosParser {
if (!deleted) {
console.warn(`Macro ${key} was not registered`);
}
this.#descriptions.delete(key);
}
/**

View File

@ -22,6 +22,7 @@ import { SlashCommandBreakPoint } from './SlashCommandBreakPoint.js';
import { SlashCommandDebugController } from './SlashCommandDebugController.js';
import { commonEnumProviders } from './SlashCommandCommonEnumsProvider.js';
import { SlashCommandBreak } from './SlashCommandBreak.js';
import { MacrosParser } from '../macros.js';
/** @typedef {import('./SlashCommand.js').NamedArgumentsCapture} NamedArgumentsCapture */
/** @typedef {import('./SlashCommand.js').NamedArguments} NamedArguments */
@ -494,6 +495,9 @@ export class SlashCommandParser {
li.querySelector('tt').textContent,
(li.querySelector('tt').remove(),li.innerHTML),
));
for (const macro of MacrosParser) {
options.push(new MacroAutoCompleteOption(macro.name, `{{${macro.name}}}`, macro.description || 'No description provided'));
}
const result = new AutoCompleteNameResult(
macro.name,
macro.start + 2,