Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
90d096e
Initial plan
Copilot May 15, 2026
d200daf
feat(ted): port settings UI with view/options menus and persisted edi…
Copilot May 15, 2026
7618133
fix(ted): harden settings save replacement and use cross-platform con…
Copilot May 15, 2026
18dd69a
refactor(ted): persist show-tabs from editor state
Copilot May 15, 2026
b617477
fix(ted): preserve editor defaults and harden settings save path setup
Copilot May 15, 2026
2912c52
fix(ci): apply formatter-required indexer spacing in ted settings
Copilot May 15, 2026
9d10a78
fix(ted): always save settings to ted.config and add persistence tests
Copilot May 15, 2026
d43f198
test(ted): align settings persistence tests with var-style rules
Copilot May 15, 2026
48d362e
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig May 15, 2026
9606b82
fix(ted): persist view settings reliably and harden cross-platform tests
Copilot May 15, 2026
58861bd
test(ted): fix cross-platform settings persistence coverage and namin…
Copilot May 15, 2026
586bb17
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig May 15, 2026
4b14ad7
fix(ted): persist settings on all app exit paths
Copilot May 15, 2026
5aa6d34
Merge branch 'develop' into copilot/port-settings-ui-to-ted
tig May 15, 2026
82ea8bb
Align ted settings persistence with clet behavior
Copilot May 15, 2026
cda648b
Merge origin/develop to resolve conflicts
Copilot May 15, 2026
02701c0
Address review feedback in merged TedApp tests
Copilot May 15, 2026
eacf13d
fix ted settings validation and JSONC comma insertion edge case
Copilot May 15, 2026
ef498fa
test: clarify settings dialog reflection and inline JSON fixture
Copilot May 15, 2026
a8bdfe9
refactor: simplify trailing-comment comma insertion whitespace trim
Copilot May 15, 2026
93b5dcd
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig May 15, 2026
9ce3c86
fix(ted): persist settings on word-wrap checkbox value changes
Copilot May 15, 2026
8886220
Merge branch 'develop' into copilot/port-settings-ui-to-ted
tig May 15, 2026
48e2a1e
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig May 15, 2026
41a189e
fix(ted): save word-wrap changes from menu action path
Copilot May 15, 2026
f2cf09f
test(ted): stabilize word-wrap menu persistence test across CI runners
Copilot May 15, 2026
f61d8c3
test(ted): make word-wrap menu hit-target lookup resilient
Copilot May 15, 2026
e450119
test(ted): prefer label hit-target with glyph fallbacks in menu test
Copilot May 15, 2026
7e8815f
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig May 16, 2026
6f0726f
Merge branch 'develop' into copilot/port-settings-ui-to-ted
tig May 16, 2026
77e6f47
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig May 16, 2026
eaae70f
fix(ted): remove ValueChanged handler that caused WordWrap double-toggle
tig May 16, 2026
51d05c5
fix(ted): unify config path to ~/.tui, add Load, remove ConfigMgr reset
tig May 16, 2026
89d2e27
fix(ted): prevent IndentSize < 1 via ValueChanging
tig May 16, 2026
ae0b0ff
fix(test): reset EditorSettings statics after Load tests
tig May 16, 2026
504654a
refactor(test): avoid direct static assertions, reset in ConfigPathScope
tig May 16, 2026
61bf634
fix(ted): skip JSONC-commented lines when loading settings
tig May 16, 2026
f628184
Merge branch 'develop' into copilot/port-settings-ui-to-ted
tig May 16, 2026
8f0493a
fix(ted): harden ReadBool/ReadInt and Save against edge cases
tig May 16, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 280 additions & 0 deletions examples/ted/EditorSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
using System.Text.RegularExpressions;
using Terminal.Gui.App;
using Terminal.Gui.Configuration;

namespace Ted;

internal sealed class TedSettingsScope;

internal static class EditorSettings
{
[ConfigurationProperty (Scope = typeof (TedSettingsScope))]
public static bool LineNumbers { get; set; } = true;

[ConfigurationProperty (Scope = typeof (TedSettingsScope))]
public static bool FoldIndicators { get; set; } = true;

[ConfigurationProperty (Scope = typeof (TedSettingsScope))]
public static bool WordWrap { get; set; }

[ConfigurationProperty (Scope = typeof (TedSettingsScope))]
public static bool ShowTabs { get; set; }

[ConfigurationProperty (Scope = typeof (TedSettingsScope))]
public static bool UseThemeBackground { get; set; } = true;

[ConfigurationProperty (Scope = typeof (TedSettingsScope))]
public static int IndentSize { get; set; } = 4;

[ConfigurationProperty (Scope = typeof (TedSettingsScope))]
public static bool ConvertTabsToSpaces { get; set; } = true;

[ConfigurationProperty (Scope = typeof (TedSettingsScope))]
public static bool AutoIndent { get; set; } = true;

/// <summary>
/// Loads settings from the config file at <see cref="GetConfigPath" />.
/// Called once at startup before constructing <see cref="TedApp" />.
/// </summary>
internal static void Load ()
{
Load (GetConfigPath ());
}

internal static void Load (string path)
{
if (!File.Exists (path))
{
return;
}

try
{
string text = File.ReadAllText (path);

LineNumbers = ReadBool (text, "EditorSettings.LineNumbers", LineNumbers);
FoldIndicators = ReadBool (text, "EditorSettings.FoldIndicators", FoldIndicators);
WordWrap = ReadBool (text, "EditorSettings.WordWrap", WordWrap);
ShowTabs = ReadBool (text, "EditorSettings.ShowTabs", ShowTabs);
UseThemeBackground = ReadBool (text, "EditorSettings.UseThemeBackground", UseThemeBackground);
IndentSize = ReadInt (text, "EditorSettings.IndentSize", IndentSize);
ConvertTabsToSpaces = ReadBool (text, "EditorSettings.ConvertTabsToSpaces", ConvertTabsToSpaces);
AutoIndent = ReadBool (text, "EditorSettings.AutoIndent", AutoIndent);
}
catch (Exception ex)
{
Logging.Error ($"EditorSettings.Load: {ex.GetType ().Name}: {ex.Message}");
}
}

internal static void Save ()
{
Save (GetConfigPath ());
}

internal static void Save (string path)
{
try
{
EnsureConfigFile (path);
string text = File.ReadAllText (path);
Dictionary<string, string> entries = new ()
{
["EditorSettings.LineNumbers"] = ToJson (LineNumbers),
["EditorSettings.FoldIndicators"] = ToJson (FoldIndicators),
["EditorSettings.WordWrap"] = ToJson (WordWrap),
["EditorSettings.ShowTabs"] = ToJson (ShowTabs),
["EditorSettings.UseThemeBackground"] = ToJson (UseThemeBackground),
["EditorSettings.IndentSize"] = IndentSize.ToString (),
["EditorSettings.ConvertTabsToSpaces"] = ToJson (ConvertTabsToSpaces),
["EditorSettings.AutoIndent"] = ToJson (AutoIndent)
};

List<string> toInsert = [];

foreach ((string key, string value) in entries)
{
Regex pattern = new (
$@"^(?<prefix>\s*""{Regex.Escape (key)}""\s*:\s*)(?:true|false|-?\d+)(?<suffix>\s*,?\s*(?://.*)?)$",
RegexOptions.Multiline);
bool replaced = false;
text = pattern.Replace (
text,
match =>
{
replaced = true;

return $"{match.Groups["prefix"].Value}{value}{match.Groups["suffix"].Value}";
},
1);

if (replaced)
{
continue;
}

toInsert.Add ($" \"{key}\": {value}");
}

if (toInsert.Count > 0)
{
int lastBrace = FindRootClosingBrace (text);

if (lastBrace >= 0)
{
int insertCommaAfter = FindLastObjectMemberCharacterPosition (text, lastBrace);

if (insertCommaAfter >= 0 && text[insertCommaAfter] != ',' && text[insertCommaAfter] != '{')
{
text = text.Insert (insertCommaAfter + 1, ",");
lastBrace = FindRootClosingBrace (text);
}

string insertion = $"\n\n{string.Join (",\n", toInsert)}\n";
text = text.Insert (lastBrace, insertion);
}
}

File.WriteAllText (path, text);
}
catch (Exception ex)
{
Logging.Error ($"EditorSettings.Save: {ex.GetType ().Name}: {ex.Message}");
}
}

internal static string GetConfigPath ()
{
string home =
Environment.GetEnvironmentVariable ("HOME")
?? Environment.GetFolderPath (Environment.SpecialFolder.UserProfile)
?? Directory.GetCurrentDirectory ();
Comment thread
tig marked this conversation as resolved.

return Path.Combine (home, ".tui", "ted.config.json");
}

private static void EnsureConfigFile (string path)
{
string? directory = Path.GetDirectoryName (path);

if (!string.IsNullOrWhiteSpace (directory))
{
Directory.CreateDirectory (directory);
}

if (!File.Exists (path))
{
File.WriteAllText (path, "{}");
}
}

private static string ToJson (bool value)
{
return value ? "true" : "false";
}

private static bool ReadBool (string json, string key, bool defaultValue)
{
// Match key only at a JSON property position: line starts with optional whitespace,
// then the key. Negative lookahead skips // comment lines.
Match m = Regex.Match (
json,
$@"^(?!\s*//)\s*""{Regex.Escape (key)}""\s*:\s*(?<v>true|false)",
RegexOptions.IgnoreCase | RegexOptions.Multiline);

return m.Success ? string.Equals (m.Groups["v"].Value, "true", StringComparison.OrdinalIgnoreCase) : defaultValue;
}

private static int ReadInt (string json, string key, int defaultValue)
{
Match m = Regex.Match (
json,
$@"^(?!\s*//)\s*""{Regex.Escape (key)}""\s*:\s*(?<v>-?\d+)",
RegexOptions.Multiline);

return m.Success && int.TryParse (m.Groups["v"].Value, out int v) ? v : defaultValue;
}

/// <summary>
/// Finds the last '}' that is NOT inside a // comment.
/// Scans backwards, skipping any '}' on a line whose non-whitespace content starts with //.
/// </summary>
private static int FindRootClosingBrace (string text)
{
int i = text.Length - 1;

while (i >= 0)
{
i = text.LastIndexOf ('}', i);

if (i < 0)
{
return -1;
}

// Check if this '}' is on a comment line
int lineStart = text.LastIndexOf ('\n', i) + 1;
string lineBeforeBrace = text[lineStart..i];

if (lineBeforeBrace.TrimStart ().StartsWith ("//", StringComparison.Ordinal))
{
i--;

continue;
}

return i;
}

return -1;
}

private static int FindLastObjectMemberCharacterPosition (string text, int braceIndex)
{
int i = braceIndex - 1;

while (i >= 0)
{
char c = text[i];

if (char.IsWhiteSpace (c))
{
i--;

continue;
}

int lineStart = text.LastIndexOf ('\n', i) + 1;
string line = text[lineStart..(i + 1)];
string trimmedLine = line.TrimStart ();

if (trimmedLine.StartsWith ("//", StringComparison.Ordinal))
{
i = lineStart - 1;

continue;
}

int commentStart = line.IndexOf ("//", StringComparison.Ordinal);

if (commentStart >= 0)
{
string withoutComment = line[..commentStart];
Comment thread
tig marked this conversation as resolved.
int lastNonWhitespace = withoutComment.TrimEnd ().Length - 1;

if (lastNonWhitespace >= 0)
{
return lineStart + lastNonWhitespace;
}

i = lineStart - 1;

continue;
}

return i;
}

return -1;
}
}
Loading
Loading