-
-
Notifications
You must be signed in to change notification settings - Fork 444
[3.0] Improve handling of affixed names when singularizing (OES/SGIS issue) and expose NameAffix API #2535
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
base: develop/3.0
Are you sure you want to change the base?
[3.0] Improve handling of affixed names when singularizing (OES/SGIS issue) and expose NameAffix API #2535
Changes from all commits
89495d3
473f15c
affc583
a364ca3
255c4d0
0f1e510
f53ff21
710e2eb
94a8e3f
589792e
31323ed
0a25887
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,7 +128,7 @@ public async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default) | |
| // The dictionary containing mappings from the original type names to the new names of the type and its members | ||
| var newNames = new Dictionary<string, RenamedType>(); | ||
|
|
||
| var nameAffixer = new NameAffixer(visitor.AffixTypes, cfg.Affixes); | ||
| var nameAffixer = new PrettifyNamesAffixer(visitor.AffixTypes, cfg.Affixes); | ||
| var namePrettifier = new NamePrettifier(cfg.LongAcronymThreshold); | ||
|
|
||
| // Trim the trimmable names if the trimmer baseline is set | ||
|
|
@@ -463,7 +463,7 @@ private string ApplyPrettifyOnlyPipeline( | |
| string? container, | ||
| string name, | ||
| Dictionary<string, string> nameOverrides, | ||
| NameAffixer nameAffixer, | ||
| PrettifyNamesAffixer nameAffixer, | ||
| NamePrettifier namePrettifier | ||
| ) | ||
| { | ||
|
|
@@ -870,13 +870,6 @@ private record struct RenamedType( | |
| Dictionary<string, string> Functions | ||
| ); | ||
|
|
||
| private record struct NameAffix( | ||
| bool IsPrefix, | ||
| string Category, | ||
| string Affix, | ||
| int DeclarationOrder | ||
|
Comment on lines
-873
to
-877
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This been moved to a separate file as a public type. |
||
| ); | ||
|
|
||
| private record struct TypeData(List<string> NonFunctions, List<FunctionData> Functions); | ||
|
|
||
| private record struct FunctionData(string Name, MethodDeclarationSyntax Syntax); | ||
|
|
@@ -958,52 +951,13 @@ _typeInProgress is not null | |
| || _enumInProgress is not null | ||
| || node.Ancestors().OfType<BaseTypeDeclarationSyntax>().Any(); | ||
|
|
||
| private bool TryGetAffixData( | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced by |
||
| SyntaxList<AttributeListSyntax> attributeLists, | ||
| out NameAffix[] affixes | ||
| ) | ||
| { | ||
| affixes = []; | ||
| var declarationOrder = 0; | ||
| foreach (var list in attributeLists) | ||
| { | ||
| foreach (var attribute in list.Attributes) | ||
| { | ||
| if (!attribute.IsAttribute("Silk.NET.Core.NameAffix")) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var argumentList = attribute.ArgumentList; | ||
| if ( | ||
| argumentList != null | ||
| && argumentList.Arguments[0].Expression | ||
| is LiteralExpressionSyntax { Token.Value: string type } | ||
| && argumentList.Arguments[1].Expression | ||
| is LiteralExpressionSyntax { Token.Value: string category } | ||
| && argumentList.Arguments[2].Expression | ||
| is LiteralExpressionSyntax { Token.Value: string affix } | ||
| ) | ||
| { | ||
| affixes = | ||
| [ | ||
| .. affixes, | ||
| new NameAffix(type == "Prefix", category, affix, declarationOrder), | ||
| ]; | ||
| declarationOrder++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return affixes.Length != 0; | ||
| } | ||
|
|
||
| private void ReportTypeAffixData( | ||
| string typeIdentifier, | ||
| SyntaxList<AttributeListSyntax> attributeLists | ||
| ) | ||
| { | ||
| if (!TryGetAffixData(attributeLists, out var affixes)) | ||
| var affixes = attributeLists.GetNameAffixes(); | ||
| if (affixes.Length == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
@@ -1025,7 +979,8 @@ private void ReportMemberAffixData( | |
| SyntaxList<AttributeListSyntax> attributeLists | ||
| ) | ||
| { | ||
| if (!TryGetAffixData(attributeLists, out var affixData)) | ||
| var affixes = attributeLists.GetNameAffixes(); | ||
| if (affixes.Length == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
@@ -1037,7 +992,7 @@ SyntaxList<AttributeListSyntax> attributeLists | |
|
|
||
| // Note that TryAdd will lead to affixes for later members being silently dropped. | ||
| // This is to handle methods which have the same name and affixes. It is fine to drop the affixes in this case. | ||
| (typeAffixData.MemberAffixes ??= []).TryAdd(memberIdentifier, affixData); | ||
| (typeAffixData.MemberAffixes ??= []).TryAdd(memberIdentifier, affixes); | ||
| AffixTypes[typeIdentifier] = typeAffixData; | ||
| } | ||
|
|
||
|
|
@@ -1286,7 +1241,7 @@ public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) | |
|
|
||
| /// <param name="affixTypes">The affix data retrieved by the <see cref="Visitor"/>.</param> | ||
| /// <param name="config">The configuration from <see cref="Configuration.Affixes"/>.</param> | ||
| private class NameAffixer( | ||
| private class PrettifyNamesAffixer( | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class is the |
||
| Dictionary<string, TypeAffixData> affixTypes, | ||
| Dictionary<string, NameAffixConfiguration> config | ||
| ) | ||
|
|
@@ -1317,61 +1272,13 @@ public string RemoveAffixes( | |
| return primary; | ||
| } | ||
|
|
||
| var originalPrimary = primary; | ||
|
|
||
| // Sort affixes so that the outer affixes are first | ||
| affixes.Sort( | ||
| static (a, b) => | ||
| { | ||
| // Sort by descending declaration order | ||
| // Lower declaration order means the affix is closer to the inside of the name | ||
| return -a.DeclarationOrder.CompareTo(b.DeclarationOrder); | ||
| } | ||
| ); | ||
|
|
||
| var prefixes = affixes.Where(x => x.IsPrefix).ToList(); | ||
| var suffixes = affixes.Where(x => !x.IsPrefix).ToList(); | ||
|
|
||
| RemoveSide(true, prefixes); | ||
| RemoveSide(false, suffixes); | ||
|
|
||
| if (originalPrimary != primary) | ||
| var stripped = NameAffixer.StripAffixes(primary, affixes); | ||
| if (stripped != primary) | ||
| { | ||
| secondary?.Add(originalPrimary); | ||
| secondary?.Add(primary); | ||
| } | ||
|
|
||
| return primary; | ||
|
|
||
| void RemoveSide(bool isPrefix, List<NameAffix> nameAffixes) | ||
| { | ||
| while (nameAffixes.Count > 0) | ||
| { | ||
| var removedAffix = false; | ||
| for (var i = 0; i < nameAffixes.Count; i++) | ||
| { | ||
| var affix = nameAffixes[i]; | ||
| if ( | ||
| isPrefix | ||
| ? primary.StartsWith(affix.Affix) | ||
| : primary.EndsWith(affix.Affix) | ||
| ) | ||
| { | ||
| primary = isPrefix | ||
| ? primary[affix.Affix.Length..] | ||
| : primary[..^affix.Affix.Length]; | ||
|
|
||
| nameAffixes.RemoveAt(i); | ||
| removedAffix = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!removedAffix) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return stripped; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -1479,7 +1386,7 @@ void CreateName(string name, Span<NameAffix> currentAffixes) | |
| { | ||
| if (!GetConfiguration(affix).Remove) | ||
| { | ||
| if (affix.IsPrefix) | ||
| if (affix.Type == NameAffixType.Prefix) | ||
| { | ||
| name = affix.Affix + name; | ||
| } | ||
|
|
@@ -1544,7 +1451,7 @@ private NameAffixConfiguration GetConfiguration(string category) => | |
| /// Removes identified affixes so that other trimmers can process the base name separately. | ||
| /// These affixes should be reapplied by <see cref="NameAffixerLateTrimmer"/>. | ||
| /// </summary> | ||
| private class NameAffixerEarlyTrimmer(NameAffixer affixer) : INameTrimmer | ||
| private class NameAffixerEarlyTrimmer(PrettifyNamesAffixer affixer) : INameTrimmer | ||
| { | ||
| /// <inheritdoc/> | ||
| public Version Version => new(0, 0, 0); | ||
|
|
@@ -1583,7 +1490,7 @@ public void Trim(NameTrimmerContext context) | |
| /// <summary> | ||
| /// Reapplies and transforms identified affixes based on <see cref="NameAffixConfiguration"/>. | ||
| /// </summary> | ||
| private class NameAffixerLateTrimmer(NameAffixer affixer) : INameTrimmer | ||
| private class NameAffixerLateTrimmer(PrettifyNamesAffixer affixer) : INameTrimmer | ||
| { | ||
| /// <inheritdoc/> | ||
| public Version Version => new(0, 0, 0); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved these to the new
NameAffixerstatic class to keep everything related to name affixes together.I also changed the API to use the new
NameAffixTypeenum and changed it so that we only have theAddNameAffixmethod instead ofAddNamePrefix/Suffixseparately.