Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ public CSharpDecompiledSourceService()
{
}

public async Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, MetadataReference metadataReference, string assemblyLocation, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
public async Task<Document?> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, MetadataReference? metadataReference, string? assemblyLocation, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
{
// Get the name of the type the symbol is in
var containingOrThis = symbol.GetContainingTypeOrThis();
var fullName = GetFullReflectionName(containingOrThis);

// Decompile
document = PerformDecompilation(document, fullName, symbolCompilation, metadataReference, assemblyLocation);
var decompiledDocument = PerformDecompilation(document, fullName, symbolCompilation, metadataReference, assemblyLocation);

if (decompiledDocument is null)
return null;

document = decompiledDocument;

document = await AddAssemblyInfoRegionAsync(document, symbol, cancellationToken).ConfigureAwait(false);

Expand All @@ -74,7 +79,7 @@ public static async Task<Document> FormatDocumentAsync(Document document, Syntax
return formattedDoc;
}

private static Document PerformDecompilation(Document document, string fullName, Compilation compilation, MetadataReference? metadataReference, string assemblyLocation)
private static Document? PerformDecompilation(Document document, string fullName, Compilation compilation, MetadataReference? metadataReference, string? assemblyLocation)
{
var logger = new StringBuilder();
var resolver = new AssemblyResolver(compilation, logger);
Expand All @@ -84,12 +89,11 @@ private static Document PerformDecompilation(Document document, string fullName,
if (metadataReference is not null)
file = resolver.TryResolve(metadataReference, PEStreamOptions.PrefetchEntireImage);

if (file is null && assemblyLocation is null)
{
throw new NotSupportedException(FeaturesResources.Cannot_navigate_to_the_symbol_under_the_caret);
}
if (file is null && assemblyLocation is not null)
file = new PEFile(assemblyLocation, PEStreamOptions.PrefetchEntireImage);

file ??= new PEFile(assemblyLocation, PEStreamOptions.PrefetchEntireImage);
if (file is null)
return null;

// Initialize a decompiler with default settings.
var decompiler = new CSharpDecompiler(file, resolver, new DecompilerSettings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#nullable disable

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Formatting;
Expand All @@ -24,7 +22,7 @@ internal interface IDecompiledSourceService : ILanguageService
/// <param name="metadataReference">The reference that contains the symbol</param>
/// <param name="assemblyLocation">The location of the implementation assembly to decompile</param>
/// <param name="cancellationToken">To cancel document operations</param>
/// <returns>The updated document</returns>
Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, MetadataReference metadataReference, string assemblyLocation, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken);
/// <returns>The updated document, or null if the decompilation could not be performed</returns>
Task<Document?> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, MetadataReference? metadataReference, string? assemblyLocation, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ public DecompilationMetadataAsSourceFileProvider(IImplementationAssemblyLookupSe

if (decompiledSourceService != null)
{
temporaryDocument = await decompiledSourceService.AddSourceToAsync(temporaryDocument, compilation, symbol, refInfo.metadataReference, refInfo.assemblyLocation, options.GenerationOptions.CleanupOptions.FormattingOptions, cancellationToken).ConfigureAwait(false);
var decompilationDocument = await decompiledSourceService.AddSourceToAsync(temporaryDocument, compilation, symbol, refInfo.metadataReference, refInfo.assemblyLocation, options.GenerationOptions.CleanupOptions.FormattingOptions, cancellationToken).ConfigureAwait(false);
if (decompilationDocument is not null)
{
temporaryDocument = decompilationDocument;
}
else
{
useDecompiler = false;
}
}
else
{
Expand Down