mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2024-12-12 09:26:33 +01:00
substitute /break argument
This commit is contained in:
parent
8b64dbc3ee
commit
9226b14c88
@ -232,13 +232,6 @@ export class SlashCommandClosure {
|
|||||||
this.debugController.stepStack[this.debugController.stepStack.length - 1] = true;
|
this.debugController.stepStack[this.debugController.stepStack.length - 1] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (step.value instanceof SlashCommandBreak) {
|
|
||||||
console.log('encountered SlashCommandBreak');
|
|
||||||
if (this.breakController) {
|
|
||||||
this.breakController?.break();
|
|
||||||
this.scope.pipe = step.value.value ?? this.scope.pipe;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else if (!step.done && this.debugController?.testStepping(this)) {
|
} else if (!step.done && this.debugController?.testStepping(this)) {
|
||||||
this.debugController.isSteppingInto = false;
|
this.debugController.isSteppingInto = false;
|
||||||
const hasImmediateClosureInNamedArgs = step.value?.namedArgumentList?.find(it=>it.value instanceof SlashCommandClosure && it.value.executeNow);
|
const hasImmediateClosureInNamedArgs = step.value?.namedArgumentList?.find(it=>it.value instanceof SlashCommandClosure && it.value.executeNow);
|
||||||
@ -249,7 +242,13 @@ export class SlashCommandClosure {
|
|||||||
}
|
}
|
||||||
// resolve args
|
// resolve args
|
||||||
step = await stepper.next();
|
step = await stepper.next();
|
||||||
if (!step.done && this.debugController?.testStepping(this)) {
|
if (step.value instanceof SlashCommandBreak) {
|
||||||
|
console.log('encountered SlashCommandBreak');
|
||||||
|
if (this.breakController) {
|
||||||
|
this.breakController?.break();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (!step.done && this.debugController?.testStepping(this)) {
|
||||||
this.debugController.isSteppingInto = false;
|
this.debugController.isSteppingInto = false;
|
||||||
this.debugController.isStepping = yield { closure:this, executor:step.value };
|
this.debugController.isStepping = yield { closure:this, executor:step.value };
|
||||||
}
|
}
|
||||||
@ -274,18 +273,8 @@ export class SlashCommandClosure {
|
|||||||
this.onProgress?.(done, this.commandCount);
|
this.onProgress?.(done, this.commandCount);
|
||||||
this.debugController?.setExecutor(executor);
|
this.debugController?.setExecutor(executor);
|
||||||
yield executor;
|
yield executor;
|
||||||
if (executor instanceof SlashCommandBreakPoint) {
|
|
||||||
// no execution for breakpoints, just raise counter
|
|
||||||
done++;
|
|
||||||
yield executor;
|
|
||||||
isFirst = false;
|
|
||||||
} else if (executor instanceof SlashCommandBreak) {
|
|
||||||
done += this.executorList.length - this.executorList.indexOf(executor);
|
|
||||||
this.scope.pipe = executor.value ?? this.scope.pipe;
|
|
||||||
yield executor;
|
|
||||||
isFirst = false;
|
|
||||||
} else {
|
|
||||||
/**@type {import('./SlashCommand.js').NamedArguments} */
|
/**@type {import('./SlashCommand.js').NamedArguments} */
|
||||||
|
// @ts-ignore
|
||||||
let args = {
|
let args = {
|
||||||
_scope: this.scope,
|
_scope: this.scope,
|
||||||
_parserFlags: executor.parserFlags,
|
_parserFlags: executor.parserFlags,
|
||||||
@ -293,86 +282,20 @@ export class SlashCommandClosure {
|
|||||||
_debugController: this.debugController,
|
_debugController: this.debugController,
|
||||||
_hasUnnamedArgument: executor.unnamedArgumentList.length > 0,
|
_hasUnnamedArgument: executor.unnamedArgumentList.length > 0,
|
||||||
};
|
};
|
||||||
let value;
|
if (executor instanceof SlashCommandBreakPoint) {
|
||||||
// substitute named arguments
|
// no execution for breakpoints, just raise counter
|
||||||
for (const arg of executor.namedArgumentList) {
|
done++;
|
||||||
if (arg.value instanceof SlashCommandClosure) {
|
yield executor;
|
||||||
/**@type {SlashCommandClosure}*/
|
isFirst = false;
|
||||||
const closure = arg.value;
|
} else if (executor instanceof SlashCommandBreak) {
|
||||||
closure.scope.parent = this.scope;
|
const value = await this.substituteUnnamedArgument(executor, isFirst, args);
|
||||||
closure.breakController = this.breakController;
|
done += this.executorList.length - this.executorList.indexOf(executor);
|
||||||
if (this.debugController && !closure.debugController) {
|
this.scope.pipe = value ?? this.scope.pipe;
|
||||||
closure.debugController = this.debugController;
|
yield executor;
|
||||||
}
|
isFirst = false;
|
||||||
if (closure.executeNow) {
|
|
||||||
args[arg.name] = (await closure.execute())?.pipe;
|
|
||||||
} else {
|
} else {
|
||||||
args[arg.name] = closure;
|
await this.substituteNamedArguments(executor, args);
|
||||||
}
|
let value = await this.substituteUnnamedArgument(executor, isFirst, args);
|
||||||
} else {
|
|
||||||
args[arg.name] = this.substituteParams(arg.value);
|
|
||||||
}
|
|
||||||
// unescape named argument
|
|
||||||
if (typeof args[arg.name] == 'string') {
|
|
||||||
args[arg.name] = args[arg.name]
|
|
||||||
?.replace(/\\\{/g, '{')
|
|
||||||
?.replace(/\\\}/g, '}')
|
|
||||||
;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// substitute unnamed argument
|
|
||||||
if (executor.unnamedArgumentList.length == 0) {
|
|
||||||
if (!isFirst && executor.injectPipe) {
|
|
||||||
value = this.scope.pipe;
|
|
||||||
args._hasUnnamedArgument = this.scope.pipe !== null && this.scope.pipe !== undefined;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
value = [];
|
|
||||||
for (let i = 0; i < executor.unnamedArgumentList.length; i++) {
|
|
||||||
let v = executor.unnamedArgumentList[i].value;
|
|
||||||
if (v instanceof SlashCommandClosure) {
|
|
||||||
/**@type {SlashCommandClosure}*/
|
|
||||||
const closure = v;
|
|
||||||
closure.scope.parent = this.scope;
|
|
||||||
closure.breakController = this.breakController;
|
|
||||||
if (this.debugController && !closure.debugController) {
|
|
||||||
closure.debugController = this.debugController;
|
|
||||||
}
|
|
||||||
if (closure.executeNow) {
|
|
||||||
v = (await closure.execute())?.pipe;
|
|
||||||
} else {
|
|
||||||
v = closure;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
v = this.substituteParams(v);
|
|
||||||
}
|
|
||||||
value[i] = v;
|
|
||||||
}
|
|
||||||
if (!executor.command.splitUnnamedArgument) {
|
|
||||||
if (value.length == 1) {
|
|
||||||
value = value[0];
|
|
||||||
} else if (!value.find(it=>it instanceof SlashCommandClosure)) {
|
|
||||||
value = value.join('');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// unescape unnamed argument
|
|
||||||
if (typeof value == 'string') {
|
|
||||||
value = value
|
|
||||||
?.replace(/\\\{/g, '{')
|
|
||||||
?.replace(/\\\}/g, '}')
|
|
||||||
;
|
|
||||||
} else if (Array.isArray(value)) {
|
|
||||||
value = value.map(v=>{
|
|
||||||
if (typeof v == 'string') {
|
|
||||||
return v
|
|
||||||
?.replace(/\\\{/g, '{')
|
|
||||||
?.replace(/\\\}/g, '}');
|
|
||||||
}
|
|
||||||
return v;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let abortResult = await this.testAbortController();
|
let abortResult = await this.testAbortController();
|
||||||
if (abortResult) {
|
if (abortResult) {
|
||||||
@ -427,6 +350,102 @@ export class SlashCommandClosure {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {SlashCommandExecutor} executor
|
||||||
|
* @param {import('./SlashCommand.js').NamedArguments} args
|
||||||
|
*/
|
||||||
|
async substituteNamedArguments(executor, args) {
|
||||||
|
// substitute named arguments
|
||||||
|
for (const arg of executor.namedArgumentList) {
|
||||||
|
if (arg.value instanceof SlashCommandClosure) {
|
||||||
|
/**@type {SlashCommandClosure}*/
|
||||||
|
const closure = arg.value;
|
||||||
|
closure.scope.parent = this.scope;
|
||||||
|
closure.breakController = this.breakController;
|
||||||
|
if (this.debugController && !closure.debugController) {
|
||||||
|
closure.debugController = this.debugController;
|
||||||
|
}
|
||||||
|
if (closure.executeNow) {
|
||||||
|
args[arg.name] = (await closure.execute())?.pipe;
|
||||||
|
} else {
|
||||||
|
args[arg.name] = closure;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args[arg.name] = this.substituteParams(arg.value);
|
||||||
|
}
|
||||||
|
// unescape named argument
|
||||||
|
if (typeof args[arg.name] == 'string') {
|
||||||
|
args[arg.name] = args[arg.name]
|
||||||
|
?.replace(/\\\{/g, '{')
|
||||||
|
?.replace(/\\\}/g, '}')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {SlashCommandExecutor} executor
|
||||||
|
* @param {boolean} isFirst
|
||||||
|
* @param {import('./SlashCommand.js').NamedArguments} args
|
||||||
|
* @returns {Promise<string|SlashCommandClosure|(string|SlashCommandClosure)[]>}
|
||||||
|
*/
|
||||||
|
async substituteUnnamedArgument(executor, isFirst, args) {
|
||||||
|
let value;
|
||||||
|
// substitute unnamed argument
|
||||||
|
if (executor.unnamedArgumentList.length == 0) {
|
||||||
|
if (!isFirst && executor.injectPipe) {
|
||||||
|
value = this.scope.pipe;
|
||||||
|
args._hasUnnamedArgument = this.scope.pipe !== null && this.scope.pipe !== undefined;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
value = [];
|
||||||
|
for (let i = 0; i < executor.unnamedArgumentList.length; i++) {
|
||||||
|
let v = executor.unnamedArgumentList[i].value;
|
||||||
|
if (v instanceof SlashCommandClosure) {
|
||||||
|
/**@type {SlashCommandClosure}*/
|
||||||
|
const closure = v;
|
||||||
|
closure.scope.parent = this.scope;
|
||||||
|
closure.breakController = this.breakController;
|
||||||
|
if (this.debugController && !closure.debugController) {
|
||||||
|
closure.debugController = this.debugController;
|
||||||
|
}
|
||||||
|
if (closure.executeNow) {
|
||||||
|
v = (await closure.execute())?.pipe;
|
||||||
|
} else {
|
||||||
|
v = closure;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
v = this.substituteParams(v);
|
||||||
|
}
|
||||||
|
value[i] = v;
|
||||||
|
}
|
||||||
|
if (!executor.command.splitUnnamedArgument) {
|
||||||
|
if (value.length == 1) {
|
||||||
|
value = value[0];
|
||||||
|
} else if (!value.find(it=>it instanceof SlashCommandClosure)) {
|
||||||
|
value = value.join('');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// unescape unnamed argument
|
||||||
|
if (typeof value == 'string') {
|
||||||
|
value = value
|
||||||
|
?.replace(/\\\{/g, '{')
|
||||||
|
?.replace(/\\\}/g, '}')
|
||||||
|
;
|
||||||
|
} else if (Array.isArray(value)) {
|
||||||
|
value = value.map(v=>{
|
||||||
|
if (typeof v == 'string') {
|
||||||
|
return v
|
||||||
|
?.replace(/\\\{/g, '{')
|
||||||
|
?.replace(/\\\}/g, '}');
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Auto-fixes the pipe if it is not a valid result for STscript.
|
* Auto-fixes the pipe if it is not a valid result for STscript.
|
||||||
* @param {SlashCommand} command Command being executed
|
* @param {SlashCommand} command Command being executed
|
||||||
|
Loading…
Reference in New Issue
Block a user