* Refactor: Move MacroBrowser.js to macros/engine directory and update import paths
* Replace macro name with alias in displayOverride for macro browser
- Import escapeRegex utility for safe regex pattern creation
- Check if macro has aliasOf property when displayOverride is present
- Replace all occurrences of the original macro name with the alias name in displayOverride text
- Use word boundary regex pattern to match macro names in {{macro}}, {{macro::}}, and {{macro }} contexts
* Add whitespace padding preservation for macro closing tag autocomplete
- Extract paddingBefore and paddingAfter from opening macro tags in MacroCstWalker
- Pass padding info through unclosed scope detection in findUnclosedScopes() and findUnclosedScopesRegex()
- Update MacroClosingTagAutoCompleteOption constructor to accept padding options
- Apply same whitespace padding to closing tags as their corresponding opening tags
- Update regex fallback to capture and preserve whitespace from opening tags
* Add replacementStartOffset to AutoCompleteOption for whitespace normalization in closing tags
- Add replacementStartOffset property to AutoCompleteOption class with JSDoc documentation
- Update AutoComplete.select() to apply per-option replacement offset when calculating effectiveStart
- Calculate replacementStartOffset in MacroClosingTagAutoCompleteOption based on currentPadding length
- Pass currentPadding from context.paddingBefore to MacroClosingTagAutoCompleteOption in SlashCommandParser
* Fix closing tag detection regex to correctly autocomplete // macro
* Add flags and fullText to EnhancedMacroAutoCompleteOption context and fix {{//}} autocomplete offset
- Add flags, currentFlag, and fullText properties to EnhancedMacroAutoCompleteOptions JSDoc
- Store options object as #options field in EnhancedMacroAutoCompleteOption for later access
- Pass flags, currentFlag, and fullText from context to macroContext in SlashCommandParser
- Fix {{//}} macro autocomplete to adjust replacementStartOffset when typed after single slash to avoid treating it as flag
* Add unnamedArgs definition to {{//}} comment macro for better documentation
---------
Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
239 lines
10 KiB
JavaScript
239 lines
10 KiB
JavaScript
import { AutoCompleteFuzzyScore } from './AutoCompleteFuzzyScore.js';
|
|
|
|
|
|
|
|
export class AutoCompleteOption {
|
|
/** @type {string} */ name;
|
|
/** @type {string} */ typeIcon;
|
|
/** @type {string} */ type;
|
|
/** @type {number} */ nameOffset = 0;
|
|
/** @type {AutoCompleteFuzzyScore} */ score;
|
|
/** @type {string} */ replacer;
|
|
/** @type {HTMLElement} */ dom;
|
|
/** @type {(input:string)=>boolean} */ matchProvider;
|
|
/** @type {(input:string)=>string} */ valueProvider;
|
|
/** @type {boolean} */ makeSelectable = false;
|
|
|
|
/**
|
|
* Offset to adjust the replacement start position.
|
|
* Negative values start replacement earlier (e.g., -2 to include 2 chars before normal start).
|
|
* Used by closing tag autocomplete to replace leading whitespace.
|
|
* @type {number}
|
|
*/
|
|
replacementStartOffset = 0;
|
|
|
|
/**
|
|
* Priority for sorting. Lower values = higher priority (sorted first).
|
|
* Default is 100 (normal priority). Use lower values for items that should appear at the top.
|
|
* @type {number}
|
|
*/
|
|
sortPriority = 100;
|
|
|
|
|
|
/**
|
|
* Used as a comparison value when removing duplicates (e.g., when a SlashCommand has aliases).
|
|
* @type {any}
|
|
* */
|
|
get value() {
|
|
return this.name;
|
|
}
|
|
|
|
get isSelectable() {
|
|
return this.makeSelectable || !this.valueProvider;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param {string} name
|
|
*/
|
|
constructor(name, typeIcon = ' ', type = '', matchProvider = null, valueProvider = null, makeSelectable = false) {
|
|
this.name = name;
|
|
this.typeIcon = typeIcon;
|
|
this.type = type;
|
|
this.matchProvider = matchProvider;
|
|
this.valueProvider = valueProvider;
|
|
this.makeSelectable = makeSelectable;
|
|
}
|
|
|
|
|
|
makeItem(key, typeIcon, noSlash, namedArguments = [], unnamedArguments = [], returnType = 'void', helpString = '', aliasList = []) {
|
|
const li = document.createElement('li'); {
|
|
li.classList.add('item');
|
|
const type = document.createElement('span'); {
|
|
type.classList.add('type');
|
|
type.classList.add('monospace');
|
|
type.textContent = typeIcon;
|
|
li.append(type);
|
|
}
|
|
const specs = document.createElement('span'); {
|
|
specs.classList.add('specs');
|
|
const name = document.createElement('span'); {
|
|
name.classList.add('name');
|
|
name.classList.add('monospace');
|
|
name.textContent = noSlash ? '' : '/';
|
|
key.split('').forEach(char=>{
|
|
const span = document.createElement('span'); {
|
|
span.textContent = char;
|
|
name.append(span);
|
|
}
|
|
});
|
|
specs.append(name);
|
|
}
|
|
const body = document.createElement('span'); {
|
|
body.classList.add('body');
|
|
const args = document.createElement('span'); {
|
|
args.classList.add('arguments');
|
|
for (const arg of namedArguments) {
|
|
const argItem = document.createElement('span'); {
|
|
argItem.classList.add('argument');
|
|
argItem.classList.add('namedArgument');
|
|
if (!arg.isRequired || (arg.defaultValue ?? false)) argItem.classList.add('optional');
|
|
if (arg.acceptsMultiple) argItem.classList.add('multiple');
|
|
const name = document.createElement('span'); {
|
|
name.classList.add('argument-name');
|
|
name.textContent = arg.name;
|
|
argItem.append(name);
|
|
}
|
|
if (arg.enumList.length > 0) {
|
|
const enums = document.createElement('span'); {
|
|
enums.classList.add('argument-enums');
|
|
for (const e of arg.enumList) {
|
|
const enumItem = document.createElement('span'); {
|
|
enumItem.classList.add('argument-enum');
|
|
enumItem.textContent = e;
|
|
enums.append(enumItem);
|
|
}
|
|
}
|
|
argItem.append(enums);
|
|
}
|
|
} else {
|
|
const types = document.createElement('span'); {
|
|
types.classList.add('argument-types');
|
|
for (const t of arg.typeList) {
|
|
const type = document.createElement('span'); {
|
|
type.classList.add('argument-type');
|
|
type.textContent = t;
|
|
types.append(type);
|
|
}
|
|
}
|
|
argItem.append(types);
|
|
}
|
|
}
|
|
args.append(argItem);
|
|
}
|
|
}
|
|
for (const arg of unnamedArguments) {
|
|
const argItem = document.createElement('span'); {
|
|
argItem.classList.add('argument');
|
|
argItem.classList.add('unnamedArgument');
|
|
if (!arg.isRequired || (arg.defaultValue ?? false)) argItem.classList.add('optional');
|
|
if (arg.acceptsMultiple) argItem.classList.add('multiple');
|
|
if (arg.enumList.length > 0) {
|
|
const enums = document.createElement('span'); {
|
|
enums.classList.add('argument-enums');
|
|
for (const e of arg.enumList) {
|
|
const enumItem = document.createElement('span'); {
|
|
enumItem.classList.add('argument-enum');
|
|
enumItem.textContent = e;
|
|
enums.append(enumItem);
|
|
}
|
|
}
|
|
argItem.append(enums);
|
|
}
|
|
} else {
|
|
const types = document.createElement('span'); {
|
|
types.classList.add('argument-types');
|
|
for (const t of arg.typeList) {
|
|
const type = document.createElement('span'); {
|
|
type.classList.add('argument-type');
|
|
type.textContent = t;
|
|
types.append(type);
|
|
}
|
|
}
|
|
argItem.append(types);
|
|
}
|
|
}
|
|
args.append(argItem);
|
|
}
|
|
}
|
|
body.append(args);
|
|
}
|
|
const returns = document.createElement('span'); {
|
|
returns.classList.add('returns');
|
|
returns.textContent = returnType ?? 'void';
|
|
// body.append(returns);
|
|
}
|
|
specs.append(body);
|
|
}
|
|
li.append(specs);
|
|
}
|
|
const stopgap = document.createElement('span'); {
|
|
stopgap.classList.add('stopgap');
|
|
stopgap.textContent = '';
|
|
li.append(stopgap);
|
|
}
|
|
const help = document.createElement('span'); {
|
|
help.classList.add('help');
|
|
const content = document.createElement('span'); {
|
|
content.classList.add('helpContent');
|
|
content.innerHTML = helpString;
|
|
const text = content.textContent;
|
|
content.innerHTML = '';
|
|
content.textContent = text;
|
|
help.append(content);
|
|
}
|
|
li.append(help);
|
|
}
|
|
if (aliasList.length > 0) {
|
|
const aliases = document.createElement('span'); {
|
|
aliases.classList.add('aliases');
|
|
aliases.append(' (alias: ');
|
|
for (const aliasName of aliasList) {
|
|
const alias = document.createElement('span'); {
|
|
alias.classList.add('monospace');
|
|
alias.textContent = `/${aliasName}`;
|
|
aliases.append(alias);
|
|
}
|
|
}
|
|
aliases.append(')');
|
|
// li.append(aliases);
|
|
}
|
|
}
|
|
}
|
|
return li;
|
|
}
|
|
|
|
|
|
/**
|
|
* @returns {HTMLElement}
|
|
*/
|
|
renderItem() {
|
|
// throw new Error(`${this.constructor.name}.renderItem() is not implemented`);
|
|
let li;
|
|
li = this.makeItem(this.name, this.typeIcon, true);
|
|
li.setAttribute('data-name', this.name);
|
|
li.setAttribute('data-option-type', this.type);
|
|
return li;
|
|
}
|
|
|
|
|
|
/**
|
|
* @returns {DocumentFragment}
|
|
*/
|
|
renderDetails() {
|
|
// throw new Error(`${this.constructor.name}.renderDetails() is not implemented`);
|
|
const frag = document.createDocumentFragment();
|
|
const specs = document.createElement('div'); {
|
|
specs.classList.add('specs');
|
|
const name = document.createElement('div'); {
|
|
name.classList.add('name');
|
|
name.classList.add('monospace');
|
|
name.textContent = this.name;
|
|
specs.append(name);
|
|
}
|
|
frag.append(specs);
|
|
}
|
|
return frag;
|
|
}
|
|
}
|