Skip to content

Commit 43a2336

Browse files
committed
Backport PrettifyNames changes from Curin's Microsoft branch
Technically this is from my update PR into Curin's branch (curin#101), but is effectively the same as backporting the changes from Curin's branch. Exact commit that I copied the changes from: 0cc698a
1 parent 553d33f commit 43a2336

File tree

1 file changed

+99
-15
lines changed

1 file changed

+99
-15
lines changed

sources/SilkTouch/SilkTouch/Mods/PrettifyNames.cs

Lines changed: 99 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,17 @@ functions is null || functionNames is null
190190
var prettifiedOnly = visitor.PrettifyOnlyTypes.TryGetValue(typeName, out var val)
191191
? val.Select(x => new KeyValuePair<string, (string Primary, List<string>?)>(
192192
x,
193-
(x, null)
193+
(GetOverriddenName(typeName, x, cfg.NameOverrides!, translator), null)
194194
))
195195
: [];
196196

197197
// Add it to the rewriter's list of names to... rewrite...
198198
types[typeName] = (
199199
newTypeName.Prettify(translator, allowAllCaps: true), // <-- lenient about caps for type names
200-
// TODO deprecate secondaries if they're within the baseline?
201-
constNames
202-
.Concat(prettifiedOnly)
203-
.ToDictionary(x => x.Key, x => x.Value.Primary.Prettify(translator)),
200+
// TODO deprecate secondaries if they're within the baseline?
201+
constNames.Select(x => new KeyValuePair<string, (string Primary, List<string>?)>(x.Key, (x.Value.Primary.Prettify(translator), x.Value.Item2)))
202+
.Concat(prettifiedOnly.DistinctBy(kvp => kvp.Key).ToDictionary())
203+
.ToDictionary(x => x.Key, x => x.Value.Primary),
204204
functionNames?.ToDictionary(
205205
x => x.Key,
206206
x => x.Value.Primary.Prettify(translator)
@@ -215,9 +215,9 @@ functions is null || functionNames is null
215215
foreach (var (name, (nonFunctions, functions, isEnum)) in visitor.Types)
216216
{
217217
types[name] = (
218-
name.Prettify(translator, allowAllCaps: true), // <-- lenient about caps for type names (e.g. GL)
219-
nonFunctions?.ToDictionary(x => x, x => x.Prettify(translator)),
220-
functions?.ToDictionary(x => x.Name, x => x.Name.Prettify(translator)),
218+
GetOverriddenName(null, name, cfg.NameOverrides!, translator, true), // <-- lenient about caps for type names (e.g. GL)
219+
nonFunctions?.ToDictionary(x => x, x => GetOverriddenName(name, x, cfg.NameOverrides!, translator)),
220+
functions?.ToDictionary(x => x.Name, x => GetOverriddenName(name, x.Name, cfg.NameOverrides!, translator)),
221221
isEnum
222222
);
223223
}
@@ -263,11 +263,13 @@ functions is null || functionNames is null
263263
var sw = Stopwatch.StartNew();
264264
logger.LogDebug("Discovering references to symbols to rename for {}...", ctx.JobKey);
265265
ctx.SourceProject = proj;
266+
266267
var comp =
267268
await proj.GetCompilationAsync(ct)
268269
?? throw new InvalidOperationException(
269270
"Failed to obtain compilation for source project!"
270271
);
272+
271273
await NameUtils.RenameAllAsync(
272274
ctx,
273275
types.SelectMany(x =>
@@ -311,6 +313,7 @@ z.MethodKind is MethodKind.Constructor or MethodKind.Destructor
311313
logger,
312314
ct
313315
);
316+
314317
logger.LogDebug(
315318
"Reference renaming took {} seconds for {}.",
316319
sw.Elapsed.TotalSeconds,
@@ -333,12 +336,86 @@ z.MethodKind is MethodKind.Constructor or MethodKind.Destructor
333336
continue;
334337
}
335338

336-
proj = doc.ReplaceNameAndPath(oldName, newName).Project;
339+
var originalName = doc.Name;
340+
doc = doc.ReplaceNameAndPath(oldName, newName);
341+
342+
var found = false;
343+
if (doc.FilePath is not null)
344+
{
345+
foreach (var checkDocId in proj.DocumentIds)
346+
{
347+
if (checkDocId == docId)
348+
continue;
349+
350+
var checkDoc = proj.GetDocument(checkDocId);
351+
352+
if (checkDoc is null ||
353+
checkDoc.FilePath is null)
354+
continue;
355+
356+
if (checkDoc.FilePath == doc.FilePath)
357+
{
358+
found = true;
359+
break;
360+
}
361+
}
362+
}
363+
364+
365+
if (found)
366+
{
367+
logger.LogError($"{originalName} -> {doc.Name} failed to rename file as a file already exists at {doc.FilePath}");
368+
}
369+
else
370+
proj = doc.Project;
337371
}
338372

339373
ctx.SourceProject = proj;
340374
}
341375

376+
private string GetOverriddenName(
377+
string? container,
378+
string name,
379+
Dictionary<string, string>? nameOverrides,
380+
NameUtils.NameTransformer translator,
381+
bool allowAllCaps = false)
382+
{
383+
foreach (
384+
var (nativeName, overriddenName) in nameOverrides
385+
?? Enumerable.Empty<KeyValuePair<string, string>>()
386+
)
387+
{
388+
var nameToAdd = nativeName;
389+
if (nativeName.Contains('.'))
390+
{
391+
// We're processing a type dictionary, so don't add a member thing.
392+
if (container is null)
393+
{
394+
continue;
395+
}
396+
397+
// Check whether the override is for this type.
398+
var span = nativeName.AsSpan();
399+
var containerSpan = span[..span.IndexOf('.')];
400+
if (!containerSpan.Equals("*", StringComparison.Ordinal) && !containerSpan.Equals(container, StringComparison.Ordinal))
401+
{
402+
continue;
403+
}
404+
405+
nameToAdd = span[(span.IndexOf('.') + 1)..].ToString();
406+
if (nameToAdd == name)
407+
{
408+
return overriddenName;
409+
}
410+
}
411+
else if (nativeName == name)
412+
{
413+
return overriddenName;
414+
}
415+
}
416+
return name.Prettify(translator, allowAllCaps);
417+
}
418+
342419
private void Trim(
343420
NameTrimmerContext context,
344421
IEnumerable<INameTrimmer> trimmers,
@@ -363,11 +440,9 @@ private void Trim(
363440
}
364441

365442
// Check whether the override is for this type.
366-
var span = context.Container.AsSpan();
367-
if (
368-
span[..span.IndexOf('.')] is "*"
369-
|| span[..span.IndexOf('.')] == context.Container
370-
)
443+
var span = nativeName.AsSpan();
444+
var containerSpan = span[..span.IndexOf('.')];
445+
if (containerSpan.Equals("*", StringComparison.Ordinal) || containerSpan.Equals(context.Container, StringComparison.Ordinal))
371446
{
372447
nameToAdd = span[(span.IndexOf('.') + 1)..].ToString();
373448
}
@@ -729,7 +804,7 @@ public override void VisitClassDeclaration(ClassDeclarationSyntax node)
729804

730805
// Merge with the other partials.
731806
(inner.NonFunctions ??= new List<string>()).AddRange(
732-
_classInProgress.Value.NonFunctions
807+
_classInProgress.Value.NonFunctions.Where(val => !inner.NonFunctions?.Contains(val) ?? true)
733808
);
734809
(inner.Functions ??= new List<(string, MethodDeclarationSyntax)>()).AddRange(
735810
_classInProgress.Value.Functions
@@ -921,6 +996,15 @@ public Task<List<ResponseFile>> BeforeScrapeAsync(string key, List<ResponseFile>
921996
key
922997
);
923998
}
999+
if (!responseFile.GeneratorConfiguration.DontUseUsingStaticsForGuidMember)
1000+
{
1001+
logger.LogWarning(
1002+
"{} (for {}) should use exclude-using-statics-for-guid-members as PrettifyNames does not resolve "
1003+
+ "conflicts with members of other types.",
1004+
responseFile.FilePath,
1005+
key
1006+
);
1007+
}
9241008
}
9251009

9261010
return Task.FromResult(rsps);

0 commit comments

Comments
 (0)