Skip to content

Commit

Permalink
feat(Studio): Enforce command style with StyleConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Dec 27, 2024
1 parent b5ab201 commit 4a90619
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
40 changes: 27 additions & 13 deletions Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -121,7 +124,7 @@ void DetectPreference() {
void HandleTextChanged(Document _, Dictionary<int, string> insertions, Dictionary<int, string> deletions) {
lastModification = DateTime.UtcNow;

ConvertToActionLines(insertions.Keys);
FormatLines(insertions.Keys);

// Adjust total frame count
foreach (string deletion in deletions.Values) {
Expand Down Expand Up @@ -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<int> rows) {
/// Applies the correct formatting to all specified lines
private void FormatLines(IEnumerable<int> 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]));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Studio/CelesteStudio/Editing/StyleConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4a90619

Please sign in to comment.