From 4c94864c7a56eac2041c869235f5c1ebfd47a5dc Mon Sep 17 00:00:00 2001 From: WolfGangS Date: Tue, 2 Dec 2025 21:44:50 +0000 Subject: [PATCH 1/3] Prerun the preprocessor to build the include file tree so any edited file will trigger a run --- src/scriptsync.ts | 123 ++++++++++++++++++++++++-------------------- src/synchservice.ts | 1 + 2 files changed, 69 insertions(+), 55 deletions(-) diff --git a/src/scriptsync.ts b/src/scriptsync.ts index 60b3427..d6ac9fb 100644 --- a/src/scriptsync.ts +++ b/src/scriptsync.ts @@ -83,6 +83,17 @@ export class ScriptSync implements vscode.Disposable { } } + public async initialize() : Promise { + const masterFilePath: string = this.getMasterFilePath(); + + const originalContent = await fs.promises.readFile( + masterFilePath, + "utf8", + ); + + await this.preProcessContent(originalContent); + } + //==================================================================== //#region utilities public showMasterDocument(): void { @@ -362,72 +373,74 @@ export class ScriptSync implements vscode.Disposable { } //#endregion - public async handleMasterSaved(): Promise { + public async preProcessContent(originalContent: string): Promise { + // Check if preprocessing is enabled + if(!this.preprocessor) return originalContent; + if(!this.config.getConfig(ConfigKey.PreprocessorEnable)) return originalContent; + + this.clearDiagnostics(); + + const masterFilePath: string = this.getMasterFilePath(); + const baseName: string = path.basename(masterFilePath); + let preprocessorResult: PreprocessorResult | null = null; + let finalContent = originalContent; try { - // Read the original content - const masterFilePath: string = this.getMasterFilePath(); - const baseName: string = path.basename(masterFilePath); + console.log(`Preprocessing enabled for: ${baseName}`); - const originalContent = await fs.promises.readFile( - masterFilePath, - "utf8", + this.macros.clearNonSystemMacros(); + preprocessorResult = await this.preprocessor.process( + originalContent, + normalizePath(masterFilePath), + this.language ); - let finalContent = originalContent; - let preprocessorResult: PreprocessorResult | null = null; - - this.clearDiagnostics(); - // Check if preprocessing is enabled - if (this.preprocessor && this.config.getConfig(ConfigKey.PreprocessorEnable)) { - try { - console.log(`Preprocessing enabled for: ${baseName}`); - - this.macros.clearNonSystemMacros(); - preprocessorResult = await this.preprocessor.process( - originalContent, - normalizePath(masterFilePath), - this.language - ); - - if (preprocessorResult.issues && preprocessorResult.issues.length > 0) { - const diagnostics = ScriptSync.preprocessorErrorsToDiagnostics( - preprocessorResult.issues, - `${preprocessorResult.language} Preprocessor` - ); - this.addDiagnostics(diagnostics); - } - if(preprocessorResult.includes && preprocessorResult.includes.length > 0) { - this.includedFiles = preprocessorResult.includes; - } + if (preprocessorResult.issues && preprocessorResult.issues.length > 0) { + const diagnostics = ScriptSync.preprocessorErrorsToDiagnostics( + preprocessorResult.issues, + `${preprocessorResult.language} Preprocessor` + ); + this.addDiagnostics(diagnostics); + } - if (preprocessorResult.success) { - finalContent = preprocessorResult.content; - this.lineMappings = preprocessorResult.lineMappings; + if (preprocessorResult.includes && preprocessorResult.includes.length > 0) { + this.includedFiles = preprocessorResult.includes; + } - console.log( - `${preprocessorResult.language.toUpperCase()} preprocessing completed successfully for: ${baseName}`, - ); - } else { - // Preprocessing failed, use original content and show error - finalContent = originalContent; + if (preprocessorResult.success) { + finalContent = preprocessorResult.content; + this.lineMappings = preprocessorResult.lineMappings; - vscode.window.showErrorMessage("Preprocessing failed"); - } - } catch (error) { - // Fallback to original content on any unexpected errors - finalContent = originalContent; - const errorMessage = `Preprocessing error for ${baseName}: ${ - error instanceof Error ? error.message : String(error) - }`; - console.error(errorMessage); - vscode.window.showErrorMessage(errorMessage); - } - } else { console.log( - `Preprocessing disabled, using original content for: ${baseName}`, + `${preprocessorResult.language.toUpperCase()} preprocessing completed successfully for: ${baseName}`, ); + } else { + // Preprocessing failed, use original content and show error + finalContent = originalContent; + + vscode.window.showErrorMessage("Preprocessing failed"); } + } catch (error) { + // Fallback to original content on any unexpected errors + finalContent = originalContent; + const errorMessage = `Preprocessing error for ${baseName}: ${error instanceof Error ? error.message : String(error) + }`; + console.error(errorMessage); + vscode.window.showErrorMessage(errorMessage); + } + return finalContent; + } + + public async handleMasterSaved(): Promise { + try { + // Read the original content + const masterFilePath: string = this.getMasterFilePath(); + + const originalContent = await fs.promises.readFile( + masterFilePath, + "utf8", + ); + let finalContent = await this.preProcessContent(originalContent); const sha = sha256.create(); sha.update(finalContent); diff --git a/src/synchservice.ts b/src/synchservice.ts index 5f12973..a526534 100644 --- a/src/synchservice.ts +++ b/src/synchservice.ts @@ -217,6 +217,7 @@ export class SynchService implements vscode.Disposable { parsed.scriptId, viewerDocument, ); + await sync.initialize(); this.activeSyncs.set(masterPath, sync); } From 557bc12a776ac2bd92efd202c7a1798630c743b3 Mon Sep 17 00:00:00 2001 From: WolfGangS Date: Tue, 2 Dec 2025 21:46:03 +0000 Subject: [PATCH 2/3] lint --- src/scriptsync.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/scriptsync.ts b/src/scriptsync.ts index d6ac9fb..1e4b656 100644 --- a/src/scriptsync.ts +++ b/src/scriptsync.ts @@ -423,8 +423,7 @@ export class ScriptSync implements vscode.Disposable { } catch (error) { // Fallback to original content on any unexpected errors finalContent = originalContent; - const errorMessage = `Preprocessing error for ${baseName}: ${error instanceof Error ? error.message : String(error) - }`; + const errorMessage = `Preprocessing error for ${baseName}: ${error instanceof Error ? error.message : String(error)}`; console.error(errorMessage); vscode.window.showErrorMessage(errorMessage); } From f48338b46a104f9de982246a9063a37e560d5453 Mon Sep 17 00:00:00 2001 From: WolfGangS Date: Wed, 3 Dec 2025 01:20:27 +0000 Subject: [PATCH 3/3] Do not save to same file as source --- src/scriptsync.ts | 14 ++++++++++---- src/shared/includeprocessor.ts | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/scriptsync.ts b/src/scriptsync.ts index 7301881..7001a00 100644 --- a/src/scriptsync.ts +++ b/src/scriptsync.ts @@ -446,13 +446,19 @@ export class ScriptSync implements vscode.Disposable { sha.update(finalContent); const hash = sha.hex(); - console.error(finalContent, hash); + // console.error(finalContent, hash); finalContent = this.prefixWithMetaInformation(finalContent, hash); // Walk through all TrackedDocuments and save their finalContents if the hash has changed await Promise.all( this.getFileMappingsFilteredByHash(hash) .map((mapping) => { + if(masterFilePath == mapping.viewerDocument.fileName) { + // Do not write to the same file we are processing from + // Allows quick editing of a script externally that hasnt matched + // Without the script extending forever if the preproc is enabled + return Promise.resolve(); + } mapping.hash = hash; return fs.promises.writeFile( mapping.viewerDocument.fileName, @@ -469,14 +475,14 @@ export class ScriptSync implements vscode.Disposable { } private prefixWithMetaInformation(content:string, hash: string) : string { - console.error("PREFIX ENABLED", this.config.getConfig(ConfigKey.FileMetaInfoInOutput,false)); + // console.error("PREFIX ENABLED", this.config.getConfig(ConfigKey.FileMetaInfoInOutput,false)); if(!this.config.getConfig(ConfigKey.FileMetaInfoInOutput,false)) { return content; } const meta : string[]= []; const date = (new Date()).toISOString().split("T"); - console.error("PREFIX") + // console.error("PREFIX") const path = vscode.workspace.asRelativePath(this.masterDocument.uri.fsPath); @@ -485,7 +491,7 @@ export class ScriptSync implements vscode.Disposable { meta.push(`${comment} @file ${path}`); meta.push(`${comment} @hash ${hash}`); meta.push(`${comment} @date ${date[0]} ${date[1].split(".")[0]}`); - console.error("PREFIX CREATOR", this.config.getConfig(ConfigKey.FileMetaInfoIncludeCreator,false)); + // console.error("PREFIX CREATOR", this.config.getConfig(ConfigKey.FileMetaInfoIncludeCreator,false)); if(this.config.getConfig(ConfigKey.FileMetaInfoIncludeCreator, false)) { meta.push(`${comment} @creator ${ScriptSync.getCurrentAgentName()}`); meta.push(`${comment} @creatorID ${ScriptSync.getCurrentAgentId()}`); diff --git a/src/shared/includeprocessor.ts b/src/shared/includeprocessor.ts index bfaf0da..705c40b 100644 --- a/src/shared/includeprocessor.ts +++ b/src/shared/includeprocessor.ts @@ -173,7 +173,7 @@ export class IncludeProcessor { includePaths, aliased || allowExternal, ); - console.error("Resolve: ", [filename, sourceFile, extensions, includePaths, aliased, allowExternal], resolvedPath); + // console.error("Resolve: ", [filename, sourceFile, extensions, includePaths, aliased, allowExternal], resolvedPath); if(!resolvedPath && this.language == "luau") { // Luau require supports default file in folder include mechanic 'init.luau'