mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add enumerator for MacrosParser
This commit is contained in:
@ -26,6 +26,12 @@ Handlebars.registerHelper('helperMissing', function () {
|
|||||||
* @typedef {(nonce: string) => string} MacroFunction
|
* @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 {
|
export class MacrosParser {
|
||||||
/**
|
/**
|
||||||
* A map of registered macros.
|
* A map of registered macros.
|
||||||
@ -33,12 +39,29 @@ export class MacrosParser {
|
|||||||
*/
|
*/
|
||||||
static #macros = new Map();
|
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.
|
* Registers a global macro that can be used anywhere where substitution is allowed.
|
||||||
* @param {string} key Macro name (key)
|
* @param {string} key Macro name (key)
|
||||||
* @param {string|MacroFunction} value A string or a function that returns a string
|
* @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') {
|
if (typeof key !== 'string') {
|
||||||
throw new Error('Macro key must be a string');
|
throw new Error('Macro key must be a string');
|
||||||
}
|
}
|
||||||
@ -64,6 +87,10 @@ export class MacrosParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.#macros.set(key, value);
|
this.#macros.set(key, value);
|
||||||
|
|
||||||
|
if (typeof description === 'string' && description) {
|
||||||
|
this.#descriptions.set(key, description);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -88,6 +115,8 @@ export class MacrosParser {
|
|||||||
if (!deleted) {
|
if (!deleted) {
|
||||||
console.warn(`Macro ${key} was not registered`);
|
console.warn(`Macro ${key} was not registered`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.#descriptions.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,6 +22,7 @@ import { SlashCommandBreakPoint } from './SlashCommandBreakPoint.js';
|
|||||||
import { SlashCommandDebugController } from './SlashCommandDebugController.js';
|
import { SlashCommandDebugController } from './SlashCommandDebugController.js';
|
||||||
import { commonEnumProviders } from './SlashCommandCommonEnumsProvider.js';
|
import { commonEnumProviders } from './SlashCommandCommonEnumsProvider.js';
|
||||||
import { SlashCommandBreak } from './SlashCommandBreak.js';
|
import { SlashCommandBreak } from './SlashCommandBreak.js';
|
||||||
|
import { MacrosParser } from '../macros.js';
|
||||||
|
|
||||||
/** @typedef {import('./SlashCommand.js').NamedArgumentsCapture} NamedArgumentsCapture */
|
/** @typedef {import('./SlashCommand.js').NamedArgumentsCapture} NamedArgumentsCapture */
|
||||||
/** @typedef {import('./SlashCommand.js').NamedArguments} NamedArguments */
|
/** @typedef {import('./SlashCommand.js').NamedArguments} NamedArguments */
|
||||||
@ -494,6 +495,9 @@ export class SlashCommandParser {
|
|||||||
li.querySelector('tt').textContent,
|
li.querySelector('tt').textContent,
|
||||||
(li.querySelector('tt').remove(),li.innerHTML),
|
(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(
|
const result = new AutoCompleteNameResult(
|
||||||
macro.name,
|
macro.name,
|
||||||
macro.start + 2,
|
macro.start + 2,
|
||||||
|
Reference in New Issue
Block a user