Skip to content

Commit

Permalink
tweak(Studio): More document formatting during save
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Nov 15, 2024
1 parent 63e4da7 commit 15c6e8b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Studio/CelesteStudio/Editing/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using CelesteStudio.Communication;
using CelesteStudio.Data;
using CelesteStudio.Util;
using Eto.Forms;
using StudioCommunication.Util;
Expand Down Expand Up @@ -137,7 +138,15 @@ private void OnTextChanged(Dictionary<int, string> insertions, Dictionary<int, s

/// Formats lines of a file into a single string, using consistent formatting rules
public static string FormatLinesToText(IEnumerable<string> lines) {
string text = string.Join(NewLine, lines);
// TODO: Maybe don't allocate this much on every save?
string text = string.Join(NewLine, lines.Select(line => {
// Trim whitespace, remove invalid characters, format lines
if (ActionLine.TryParse(line, out var actionLine)) {
return actionLine.ToString();
} else {
return new string(line.Trim().Where(c => !char.IsControl(c) && c != char.MaxValue).ToArray());
}
}));

// Require exactly 1 empty line at end of file
text = text.TrimEnd(NewLine) + $"{NewLine}";
Expand Down Expand Up @@ -876,7 +885,7 @@ public void SwapLines(int rowA, int rowB) {
// Swap anchors
var aAnchors = CurrentAnchors.TryGetValue(rowA, out var anchors) ? anchors : [];
var bAnchors = CurrentAnchors.TryGetValue(rowA, out anchors) ? anchors : [];

CurrentAnchors[rowA] = bAnchors;
CurrentAnchors[rowB] = aAnchors;
}
Expand Down

0 comments on commit 15c6e8b

Please sign in to comment.