|
16 | 16 | using Microsoft.CodeAnalysis.FindSymbols; |
17 | 17 | using Microsoft.Extensions.Logging; |
18 | 18 | using Silk.NET.SilkTouch.Mods; |
| 19 | +using Silk.NET.SilkTouch.Mods.LocationTransformation; |
19 | 20 | using Project = Microsoft.CodeAnalysis.Project; |
20 | 21 |
|
21 | 22 | namespace Silk.NET.SilkTouch.Naming; |
@@ -348,116 +349,9 @@ public static async Task RenameAllAsync( |
348 | 349 | bool includeCandidateLocations = false |
349 | 350 | ) |
350 | 351 | { |
351 | | - if (ctx.SourceProject is null) |
352 | | - { |
353 | | - return; |
354 | | - } |
355 | | - |
356 | | - var locations = new ConcurrentDictionary<Location, string>(); |
357 | | - // TODO this needs parallelisation config & be sensitive to the environment (future src generator form factor?) |
358 | | - await Parallel.ForEachAsync( |
359 | | - toRename, |
360 | | - ct, |
361 | | - async (tuple, _) => |
362 | | - { |
363 | | - // First, let's add all of the locations of the declaration identifiers. |
364 | | - var (symbol, newName) = tuple; |
365 | | - if (includeDeclarations) |
366 | | - { |
367 | | - foreach (var syntaxRef in symbol.DeclaringSyntaxReferences) |
368 | | - { |
369 | | - var identifierLocation = IdentifierLocation( |
370 | | - await syntaxRef.GetSyntaxAsync(ct) |
371 | | - ); |
372 | | - if (identifierLocation is not null) |
373 | | - { |
374 | | - locations.TryAdd(identifierLocation, newName); |
375 | | - } |
376 | | - } |
377 | | - } |
378 | | - |
379 | | - // Next, let's find all the references of the symbols. |
380 | | - var references = await SymbolFinder.FindReferencesAsync( |
381 | | - symbol, |
382 | | - ctx.SourceProject?.Solution |
383 | | - ?? throw new ArgumentException("SourceProject is null"), |
384 | | - ct |
385 | | - ); |
386 | | - |
387 | | - foreach (var referencedSymbol in references) |
388 | | - { |
389 | | - foreach (var referencedSymbolLocation in referencedSymbol.Locations) |
390 | | - { |
391 | | - if ( |
392 | | - !includeCandidateLocations |
393 | | - && ( |
394 | | - referencedSymbolLocation.IsCandidateLocation |
395 | | - || referencedSymbolLocation.IsImplicit |
396 | | - ) |
397 | | - ) |
398 | | - { |
399 | | - continue; |
400 | | - } |
401 | | - |
402 | | - locations.TryAdd(referencedSymbolLocation.Location, newName); |
403 | | - } |
404 | | - } |
405 | | - } |
406 | | - ); |
407 | | - |
408 | | - logger.LogDebug("{} referencing locations for renames for {}", locations.Count, ctx.JobKey); |
409 | | - |
410 | | - // Now it's just a simple find and replace. |
411 | | - var sln = ctx.SourceProject.Solution; |
412 | | - var srcProjId = ctx.SourceProject.Id; |
413 | | - var testProjId = ctx.TestProject?.Id; |
414 | | - foreach ( |
415 | | - var (syntaxTree, renameLocations) in locations |
416 | | - .GroupBy(x => x.Key.SourceTree) |
417 | | - .Select(x => (x.Key, x.OrderByDescending(y => y.Key.SourceSpan))) |
418 | | - ) |
419 | | - { |
420 | | - if ( |
421 | | - syntaxTree is null |
422 | | - || sln.GetDocument(syntaxTree) is not { } doc |
423 | | - || (doc.Project.Id != srcProjId && doc.Project.Id != testProjId) |
424 | | - || await syntaxTree.GetTextAsync(ct) is not { } text |
425 | | - ) |
426 | | - { |
427 | | - continue; |
428 | | - } |
429 | | - |
430 | | - var ogText = text; |
431 | | - foreach (var (location, newName) in renameLocations) |
432 | | - { |
433 | | - var contents = ogText.GetSubText(location.SourceSpan).ToString(); |
434 | | - if (contents.Contains(' ')) |
435 | | - { |
436 | | - logger.LogWarning( |
437 | | - "Refusing to do unsafe rename/replacement of \"{}\" to \"{}\" at {}", |
438 | | - contents, |
439 | | - newName, |
440 | | - location.GetLineSpan() |
441 | | - ); |
442 | | - continue; |
443 | | - } |
444 | | - |
445 | | - if (logger.IsEnabled(LogLevel.Trace)) |
446 | | - { |
447 | | - logger.LogTrace( |
448 | | - "\"{}\" -> \"{}\" at {}", |
449 | | - contents, |
450 | | - newName, |
451 | | - location.GetLineSpan() |
452 | | - ); |
453 | | - } |
454 | | - |
455 | | - text = text.Replace(location.SourceSpan, newName); |
456 | | - } |
457 | | - |
458 | | - sln = doc.WithText(text).Project.Solution; |
459 | | - } |
460 | | - |
461 | | - ctx.SourceProject = sln.GetProject(srcProjId); |
| 352 | + var toRenameList = toRename.ToList(); |
| 353 | + await LocationTransformationUtils.ModifyAllReferencesAsync(ctx, logger, toRenameList.Select(t => t.Symbol), [ |
| 354 | + new IdentifierRenamingTransformer(toRenameList) |
| 355 | + ], ct); |
462 | 356 | } |
463 | 357 | } |
0 commit comments