From 4a906193b59be9de6f616667c1290d74bd537b03 Mon Sep 17 00:00:00 2001 From: psyGamer Date: Fri, 27 Dec 2024 16:03:15 +0100 Subject: [PATCH] feat(Studio): Enforce command style with StyleConfig --- Studio/CelesteStudio/Editing/Editor.cs | 40 ++++++++++++++------- Studio/CelesteStudio/Editing/StyleConfig.cs | 2 +- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/Studio/CelesteStudio/Editing/Editor.cs b/Studio/CelesteStudio/Editing/Editor.cs index 86a865ed..ca34ab06 100644 --- a/Studio/CelesteStudio/Editing/Editor.cs +++ b/Studio/CelesteStudio/Editing/Editor.cs @@ -45,6 +45,9 @@ public Document Document { DocumentChanged(document, value); document = value; + // Ensure everything is properly formatted + FormatLines(Enumerable.Range(0, document.Lines.Count)); + // Jump to end when file only 10 lines, else the start document.Caret = document.Lines.Count is > 0 and <= 10 ? new CaretPosition(document.Lines.Count - 1, document.Lines[^1].Length) @@ -121,7 +124,7 @@ void DetectPreference() { void HandleTextChanged(Document _, Dictionary insertions, Dictionary deletions) { lastModification = DateTime.UtcNow; - ConvertToActionLines(insertions.Keys); + FormatLines(insertions.Keys); // Adjust total frame count foreach (string deletion in deletions.Values) { @@ -819,28 +822,39 @@ public bool TryParseAndFormatActionLine(int row, out ActionLine actionLine) { return false; } - /// Applies the correct action-line formatting to all specified lines - private void ConvertToActionLines(IEnumerable rows) { + /// Applies the correct formatting to all specified lines + private void FormatLines(IEnumerable rows) { using var __ = Document.Update(raiseEvents: false); // Convert to action lines, if possible foreach (int row in rows) { string line = Document.Lines[row]; - if (!ActionLine.TryParse(line, out var actionLine)) { - continue; - } - string newLine = actionLine.ToString(); + if (ActionLine.TryParse(line, out var actionLine)) { + string newLine = actionLine.ToString(); - if (Document.Caret.Row == row) { - if (Document.Caret.Col == line.Length) { - Document.Caret.Col = newLine.Length; + if (Document.Caret.Row == row) { + if (Document.Caret.Col == line.Length) { + Document.Caret.Col = newLine.Length; + } else { + Document.Caret.Col = SnapColumnToActionLine(actionLine, Document.Caret.Col); + } + } + + Document.ReplaceLine(row, newLine); + } else if (CommandLine.TryParse(line, out var commandLine)) { + string commandName; + if (StyleConfig.Current.ForceCorrectCommandCasing && + CommunicationWrapper.Commands.FirstOrDefault(cmd => string.Equals(cmd.Name, commandLine.Command, StringComparison.OrdinalIgnoreCase)) is { } command && !string.IsNullOrEmpty(command.Name)) + { + commandName = command.Name; } else { - Document.Caret.Col = SnapColumnToActionLine(actionLine, Document.Caret.Col); + commandName = commandLine.Command; } - } - Document.ReplaceLine(row, newLine); + string separator = StyleConfig.Current.CommandArgumentSeparator ?? commandLine.ArgumentSeparator; + Document.ReplaceLine(row, string.Join(separator, [commandName, ..commandLine.Arguments])); + } } } diff --git a/Studio/CelesteStudio/Editing/StyleConfig.cs b/Studio/CelesteStudio/Editing/StyleConfig.cs index 3f776628..89b2deb3 100644 --- a/Studio/CelesteStudio/Editing/StyleConfig.cs +++ b/Studio/CelesteStudio/Editing/StyleConfig.cs @@ -17,7 +17,7 @@ public struct StyleConfig() { public static StyleConfig Current { get; private set; } = new(); public bool ForceCorrectCommandCasing { get; set; } = false; - public string? CommandSeparator { get; set; } = null; + public string? CommandArgumentSeparator { get; set; } = null; public int? RoomLabelStartingIndex { get; set; } = null; public AutoRoomIndexing? RoomLabelIndexing { get; set; } = null;