Added: script editor will now automatically trim trailing whitespace on save.

This commit is contained in:
ZZYZX 2017-02-20 07:07:44 +02:00
parent bd00eef8de
commit 271df46f2a
6 changed files with 54 additions and 6 deletions

View file

@ -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()

View file

@ -104,6 +104,9 @@ namespace CodeImp.DoomBuilder.Controls
// Return true when successfully saved
public override bool Save()
{
// [ZZ] remove trailing whitespace
RemoveTrailingWhitespace();
try
{
// Write the file

View file

@ -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);

View file

@ -95,6 +95,9 @@ namespace CodeImp.DoomBuilder.Controls
{
if(source.IsReadOnly) return false;
// [ZZ] remove trailing whitespace
RemoveTrailingWhitespace();
// Find lump, check it's hash
bool dosave = true;
DataReader reader = source.Resource;

View file

@ -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")]

View file

@ -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")]