Skip to content

Commit

Permalink
🧹 Apply .editorconfig format rules
Browse files Browse the repository at this point in the history
Now that records is properly supported in 5.0 version of the
tool in use in CI, we can apply again.
  • Loading branch information
kzu committed Jan 30, 2021
1 parent 90b05a8 commit 1af5a25
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
84 changes: 42 additions & 42 deletions src/ThisAssembly.Constants/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,62 @@ record Model(Area RootArea)
record Area(string Name, string Prefix)
{
public List<Area> NestedAreas { get; init; } = new();
public List<Constant> Values { get; init; } = new();
public List<Constant> Values { get; init; } = new();

public static Area Load(List<Constant> constants, string rootArea = "Constants")
{
var root = new Area(rootArea, "");

foreach (var constant in constants)
public static Area Load(List<Constant> constants, string rootArea = "Constants")
{
// Splits: ([area].)*[name]
var parts = constant.Name.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length <= 1)
{
root.Values.Add(new Constant(constant.Name, constant.Value, constant.Comment));
}
else
var root = new Area(rootArea, "");

foreach (var constant in constants)
{
var area = GetArea(root, parts.Take(parts.Length - 1));
var value = new Constant(parts.Skip(parts.Length - 1).First(), constant.Value, constant.Comment);
// Splits: ([area].)*[name]
var parts = constant.Name.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length <= 1)
{
root.Values.Add(new Constant(constant.Name, constant.Value, constant.Comment));
}
else
{
var area = GetArea(root, parts.Take(parts.Length - 1));
var value = new Constant(parts.Skip(parts.Length - 1).First(), constant.Value, constant.Comment);

area.Values.Add(value);
area.Values.Add(value);
}
}
}

SortArea(root);
return root;
}
SortArea(root);
return root;
}

static void SortArea(Area area)
{
area.Values.Sort((left, right) => left.Name.CompareTo(right.Name));
foreach (var nested in area.NestedAreas)
SortArea(nested);
}
static void SortArea(Area area)
{
area.Values.Sort((left, right) => left.Name.CompareTo(right.Name));
foreach (var nested in area.NestedAreas)
SortArea(nested);
}

static Area GetArea(Area area, IEnumerable<string> areaPath)
{
var currentArea = area;
foreach (var areaName in areaPath)
static Area GetArea(Area area, IEnumerable<string> areaPath)
{
var existing = currentArea.NestedAreas.FirstOrDefault(a => a.Name == areaName);
if (existing == null)
var currentArea = area;
foreach (var areaName in areaPath)
{
if (currentArea.Values.Any(v => v.Name == areaName))
throw new ArgumentException(string.Format(
"Area name '{0}' is already in use as a value name under area '{1}'.",
areaName, currentArea.Name));
var existing = currentArea.NestedAreas.FirstOrDefault(a => a.Name == areaName);
if (existing == null)
{
if (currentArea.Values.Any(v => v.Name == areaName))
throw new ArgumentException(string.Format(
"Area name '{0}' is already in use as a value name under area '{1}'.",
areaName, currentArea.Name));

existing = new Area(areaName, currentArea.Prefix + areaName + ".");
currentArea.NestedAreas.Add(existing);
}

existing = new Area(areaName, currentArea.Prefix + areaName + ".");
currentArea.NestedAreas.Add(existing);
currentArea = existing;
}

currentArea = existing;
return currentArea;
}

return currentArea;
}
}

[DebuggerDisplay("{Name} = {Value}")]
Expand Down
16 changes: 8 additions & 8 deletions src/ThisAssembly.Strings/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ static ResourceValue GetValue(string resourceName, string resourceValue)
record ResourceArea(string Name, string Prefix)
{
public List<ResourceArea> NestedAreas { get; init; } = new List<ResourceArea>();
public List<ResourceValue> Values { get; init; } = new List<ResourceValue>();
public List<ResourceValue> Values { get; init; } = new List<ResourceValue>();
}

[DebuggerDisplay("{Name} = {Value}")]
record ResourceValue(string Name, string? Raw)
{
public string? Value => Raw?.Replace(Environment.NewLine, "")?.Replace("<", "&lt;")?.Replace(">", "&gt;");
public string? Comment { get; init; }
public bool HasFormat => Format != null && Format.Count > 0;
// We either have *all* named or all indexed. Can't mix. We'll skip generating
// methods for mixed ones and report as an analyzer error on the Resx.
public bool IsNamedFormat => HasFormat && Format.All(x => !int.TryParse(x, out _));
public bool IsIndexedFormat => HasFormat && Format.All(x => int.TryParse(x, out _));
public List<string> Format { get; } = new List<string>();
public string? Comment { get; init; }
public bool HasFormat => Format != null && Format.Count > 0;
// We either have *all* named or all indexed. Can't mix. We'll skip generating
// methods for mixed ones and report as an analyzer error on the Resx.
public bool IsNamedFormat => HasFormat && Format.All(x => !int.TryParse(x, out _));
public bool IsIndexedFormat => HasFormat && Format.All(x => int.TryParse(x, out _));
public List<string> Format { get; } = new List<string>();
}

0 comments on commit 1af5a25

Please sign in to comment.