Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Allow mods to be name trimmers to (needed later for OGL)
Browse files Browse the repository at this point in the history
  • Loading branch information
Perksey committed Nov 11, 2023
1 parent 8c83c28 commit 72904b7
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions sources/SilkTouchX/Mods/PrettifyNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ namespace SilkTouchX.Mods;
/// <summary>
/// A mod that will convert other naming conventions to the PascalCase nomenclature typically used in C#.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="config">Configuration snapshot.</param>
/// <param name="trimmers">Name trimmers.</param>
/// <param name="otherMods">Other mods present in the generator.</param>
/// <param name="jobConfig">The general job configuration.</param>
[ModConfiguration<Configuration>]
public class PrettifyNames : IMod
public class PrettifyNames(
ILogger<PrettifyNames> logger,
IOptionsSnapshot<PrettifyNames.Configuration> config,
IEnumerable<INameTrimmer> trimmers,
IEnumerable<IMod> otherMods,
IOptionsSnapshot<SilkTouchConfiguration> jobConfig
) : IMod
{
private readonly ILogger<PrettifyNames> _logger;
private readonly IOptionsSnapshot<Configuration> _config;
private readonly IEnumerable<INameTrimmer> _trimmers;

/// <summary>
/// The configuration for the prettify names mod.
/// </summary>
Expand All @@ -46,18 +53,6 @@ public class PrettifyNames : IMod
public string? GlobalPrefixHint { get; init; }
}

/// <summary>
/// Creates an instance of this mod.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="config">Configuration snapshot.</param>
/// <param name="trimmers">Name trimmers.</param>
public PrettifyNames(
ILogger<PrettifyNames> logger,
IOptionsSnapshot<Configuration> config,
IEnumerable<INameTrimmer> trimmers
) => (_logger, _config, _trimmers) = (logger, config, trimmers);

/// <inheritdoc />
public Task<GeneratedSyntax> AfterScrapeAsync(string key, GeneratedSyntax syntax)
{
Expand All @@ -68,7 +63,7 @@ public Task<GeneratedSyntax> AfterScrapeAsync(string key, GeneratedSyntax syntax
}

var rewriter = new Rewriter();
var cfg = _config.Get(key);
var cfg = config.Get(key);
if (cfg.TrimmerBaseline is not null)
{
var typeNames = visitor.Types.ToDictionary(
Expand All @@ -78,9 +73,15 @@ public Task<GeneratedSyntax> AfterScrapeAsync(string key, GeneratedSyntax syntax
if (typeNames.Count > 1 || cfg.GlobalPrefixHint is not null)
{
foreach (
var trimmer in _trimmers
.Where(x => x.Version >= cfg.TrimmerBaseline)
.OrderBy(x => x.Version)
var trimmer in (
jobConfig.Value.Mods
?.Select(x => otherMods.First(y => y.GetType().Name == x))
.OfType<INameTrimmer>() ?? Enumerable.Empty<INameTrimmer>()
).Concat(
trimmers
.Where(x => x.Version >= cfg.TrimmerBaseline)
.OrderBy(x => x.Version)
)
)
{
trimmer.Trim(null, cfg.GlobalPrefixHint, typeNames, cfg.PrefixOverrides);
Expand All @@ -97,7 +98,7 @@ var trimmer in _trimmers
if (constNames is not null)
{
foreach (
var trimmer in _trimmers
var trimmer in trimmers
.Where(x => x.Version >= cfg.TrimmerBaseline)
.OrderBy(x => x.Version)
)
Expand All @@ -122,7 +123,7 @@ var trimmer in _trimmers
if (functionNames is not null)
{
foreach (
var trimmer in _trimmers
var trimmer in trimmers
.Where(x => x.Version >= cfg.TrimmerBaseline)
.OrderBy(x => x.Version)
)
Expand Down Expand Up @@ -166,14 +167,14 @@ var trimmer in _trimmers

foreach (var (name, (newName, nonFunctions, functions)) in rewriter.Types)
{
_logger.LogDebug("{} = {}", name, newName);
logger.LogDebug("{} = {}", name, newName);
foreach (var (old, @new) in nonFunctions ?? new())
{
_logger.LogDebug("{}.{} = {}.{}", name, old, newName, @new);
logger.LogDebug("{}.{} = {}.{}", name, old, newName, @new);
}
foreach (var (old, @new) in functions ?? new())
{
_logger.LogDebug("{}.{} = {}.{}", name, old, newName, @new);
logger.LogDebug("{}.{} = {}.{}", name, old, newName, @new);
}
}

Expand Down

0 comments on commit 72904b7

Please sign in to comment.