SyntaxTreeExtensions.cs 1019 B

1234567891011121314151617181920212223
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using Microsoft.CodeAnalysis;
  4. using Microsoft.CodeAnalysis.Text;
  5. internal static class SyntaxTreeExtensions
  6. {
  7. // Utilize the same logic used by the `CallerLinePathAttribute` for generating
  8. // a file path for a given syntax tree.
  9. // Source copied from https://github.com/dotnet/roslyn/blob/5b47c7fe326faa35940f220c14f718cd0b820c38/src/Compilers/Core/Portable/Syntax/SyntaxTree.cs#L274-L293 until
  10. // public APIs are available.
  11. internal static string GetDisplayPath(this SyntaxTree tree, TextSpan span, SourceReferenceResolver? resolver)
  12. {
  13. var mappedSpan = tree.GetMappedLineSpan(span);
  14. if (resolver == null || mappedSpan.Path.Length == 0)
  15. {
  16. return mappedSpan.Path;
  17. }
  18. return resolver.NormalizePath(mappedSpan.Path, baseFilePath: mappedSpan.HasMappedPath ? tree.FilePath : null) ?? mappedSpan.Path;
  19. }
  20. }