From aec2763b278b9be7e7a239002c1bc5cab0911fd1 Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Fri, 14 Jan 2022 05:14:56 -0500 Subject: [PATCH] Ignore trailing whitespace for dirty file asterisk Saved scripts is stored on servers with its starting & trailing whitespace removed, so comparison would fail and show the file as dirty when there was a trailing newline in the script. --- src/Script/Script.ts | 11 ++++++++++- src/ScriptEditor/ui/ScriptEditorRoot.tsx | 4 +++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Script/Script.ts b/src/Script/Script.ts index 7e8e5a562..7b055c025 100644 --- a/src/Script/Script.ts +++ b/src/Script/Script.ts @@ -118,7 +118,7 @@ export class Script { */ saveScript(player: IPlayer, filename: string, code: string, hostname: string, otherScripts: Script[]): void { // Update code and filename - this.code = code.replace(/^\s+|\s+$/g, ""); + this.code = Script.formatCode(this.code); this.filename = filename; this.server = hostname; @@ -158,6 +158,15 @@ export class Script { s.rehash(); return s; } + + /** + * Formats code: Removes the starting & trailing whitespace + * @param {string} code - The code to format + * @returns The formatted code + */ + static formatCode(code: string): string { + return code.replace(/^\s+|\s+$/g, ""); + } } Reviver.constructors.Script = Script; diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index fdc25ada5..b27b1a2e5 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -686,7 +686,9 @@ export function Root(props: IProps): React.ReactElement { const serverScript = server.scripts.find((s) => s.filename === openScript.fileName); if (serverScript === undefined) return " *"; - return serverScript.code !== openScript.code ? " *" : ""; + // The server code is stored with its starting & trailing whitespace removed + const openScriptFormatted = Script.formatCode(openScript.code); + return serverScript.code !== openScriptFormatted ? " *" : ""; } // Toolbars are roughly 112px: