-
Notifications
You must be signed in to change notification settings - Fork 1
Port ted settings UX from clet edit: immediate settings persistence via View/Options, no save-on-quit #123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
90d096e
Initial plan
Copilot d200daf
feat(ted): port settings UI with view/options menus and persisted edi…
Copilot 7618133
fix(ted): harden settings save replacement and use cross-platform con…
Copilot 18dd69a
refactor(ted): persist show-tabs from editor state
Copilot b617477
fix(ted): preserve editor defaults and harden settings save path setup
Copilot 2912c52
fix(ci): apply formatter-required indexer spacing in ted settings
Copilot 9d10a78
fix(ted): always save settings to ted.config and add persistence tests
Copilot d43f198
test(ted): align settings persistence tests with var-style rules
Copilot 48d362e
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig 9606b82
fix(ted): persist view settings reliably and harden cross-platform tests
Copilot 58861bd
test(ted): fix cross-platform settings persistence coverage and namin…
Copilot 586bb17
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig 4b14ad7
fix(ted): persist settings on all app exit paths
Copilot 5aa6d34
Merge branch 'develop' into copilot/port-settings-ui-to-ted
tig 82ea8bb
Align ted settings persistence with clet behavior
Copilot cda648b
Merge origin/develop to resolve conflicts
Copilot 02701c0
Address review feedback in merged TedApp tests
Copilot eacf13d
fix ted settings validation and JSONC comma insertion edge case
Copilot ef498fa
test: clarify settings dialog reflection and inline JSON fixture
Copilot a8bdfe9
refactor: simplify trailing-comment comma insertion whitespace trim
Copilot 93b5dcd
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig 9ce3c86
fix(ted): persist settings on word-wrap checkbox value changes
Copilot 8886220
Merge branch 'develop' into copilot/port-settings-ui-to-ted
tig 48e2a1e
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig 41a189e
fix(ted): save word-wrap changes from menu action path
Copilot f2cf09f
test(ted): stabilize word-wrap menu persistence test across CI runners
Copilot f61d8c3
test(ted): make word-wrap menu hit-target lookup resilient
Copilot e450119
test(ted): prefer label hit-target with glyph fallbacks in menu test
Copilot 7e8815f
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig 6f0726f
Merge branch 'develop' into copilot/port-settings-ui-to-ted
tig 77e6f47
Merge branch 'copilot/port-settings-ui-to-ted' of https://github.com/…
tig eaae70f
fix(ted): remove ValueChanged handler that caused WordWrap double-toggle
tig 51d05c5
fix(ted): unify config path to ~/.tui, add Load, remove ConfigMgr reset
tig 89d2e27
fix(ted): prevent IndentSize < 1 via ValueChanging
tig ae0b0ff
fix(test): reset EditorSettings statics after Load tests
tig 504654a
refactor(test): avoid direct static assertions, reset in ConfigPathScope
tig 61bf634
fix(ted): skip JSONC-commented lines when loading settings
tig f628184
Merge branch 'develop' into copilot/port-settings-ui-to-ted
tig 8f0493a
fix(ted): harden ReadBool/ReadInt and Save against edge cases
tig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 (); | ||
|
|
||
| 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]; | ||
|
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; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.