Skip to content

Commit

Permalink
feat(Studio): Read .studioconfig.toml file
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Dec 27, 2024
1 parent f2f866b commit d369818
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ namespace CelesteStudio.Editing;

public sealed class Editor : SkiaDrawable {
/// Reports insertions and deletions of the underlying document
public event Action<Document, Dictionary<int, string>, Dictionary<int, string>> TextChanged = (_, _, _) => {};
public event Action<Document, Dictionary<int, string>, Dictionary<int, string>> TextChanged = (_, _, _) => { };

/// The currently open file has changed. The previous document might not be available (for example during startup).
public event Action<Document?, Document> DocumentChanged = (_, _) => { };

private Document? document;
public Document Document {
Expand All @@ -39,6 +42,7 @@ public Document Document {
}
}

DocumentChanged(document, value);
document = value;

// Jump to end when file only 10 lines, else the start
Expand Down Expand Up @@ -313,6 +317,10 @@ public Editor(Document document, Scrollable scrollable) {
this.document = document;
this.scrollable = scrollable;

StyleConfig.Initialize(this);

DocumentChanged(null, document);

CanFocus = true;
Cursor = Cursors.IBeam;

Expand Down
49 changes: 49 additions & 0 deletions Studio/CelesteStudio/Editing/StyleConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using CelesteStudio.Data;
using CelesteStudio.Dialog;
using Eto.Forms;
using StudioCommunication.Util;
using System;
using System.IO;
using System.Reflection;
using Tomlet;
using Tomlet.Models;

namespace CelesteStudio.Editing;

/// Defined by an ".studioconfig.toml" file in the root of the project
public struct StyleConfig() {
private const string ConfigFile = ".studioconfig.toml";

public static StyleConfig Current { get; private set; } = new();

public bool ForceCorrectCommandCasing { get; set; } = false;
public string? CommandSeparator { get; set; } = null;

public int? RoomLabelStartingIndex { get; set; } = null;
public AutoRoomIndexing? RoomLabelIndexing { get; set; } = null;

public static void Initialize(Editor editor) {
editor.DocumentChanged += (_, document) => {
if (string.IsNullOrEmpty(document.FilePath)) {
return;
}

string projectRoot = Editor.FindProjectRoot(document.FilePath);
string configPath = Path.Combine(projectRoot, ConfigFile);

if (!File.Exists(configPath)) {
return;
}

Console.WriteLine($"Found project style config '{configPath}'");

try {
var toml = TomlParser.ParseFile(configPath);
Current = TomletMain.To<StyleConfig>(toml, new TomlSerializerOptions());
} catch (Exception ex) {
Console.Error.WriteLine("Failed to load project style config");
Console.Error.WriteLine(ex);
}
};
}
}

0 comments on commit d369818

Please sign in to comment.