Skip to content

Commit 43e4a83

Browse files
authored
Merge pull request gui-cs#3360 from tig/v2_3359_CM_Reset
Fixes gui-cs#3359. Updates Unit Tests to do CM.Reset
2 parents 0ef67d7 + e7a116e commit 43e4a83

File tree

7 files changed

+73
-55
lines changed

7 files changed

+73
-55
lines changed

Terminal.Gui/Configuration/ConfigProperty.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public bool Apply ()
4141
{
4242
if (PropertyInfo?.GetValue (null) is { })
4343
{
44-
PropertyInfo?.SetValue (null, DeepMemberwiseCopy (PropertyValue, PropertyInfo?.GetValue (null)));
44+
PropertyInfo?.SetValue (null, DeepMemberWiseCopy (PropertyValue, PropertyInfo?.GetValue (null)));
4545
}
4646
}
4747
catch (TargetInvocationException tie)
@@ -82,9 +82,9 @@ public bool Apply ()
8282
/// <returns></returns>
8383
public static string GetJsonPropertyName (PropertyInfo pi)
8484
{
85-
var jpna = pi.GetCustomAttribute (typeof (JsonPropertyNameAttribute)) as JsonPropertyNameAttribute;
85+
var attr = pi.GetCustomAttribute (typeof (JsonPropertyNameAttribute)) as JsonPropertyNameAttribute;
8686

87-
return jpna?.Name ?? pi.Name;
87+
return attr?.Name ?? pi.Name;
8888
}
8989

9090
/// <summary>
@@ -118,7 +118,7 @@ public static string GetJsonPropertyName (PropertyInfo pi)
118118

119119
if (PropertyValue is { })
120120
{
121-
PropertyValue = DeepMemberwiseCopy (source, PropertyValue);
121+
PropertyValue = DeepMemberWiseCopy (source, PropertyValue);
122122
}
123123
else
124124
{

Terminal.Gui/Configuration/ConfigurationManager.cs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
global using CM = Terminal.Gui.ConfigurationManager;
33
using System.Collections;
44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Reflection;
67
using System.Text.Encodings.Web;
78
using System.Text.Json;
@@ -33,19 +34,19 @@ namespace Terminal.Gui;
3334
/// Settings are applied using the following precedence (higher precedence settings overwrite lower precedence
3435
/// settings):
3536
/// <para>
36-
/// 1. Application configuration found in the users's home directory (<c>~/.tui/appname.config.json</c>) --
37+
/// 1. Application configuration found in the users' home directory (<c>~/.tui/appname.config.json</c>) --
3738
/// Highest precedence
3839
/// </para>
3940
/// <para>
4041
/// 2. Application configuration found in the directory the app was launched from (
4142
/// <c>./.tui/appname.config.json</c>).
4243
/// </para>
43-
/// <para>3. Application configuration found in the applications's resources (<c>Resources/config.json</c>).</para>
44+
/// <para>3. Application configuration found in the applications' resources (<c>Resources/config.json</c>).</para>
4445
/// <para>4. Global configuration found in the user's home directory (<c>~/.tui/config.json</c>).</para>
4546
/// <para>5. Global configuration found in the directory the app was launched from (<c>./.tui/config.json</c>).</para>
4647
/// <para>
4748
/// 6. Global configuration in <c>Terminal.Gui.dll</c>'s resources (<c>Terminal.Gui.Resources.config.json</c>) --
48-
/// Lowest Precidence.
49+
/// Lowest Precedence.
4950
/// </para>
5051
/// </summary>
5152
public static class ConfigurationManager
@@ -82,8 +83,10 @@ public enum ConfigLocations
8283
/// <see cref="ConfigurationManager"/> to get and set the property's value.
8384
/// </summary>
8485
/// <remarks>Is <see langword="null"/> until <see cref="Initialize"/> is called.</remarks>
86+
[SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
8587
internal static Dictionary<string, ConfigProperty>? _allConfigProperties;
8688

89+
[SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
8790
internal static readonly JsonSerializerOptions _serializerOptions = new ()
8891
{
8992
ReadCommentHandling = JsonCommentHandling.Skip,
@@ -104,8 +107,10 @@ public enum ConfigLocations
104107
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
105108
};
106109

107-
internal static StringBuilder jsonErrors = new ();
110+
[SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
111+
internal static StringBuilder _jsonErrors = new ();
108112

113+
[SuppressMessage ("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
109114
private static readonly string _configFilename = "config.json";
110115

111116
/// <summary>The backing property for <see cref="Settings"/>.</summary>
@@ -280,7 +285,7 @@ public static void Load (bool reset = false)
280285
public static void OnApplied ()
281286
{
282287
Debug.WriteLine ("ConfigurationManager.OnApplied()");
283-
Applied?.Invoke (null, new ConfigurationManagerEventArgs ());
288+
Applied?.Invoke (null, new ());
284289

285290
// TODO: Refactor ConfigurationManager to not use an event handler for this.
286291
// Instead, have it call a method on any class appropriately attributed
@@ -294,18 +299,18 @@ public static void OnApplied ()
294299
public static void OnUpdated ()
295300
{
296301
Debug.WriteLine (@"ConfigurationManager.OnApplied()");
297-
Updated?.Invoke (null, new ConfigurationManagerEventArgs ());
302+
Updated?.Invoke (null, new ());
298303
}
299304

300305
/// <summary>Prints any Json deserialization errors that occurred during deserialization to the console.</summary>
301306
public static void PrintJsonErrors ()
302307
{
303-
if (jsonErrors.Length > 0)
308+
if (_jsonErrors.Length > 0)
304309
{
305310
Console.WriteLine (
306311
@"Terminal.Gui ConfigurationManager encountered the following errors while deserializing configuration files:"
307312
);
308-
Console.WriteLine (jsonErrors.ToString ());
313+
Console.WriteLine (_jsonErrors.ToString ());
309314
}
310315
}
311316

@@ -326,9 +331,9 @@ public static void Reset ()
326331

327332
ClearJsonErrors ();
328333

329-
Settings = new SettingsScope ();
334+
Settings = new ();
330335
ThemeManager.Reset ();
331-
AppSettings = new AppScope ();
336+
AppSettings = new ();
332337

333338
// To enable some unit tests, we only load from resources if the flag is set
334339
if (Locations.HasFlag (ConfigLocations.DefaultOnly))
@@ -350,23 +355,20 @@ public static void Reset ()
350355
internal static void AddJsonError (string error)
351356
{
352357
Debug.WriteLine ($"ConfigurationManager: {error}");
353-
jsonErrors.AppendLine (error);
358+
_jsonErrors.AppendLine (error);
354359
}
355360

356361
/// <summary>
357362
/// System.Text.Json does not support copying a deserialized object to an existing instance. To work around this,
358-
/// we implement a 'deep, memberwise copy' method.
363+
/// we implement a 'deep, member-wise copy' method.
359364
/// </summary>
360365
/// <remarks>TOOD: When System.Text.Json implements `PopulateObject` revisit https://github.com/dotnet/corefx/issues/37627</remarks>
361366
/// <param name="source"></param>
362367
/// <param name="destination"></param>
363368
/// <returns><paramref name="destination"/> updated from <paramref name="source"/></returns>
364-
internal static object? DeepMemberwiseCopy (object? source, object? destination)
369+
internal static object? DeepMemberWiseCopy (object? source, object? destination)
365370
{
366-
if (destination is null)
367-
{
368-
throw new ArgumentNullException (nameof (destination));
369-
}
371+
ArgumentNullException.ThrowIfNull (destination);
370372

371373
if (source is null)
372374
{
@@ -406,7 +408,7 @@ internal static void AddJsonError (string error)
406408
if (((IDictionary)destination).Contains (srcKey))
407409
{
408410
((IDictionary)destination) [srcKey] =
409-
DeepMemberwiseCopy (((IDictionary)source) [srcKey], ((IDictionary)destination) [srcKey]);
411+
DeepMemberWiseCopy (((IDictionary)source) [srcKey], ((IDictionary)destination) [srcKey]);
410412
}
411413
else
412414
{
@@ -438,7 +440,7 @@ where destProp.CanWrite
438440
if (destVal is { })
439441
{
440442
// Recurse
441-
destProp.SetValue (destination, DeepMemberwiseCopy (sourceVal, destVal));
443+
destProp.SetValue (destination, DeepMemberWiseCopy (sourceVal, destVal));
442444
}
443445
else
444446
{
@@ -478,7 +480,7 @@ internal static void GetHardCodedDefaults ()
478480
throw new InvalidOperationException ("Initialize must be called first.");
479481
}
480482

481-
Settings = new SettingsScope ();
483+
Settings = new ();
482484
ThemeManager.GetHardCodedDefaults ();
483485
AppSettings?.RetrieveValues ();
484486

@@ -494,7 +496,7 @@ internal static void GetHardCodedDefaults ()
494496
/// </summary>
495497
internal static void Initialize ()
496498
{
497-
_allConfigProperties = new Dictionary<string, ConfigProperty> ();
499+
_allConfigProperties = new ();
498500
_settings = null;
499501

500502
Dictionary<string, Type> classesWithConfigProps = new (StringComparer.InvariantCultureIgnoreCase);
@@ -549,18 +551,18 @@ from p in enumerable
549551
scp.OmitClassName
550552
? ConfigProperty.GetJsonPropertyName (p)
551553
: $"{p.DeclaringType?.Name}.{p.Name}",
552-
new ConfigProperty { PropertyInfo = p, PropertyValue = null }
554+
new() { PropertyInfo = p, PropertyValue = null }
553555
);
554556
}
555557
else
556558
{
557-
throw new Exception (
558-
$"Property {
559-
p.Name
560-
} in class {
561-
p.DeclaringType?.Name
562-
} is not static. All SerializableConfigurationProperty properties must be static."
563-
);
559+
throw new (
560+
$"Property {
561+
p.Name
562+
} in class {
563+
p.DeclaringType?.Name
564+
} is not static. All SerializableConfigurationProperty properties must be static."
565+
);
564566
}
565567
}
566568
}
@@ -576,7 +578,7 @@ from p in enumerable
576578

577579
//_allConfigProperties.ToList ().ForEach (x => Debug.WriteLine ($" Property: {x.Key}"));
578580

579-
AppSettings = new AppScope ();
581+
AppSettings = new ();
580582
}
581583

582584
/// <summary>Creates a JSON document with the configuration specified.</summary>
@@ -602,5 +604,5 @@ internal static Stream ToStream ()
602604
return stream;
603605
}
604606

605-
private static void ClearJsonErrors () { jsonErrors.Clear (); }
607+
private static void ClearJsonErrors () { _jsonErrors.Clear (); }
606608
}

Terminal.sln.DotSettings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@
383383
<s:Boolean x:Key="/Default/CodeStyle/EditorConfig/ShowEditorConfigStatusBarIndicator/@EntryValue">True</s:Boolean>
384384
<s:Boolean x:Key="/Default/CodeStyle/EditorConfig/SyncToVisualStudio/@EntryValue">True</s:Boolean>
385385
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/ApplyAutoDetectedRules/@EntryValue">False</s:Boolean>
386+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
387+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PublicFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
388+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=StaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
386389
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
387390
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
388391
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>

0 commit comments

Comments
 (0)