diff --git a/Source/Core/Controls/Scripting/ScriptDocumentTab.cs b/Source/Core/Controls/Scripting/ScriptDocumentTab.cs index 31c03039..33bd105c 100755 --- a/Source/Core/Controls/Scripting/ScriptDocumentTab.cs +++ b/Source/Core/Controls/Scripting/ScriptDocumentTab.cs @@ -28,6 +28,7 @@ using CodeImp.DoomBuilder.Config; using CodeImp.DoomBuilder.Compilers; using CodeImp.DoomBuilder.ZDoom.Scripting; using ScintillaNET; +using System.Text; #endregion @@ -203,6 +204,44 @@ namespace CodeImp.DoomBuilder.Controls // This compiles the script public virtual void Compile() { } + // [ZZ] this removes trailing whitespace from every line + protected void RemoveTrailingWhitespace() + { + // after changing the contents, selection should stay on the same line, and just move to the end of that line if it was on the trailing space. + int selectionStart = editor.SelectionStart; + int selectionEnd = editor.SelectionEnd; + + int offset = 0; + string text = editor.Text; + string[] atext = text.Split(new char[] { '\n' }); + for (int i = 0; i < atext.Length; i++) + { + string oldtext = atext[i]; + string newtext = oldtext.TrimEnd(); + int lendiff = oldtext.Length - newtext.Length; + + bool selectioninline1 = selectionStart >= offset && selectionStart < offset + oldtext.Length; + bool selectioninline2 = selectionEnd >= offset && selectionEnd < offset + oldtext.Length; + + if (selectioninline1 && selectionStart > offset + newtext.Length) + selectionStart = offset + newtext.Length; + + if (selectioninline2 && selectionEnd > offset + newtext.Length) + selectionEnd = offset + newtext.Length; + + atext[i] = newtext; + offset += newtext.Length + 1; // include \n + if (selectionStart > offset) + selectionStart -= lendiff; + if (selectionEnd > offset) + selectionEnd -= lendiff; + } + + editor.Text = string.Join("\n", atext); + editor.SelectionStart = selectionStart; + editor.SelectionEnd = selectionEnd; + } + // This saves the document (used for both explicit and implicit) // Return true when successfully saved public virtual bool Save() diff --git a/Source/Core/Controls/Scripting/ScriptFileDocumentTab.cs b/Source/Core/Controls/Scripting/ScriptFileDocumentTab.cs index 715d19c3..2d8d0f6c 100755 --- a/Source/Core/Controls/Scripting/ScriptFileDocumentTab.cs +++ b/Source/Core/Controls/Scripting/ScriptFileDocumentTab.cs @@ -104,7 +104,10 @@ namespace CodeImp.DoomBuilder.Controls // Return true when successfully saved public override bool Save() { - try + // [ZZ] remove trailing whitespace + RemoveTrailingWhitespace(); + + try { // Write the file File.WriteAllBytes(filepathname, editor.GetText()); diff --git a/Source/Core/Controls/Scripting/ScriptLumpDocumentTab.cs b/Source/Core/Controls/Scripting/ScriptLumpDocumentTab.cs index 553def44..3c5d5636 100755 --- a/Source/Core/Controls/Scripting/ScriptLumpDocumentTab.cs +++ b/Source/Core/Controls/Scripting/ScriptLumpDocumentTab.cs @@ -108,6 +108,9 @@ namespace CodeImp.DoomBuilder.Controls // Implicit save public override bool Save() { + // [ZZ] remove trailing whitespace + RemoveTrailingWhitespace(); + // Store the lump data MemoryStream stream = new MemoryStream(editor.GetText()); General.Map.SetLumpData(lumpname, stream); diff --git a/Source/Core/Controls/Scripting/ScriptResourceDocumentTab.cs b/Source/Core/Controls/Scripting/ScriptResourceDocumentTab.cs index b896ea9a..173ee3a8 100755 --- a/Source/Core/Controls/Scripting/ScriptResourceDocumentTab.cs +++ b/Source/Core/Controls/Scripting/ScriptResourceDocumentTab.cs @@ -95,8 +95,11 @@ namespace CodeImp.DoomBuilder.Controls { if(source.IsReadOnly) return false; - // Find lump, check it's hash - bool dosave = true; + // [ZZ] remove trailing whitespace + RemoveTrailingWhitespace(); + + // Find lump, check it's hash + bool dosave = true; DataReader reader = source.Resource; // reload the reader bool wasReadOnly = reader.IsReadOnly; diff --git a/Source/Core/Properties/AssemblyInfo.cs b/Source/Core/Properties/AssemblyInfo.cs index 8979d16b..cb4ae259 100755 --- a/Source/Core/Properties/AssemblyInfo.cs +++ b/Source/Core/Properties/AssemblyInfo.cs @@ -30,6 +30,6 @@ using CodeImp.DoomBuilder; // Build Number // Revision // -[assembly: AssemblyVersion("2.3.0.2903")] +[assembly: AssemblyVersion("2.3.0.2904")] [assembly: NeutralResourcesLanguageAttribute("en")] -[assembly: AssemblyHash("4a79c74")] +[assembly: AssemblyHash("bd00eef")] diff --git a/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs b/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs index fd406909..109ee002 100755 --- a/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs +++ b/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ using System.Resources; // Build Number // Revision // -[assembly: AssemblyVersion("2.3.0.2903")] +[assembly: AssemblyVersion("2.3.0.2904")] [assembly: NeutralResourcesLanguageAttribute("en")]