Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/IChoiceExecutor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type IChoice from "./types/choices/IChoice";
import type { MacroAbortError } from "./errors/MacroAbortError";
import type { FormatterEvaluatorContext } from "./formatters/formatterEvaluators";

export interface IChoiceExecutor {
execute(choice: IChoice): Promise<void>;
Expand All @@ -15,4 +16,12 @@ export interface IChoiceExecutor {
* awaiting {@link execute} to determine whether the child choice stopped early.
*/
consumeAbortSignal?(): MacroAbortError | null;
evaluateMacroToken?(
macroName: string,
context: FormatterEvaluatorContext,
): Promise<unknown>;
evaluateInlineJavaScriptToken?(
code: string,
context: FormatterEvaluatorContext,
): Promise<unknown>;
}
33 changes: 33 additions & 0 deletions src/choiceExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import type IMacroChoice from "./types/choices/IMacroChoice";
import { TemplateChoiceEngine } from "./engine/TemplateChoiceEngine";
import { CaptureChoiceEngine } from "./engine/CaptureChoiceEngine";
import { MacroChoiceEngine } from "./engine/MacroChoiceEngine";
import { SingleInlineScriptEngine } from "./engine/SingleInlineScriptEngine";
import { SingleMacroEngine } from "./engine/SingleMacroEngine";
import type { IChoiceExecutor } from "./IChoiceExecutor";
import type { FormatterEvaluatorContext } from "./formatters/formatterEvaluators";
import type IMultiChoice from "./types/choices/IMultiChoice";
import ChoiceSuggester from "./gui/suggesters/choiceSuggester";
import { settingsStore } from "./settingsStore";
Expand All @@ -32,6 +35,36 @@ export class ChoiceExecutor implements IChoiceExecutor {
return abort ?? null;
}

async evaluateMacroToken(
macroName: string,
context: FormatterEvaluatorContext,
): Promise<unknown> {
const engine = new SingleMacroEngine(
this.app,
this.plugin,
this.plugin.settings.choices,
this,
context.variables,
);
return await engine.runAndGetOutput(
macroName,
context.label ? { label: context.label } : undefined,
);
}

async evaluateInlineJavaScriptToken(
code: string,
context: FormatterEvaluatorContext,
): Promise<unknown> {
const executor = new SingleInlineScriptEngine(
this.app,
this.plugin,
this,
context.variables,
);
return await executor.runAndGetOutput(code);
}

async execute(choice: IChoice): Promise<void> {
this.pendingAbort = null;
const originLeaf = getOpenFileOriginLeaf(this.app);
Expand Down
38 changes: 22 additions & 16 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ import {
QA_INTERNAL_CAPTURE_TARGET_FILE_PATH,
VALUE_SYNTAX,
} from "../constants";
import { CaptureChoiceFormatter } from "../formatters/captureChoiceFormatter";
import type { CaptureChoiceFormatter } from "../formatters/captureChoiceFormatter";
import { log } from "../logger/logManager";
import type QuickAdd from "../main";
import { FormatterFactory } from "../services/FormatterFactory";
import {
TemplateEvaluator,
TemplateFileService,
} from "../services/TemplateFileService";
import type ICaptureChoice from "../types/choices/ICaptureChoice";
import { normalizeAppendLinkOptions, type AppendLinkOptions } from "../types/linkPlacement";
import {
Expand All @@ -43,7 +48,6 @@ import { basenameWithoutMdOrCanvas } from "../utils/pathUtils";
import { QuickAddChoiceEngine } from "./QuickAddChoiceEngine";
import { ChoiceAbortError } from "../errors/ChoiceAbortError";
import { MacroAbortError } from "../errors/MacroAbortError";
import { SingleTemplateEngine } from "./SingleTemplateEngine";
import { getCaptureAction, type CaptureAction } from "./captureAction";
import {
getCanvasTextCaptureContent,
Expand All @@ -61,6 +65,7 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
choice: ICaptureChoice;
private formatter: CaptureChoiceFormatter;
private readonly plugin: QuickAdd;
private readonly templateFileService: TemplateFileService;
private templatePropertyVars?: Map<string, unknown>;
private capturePropertyVars: Map<string, unknown> = new Map();

Expand All @@ -74,7 +79,11 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
super(app);
this.choice = choice;
this.plugin = plugin;
this.formatter = new CaptureChoiceFormatter(app, plugin, choiceExecutor);
this.formatter = new FormatterFactory(
app,
plugin,
).createCaptureChoiceFormatter(choiceExecutor);
this.templateFileService = new TemplateFileService(app);
}

private showSuccessNotice(
Expand Down Expand Up @@ -709,22 +718,19 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {

let fileContent = "";
if (this.choice.createFileIfItDoesntExist.createWithTemplate) {
const singleTemplateEngine: SingleTemplateEngine =
new SingleTemplateEngine(
this.app,
this.plugin,
this.choice.createFileIfItDoesntExist.template,
this.choiceExecutor,
);

if (linkOptions?.enabled && !linkOptions.requireActiveFile) {
singleTemplateEngine.setLinkToCurrentFileBehavior("optional");
this.formatter.setLinkToCurrentFileBehavior("optional");
}

fileContent = await singleTemplateEngine.run();

// Get template variables from the template engine's formatter
const templateVars = singleTemplateEngine.getAndClearTemplatePropertyVars();
const templatePath = this.choice.createFileIfItDoesntExist.template;
const templateContent =
await this.templateFileService.readTemplateContent(templatePath);
const { content, templatePropertyVars: templateVars } =
await new TemplateEvaluator(this.formatter).evaluateTemplateContent(
templateContent,
filePath,
);
fileContent = content;

log.logMessage(`CaptureChoiceEngine: Collected ${templateVars.size} template property variables`);
if (templateVars.size > 0) {
Expand Down
12 changes: 5 additions & 7 deletions src/engine/MacroChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type { IAIAssistantCommand } from "src/types/macros/QuickCommands/IAIAssi
import { runAIAssistant } from "src/ai/AIAssistant";
import { resolveProviderApiKey } from "src/ai/providerSecrets";
import { settingsStore } from "src/settingsStore";
import { CompleteFormatter } from "src/formatters/completeFormatter";
import { FormatterFactory } from "src/services/FormatterFactory";
import {
getModelByName,
getModelNames,
Expand Down Expand Up @@ -580,11 +580,10 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {
throw new Error(`Model ${modelName} not found with any provider.`);
}

const formatter = new CompleteFormatter(
const formatter = new FormatterFactory(
this.app,
QuickAdd.instance,
this.choiceExecutor
);
).createCompleteFormatter(this.choiceExecutor);

const modelProvider = getModelProvider(model.name);

Expand Down Expand Up @@ -725,11 +724,10 @@ export class MacroChoiceEngine extends QuickAddChoiceEngine {

private async executeOpenFile(command: IOpenFileCommand) {
try {
const formatter = new CompleteFormatter(
const formatter = new FormatterFactory(
this.app,
QuickAdd.instance,
this.choiceExecutor
);
).createCompleteFormatter(this.choiceExecutor);

const resolvedPath = await formatter.formatFileName(command.filePath, "");
const normalizedPath = resolvedPath.replace(/\\/g, "/");
Expand Down
2 changes: 1 addition & 1 deletion src/engine/SingleInlineScriptEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class SingleInlineScriptEngine extends MacroChoiceEngine {
app: App,
plugin: QuickAdd,
choiceExecutor: IChoiceExecutor,
variables: Map<string, string>
variables: Map<string, unknown>
) {
//@ts-ignore
super(app, plugin, null, choiceExecutor, variables);
Expand Down
Loading
Loading