-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEditorSettings.cs
More file actions
287 lines (232 loc) · 9.12 KB
/
Copy pathEditorSettings.cs
File metadata and controls
287 lines (232 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Configuration;
using Terminal.Gui.App;
using Terminal.Gui.Configuration;
namespace Ted;
#pragma warning disable CS0618 // Keep legacy CM attributes until Terminal.Gui fully removes CM.
/// <summary>
/// ted's persisted editor settings. Microsoft.Extensions.Configuration is the primary read path:
/// startup loads <c>~/.tui/ted.config.json</c> and applies the values to these static properties
/// before <see cref="TedApp" /> is constructed. Legacy CM attributes are retained only so older
/// Terminal.Gui builds can still apply the previous <see cref="AppSettingsScope" /> format.
/// <para>
/// <see cref="Save(string)" /> writes the MEC-native shape:
/// <c>"EditorSettings": { "WordWrap": true }</c>. Other top-level keys a user may have added
/// are preserved; JSONC comments are not. Legacy flat root-level
/// <c>"EditorSettings.*"</c> keys and old CM <c>"AppSettings"</c> entries are dropped on save
/// once migrated.
/// </para>
/// </summary>
internal static class EditorSettings
{
internal const string SectionName = "EditorSettings";
[ConfigurationProperty (Scope = typeof (AppSettingsScope))]
public static bool LineNumbers
{
get => Defaults.LineNumbers;
set => Defaults.LineNumbers = value;
}
[ConfigurationProperty (Scope = typeof (AppSettingsScope))]
public static bool FoldIndicators
{
get => Defaults.FoldIndicators;
set => Defaults.FoldIndicators = value;
}
[ConfigurationProperty (Scope = typeof (AppSettingsScope))]
public static bool WordWrap
{
get => Defaults.WordWrap;
set => Defaults.WordWrap = value;
}
[ConfigurationProperty (Scope = typeof (AppSettingsScope))]
public static bool ShowTabs
{
get => Defaults.ShowTabs;
set => Defaults.ShowTabs = value;
}
[ConfigurationProperty (Scope = typeof (AppSettingsScope))]
public static int IndentSize
{
get => Defaults.IndentSize;
set => Defaults.IndentSize = value;
}
[ConfigurationProperty (Scope = typeof (AppSettingsScope))]
public static bool ConvertTabsToSpaces
{
get => Defaults.ConvertTabsToSpaces;
set => Defaults.ConvertTabsToSpaces = value;
}
[ConfigurationProperty (Scope = typeof (AppSettingsScope))]
public static bool AutoIndent
{
get => Defaults.AutoIndent;
set => Defaults.AutoIndent = value;
}
[ConfigurationProperty (Scope = typeof (AppSettingsScope))]
public static bool Scrollbars
{
get => Defaults.Scrollbars;
set => Defaults.Scrollbars = value;
}
[ConfigurationProperty (Scope = typeof (AppSettingsScope))]
public static bool AutoComplete
{
get => Defaults.AutoComplete;
set => Defaults.AutoComplete = value;
}
internal static EditorSettingsValues Defaults { get; set; } = new ();
internal static IConfiguration BuildConfiguration (string path)
{
return new ConfigurationBuilder ()
.AddJsonFile (path, true, false)
.Build ();
}
internal static void Load (string path)
{
Apply (BuildConfiguration (path));
}
internal static void Apply (IConfiguration configuration)
{
ArgumentNullException.ThrowIfNull (configuration);
EditorSettingsValues settings = new ();
ApplyLegacyDottedKeys (configuration, settings);
ApplyLegacyDottedKeys (configuration.GetSection ("AppSettings"), settings);
configuration.GetSection (SectionName).Bind (settings);
Defaults = settings;
}
internal static void ResetDefaults ()
{
Defaults = new EditorSettingsValues ();
}
internal static void Save (string path)
{
try
{
JsonObject root = ReadRoot (path);
RemoveLegacyDottedKeys (root);
if (root["AppSettings"] is JsonObject appSettings)
{
RemoveLegacyDottedKeys (appSettings);
if (appSettings.Count == 0)
{
root.Remove ("AppSettings");
}
}
root[SectionName] = new JsonObject
{
[nameof (LineNumbers)] = LineNumbers,
[nameof (FoldIndicators)] = FoldIndicators,
[nameof (WordWrap)] = WordWrap,
[nameof (ShowTabs)] = ShowTabs,
[nameof (IndentSize)] = IndentSize,
[nameof (ConvertTabsToSpaces)] = ConvertTabsToSpaces,
[nameof (AutoIndent)] = AutoIndent,
[nameof (AutoComplete)] = AutoComplete,
[nameof (Scrollbars)] = Scrollbars
};
var directory = Path.GetDirectoryName (path);
if (!string.IsNullOrWhiteSpace (directory))
{
Directory.CreateDirectory (directory);
}
File.WriteAllText (path, root.ToJsonString (new JsonSerializerOptions { WriteIndented = true }));
}
catch (Exception ex)
{
Logging.Error ($"EditorSettings.Save: {ex.GetType ().Name}: {ex.Message}");
}
}
internal static string GetConfigPath ()
{
var home =
Environment.GetEnvironmentVariable ("HOME")
?? Environment.GetFolderPath (Environment.SpecialFolder.UserProfile);
return Path.Combine (home, ".tui", "ted.config.json");
}
private static JsonObject ReadRoot (string path)
{
if (!File.Exists (path))
{
return new JsonObject ();
}
var text = File.ReadAllText (path);
if (string.IsNullOrWhiteSpace (text))
{
return new JsonObject ();
}
// Tolerate the JSON TG itself accepts (// comments, trailing commas).
JsonNode? node = JsonNode.Parse (
text,
documentOptions: new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true
});
return node as JsonObject ?? new JsonObject ();
}
private static void ApplyLegacyDottedKeys (IConfiguration configuration, EditorSettingsValues settings)
{
ApplyLegacyBoolean (configuration, nameof (LineNumbers), value => settings.LineNumbers = value);
ApplyLegacyBoolean (configuration, nameof (FoldIndicators), value => settings.FoldIndicators = value);
ApplyLegacyBoolean (configuration, nameof (WordWrap), value => settings.WordWrap = value);
ApplyLegacyBoolean (configuration, nameof (ShowTabs), value => settings.ShowTabs = value);
ApplyLegacyInt32 (configuration, nameof (IndentSize), value => settings.IndentSize = value);
ApplyLegacyBoolean (configuration, nameof (ConvertTabsToSpaces), value => settings.ConvertTabsToSpaces = value);
ApplyLegacyBoolean (configuration, nameof (AutoIndent), value => settings.AutoIndent = value);
ApplyLegacyBoolean (configuration, nameof (Scrollbars), value => settings.Scrollbars = value);
ApplyLegacyBoolean (configuration, nameof (AutoComplete), value => settings.AutoComplete = value);
}
private static void ApplyLegacyBoolean (IConfiguration configuration, string propertyName, Action<bool> apply)
{
var value = configuration[$"{SectionName}.{propertyName}"];
if (value is null)
{
return;
}
if (bool.TryParse (value, out var parsed))
{
apply (parsed);
return;
}
Logging.Error ($"EditorSettings.Load: invalid legacy {SectionName}.{propertyName} value '{value}'.");
}
private static void ApplyLegacyInt32 (IConfiguration configuration, string propertyName, Action<int> apply)
{
var value = configuration[$"{SectionName}.{propertyName}"];
if (value is null)
{
return;
}
if (int.TryParse (value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsed))
{
apply (parsed);
return;
}
Logging.Error ($"EditorSettings.Load: invalid legacy {SectionName}.{propertyName} value '{value}'.");
}
private static void RemoveLegacyDottedKeys (JsonObject json)
{
foreach (var legacyKey in json
.Where (kvp => kvp.Key.StartsWith ($"{SectionName}.", StringComparison.Ordinal))
.Select (kvp => kvp.Key)
.ToList ())
{
json.Remove (legacyKey);
}
}
internal sealed class EditorSettingsValues
{
public bool LineNumbers { get; set; } = true;
public bool FoldIndicators { get; set; } = true;
public bool WordWrap { get; set; }
public bool ShowTabs { get; set; }
public int IndentSize { get; set; } = 4;
public bool ConvertTabsToSpaces { get; set; } = true;
public bool AutoIndent { get; set; } = true;
public bool Scrollbars { get; set; } = true;
public bool AutoComplete { get; set; }
}
}
#pragma warning restore CS0618