Add enumerator for MacrosParser

1dccb08cd66bd3f212c04c7509a031beca826ab2

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +34 -1Showing whitespace changes
public/scripts/macros.js+30 -1
@@ -26,6 +26,12 @@ Handlebars.registerHelper('helperMissing', function () {
2626 * @typedef {(nonce: string) => string} MacroFunction
2727 */
2828
29+/**
30+ * @typedef {Object} CustomMacro
31+ * @property {string} key - Macro name (key)
32+ * @property {string} description - Optional description of the macro
33+ */
34+
2935export class MacrosParser {
3036 /**
3137 * A map of registered macros.
@@ -34,11 +40,28 @@ export class MacrosParser {
3440 static #macros = new Map();
3541
3642 /**
43+ * A map of macro descriptions.
44+ * @type {Map<string, string>}
45+ */
46+ static #descriptions = new Map();
47+
48+ /**
49+ * Returns an iterator over all registered macros.
50+ * @returns {IterableIterator<CustomMacro>}
51+ */
52+ static [Symbol.iterator] = function* () {
53+ for (const macro of this.#macros.values()) {
54+ yield { key: macro, description: this.#descriptions.get(macro) };
55+ }
56+ };
57+
58+ /**
3759 * Registers a global macro that can be used anywhere where substitution is allowed.
3860 * @param {string} key Macro name (key)
3961 * @param {string|MacroFunction} value A string or a function that returns a string
62+ * @param {string} [description] Optional description of the macro
4063 */
4164 static registerMacro(key, value, description = '') {
4265 if (typeof key !== 'string') {
4366 throw new Error('Macro key must be a string');
4467 }
@@ -64,6 +87,10 @@ export class MacrosParser {
6487 }
6588
6689 this.#macros.set(key, value);
90+
91+ if (typeof description === 'string' && description) {
92+ this.#descriptions.set(key, description);
93+ }
6794 }
6895
6996 /**
@@ -88,6 +115,8 @@ export class MacrosParser {
88115 if (!deleted) {
89116 console.warn(`Macro ${key} was not registered`);
90117 }
118+
119+ this.#descriptions.delete(key);
91120 }
92121
93122 /**
public/scripts/slash-commands/SlashCommandParser.js+4 -0
@@ -22,6 +22,7 @@ import { SlashCommandBreakPoint } from './SlashCommandBreakPoint.js';
2222import { SlashCommandDebugController } from './SlashCommandDebugController.js';
2323import { commonEnumProviders } from './SlashCommandCommonEnumsProvider.js';
2424import { SlashCommandBreak } from './SlashCommandBreak.js';
25+import { MacrosParser } from '../macros.js';
2526
2627/** @typedef {import('./SlashCommand.js').NamedArgumentsCapture} NamedArgumentsCapture */
2728/** @typedef {import('./SlashCommand.js').NamedArguments} NamedArguments */
@@ -494,6 +495,9 @@ export class SlashCommandParser {
494495 li.querySelector('tt').textContent,
495496 (li.querySelector('tt').remove(),li.innerHTML),
496497 ));
498+ for (const macro of MacrosParser) {
499+ options.push(new MacroAutoCompleteOption(macro.name, `{{${macro.name}}}`, macro.description || 'No description provided'));
500+ }
497501 const result = new AutoCompleteNameResult(
498502 macro.name,
499503 macro.start + 2,