Skip to content

Commit

Permalink
MetadataAsSource
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Mar 11, 2022
1 parent 0ab5959 commit 6c556e2
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal class CSharpDecompiledSourceService : IDecompiledSourceService
public CSharpDecompiledSourceService(HostLanguageServices provider)
=> this.provider = provider;

public async Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, CancellationToken cancellationToken)
public async Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
{
// Get the name of the type the symbol is in
var containingOrThis = symbol.GetContainingTypeOrThis();
Expand Down Expand Up @@ -75,13 +75,12 @@ public async Task<Document> AddSourceToAsync(Document document, Compilation symb
var docCommentFormattingService = document.GetLanguageService<IDocumentationCommentFormattingService>();
document = await ConvertDocCommentsToRegularCommentsAsync(document, docCommentFormattingService, cancellationToken).ConfigureAwait(false);

return await FormatDocumentAsync(document, cancellationToken).ConfigureAwait(false);
return await FormatDocumentAsync(document, formattingOptions, cancellationToken).ConfigureAwait(false);
}

public static async Task<Document> FormatDocumentAsync(Document document, CancellationToken cancellationToken)
public static async Task<Document> FormatDocumentAsync(Document document, SyntaxFormattingOptions options, CancellationToken cancellationToken)
{
var node = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var options = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);

// Apply formatting rules
var formattedDoc = await Formatter.FormatAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Editor.CSharp.DecompiledSource;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand Down Expand Up @@ -226,7 +227,7 @@ private static async Task TestAsync(string input, string expected)
using var workspace = TestWorkspace.CreateCSharp(input);
var document = workspace.CurrentSolution.Projects.Single().Documents.Single();

var formatted = await CSharpDecompiledSourceService.FormatDocumentAsync(document, CancellationToken.None);
var formatted = await CSharpDecompiledSourceService.FormatDocumentAsync(document, CSharpSyntaxFormattingOptions.Default, CancellationToken.None);
var test = await formatted.GetTextAsync();

AssertEx.Equal(expected, test.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;

namespace Microsoft.CodeAnalysis.DecompiledSource
Expand All @@ -22,6 +23,6 @@ internal interface IDecompiledSourceService : ILanguageService
/// <param name="symbol">The symbol to generate source for</param>
/// <param name="cancellationToken">To cancel document operations</param>
/// <returns>The updated document</returns>
Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, CancellationToken cancellationToken);
Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Microsoft.CodeAnalysis.MetadataAsSource
{
internal abstract partial class AbstractMetadataAsSourceService : IMetadataAsSourceService
{
public async Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, CancellationToken cancellationToken)
public async Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
{
if (document == null)
{
Expand Down Expand Up @@ -56,12 +56,11 @@ public async Task<Document> AddSourceToAsync(Document document, Compilation symb

var docWithAssemblyInfo = await AddAssemblyInfoRegionAsync(docWithDocComments, symbolCompilation, symbol.GetOriginalUnreducedDefinition(), cancellationToken).ConfigureAwait(false);
var node = await docWithAssemblyInfo.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var options = await SyntaxFormattingOptions.FromDocumentAsync(docWithAssemblyInfo, cancellationToken).ConfigureAwait(false);

var formattedDoc = await Formatter.FormatAsync(
docWithAssemblyInfo,
SpecializedCollections.SingletonEnumerable(node.FullSpan),
options,
formattingOptions,
GetFormattingRules(docWithAssemblyInfo),
cancellationToken).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.DecompiledSource;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -62,9 +63,9 @@ public DecompilationMetadataAsSourceFileProvider()
// avoid loading the actual file from disk since it doesn't exist yet.
var temporaryProjectInfoAndDocumentId = fileInfo.GetProjectInfoAndDocumentId(workspace, loadFileFromDisk: false);
var temporaryDocument = workspace.CurrentSolution.AddProject(temporaryProjectInfoAndDocumentId.Item1)
.GetDocument(temporaryProjectInfoAndDocumentId.Item2);
.GetRequiredDocument(temporaryProjectInfoAndDocumentId.Item2);

Contract.ThrowIfNull(temporaryDocument, "The temporary ProjectInfo didn't contain the document it said it would.");
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(temporaryDocument, cancellationToken).ConfigureAwait(false);

if (useDecompiler)
{
Expand All @@ -77,7 +78,7 @@ public DecompilationMetadataAsSourceFileProvider()

if (decompiledSourceService != null)
{
temporaryDocument = await decompiledSourceService.AddSourceToAsync(temporaryDocument, compilation, symbol, cancellationToken).ConfigureAwait(false);
temporaryDocument = await decompiledSourceService.AddSourceToAsync(temporaryDocument, compilation, symbol, formattingOptions, cancellationToken).ConfigureAwait(false);
}
else
{
Expand All @@ -93,7 +94,7 @@ public DecompilationMetadataAsSourceFileProvider()
if (!useDecompiler)
{
var sourceFromMetadataService = temporaryDocument.Project.LanguageServices.GetRequiredService<IMetadataAsSourceService>();
temporaryDocument = await sourceFromMetadataService.AddSourceToAsync(temporaryDocument, compilation, symbol, cancellationToken).ConfigureAwait(false);
temporaryDocument = await sourceFromMetadataService.AddSourceToAsync(temporaryDocument, compilation, symbol, formattingOptions, cancellationToken).ConfigureAwait(false);
}

// We have the content, so write it out to disk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;

namespace Microsoft.CodeAnalysis.MetadataAsSource
Expand All @@ -22,6 +23,6 @@ internal interface IMetadataAsSourceService : ILanguageService
/// <param name="symbol">The symbol to generate source for</param>
/// <param name="cancellationToken">To cancel document operations</param>
/// <returns>The updated document</returns>
Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, CancellationToken cancellationToken = default);
Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.Formatting;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.MetadataAsSource;
using Microsoft.CodeAnalysis.Shared.Extensions;

Expand All @@ -21,10 +24,29 @@ internal static class OmniSharpMetadataAsSourceService
/// <param name="symbol">The symbol to generate source for</param>
/// <param name="cancellationToken">To cancel document operations</param>
/// <returns>The updated document</returns>
public static Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, CancellationToken cancellationToken)
[Obsolete("Use overloads that takes formatting options")]
public static async Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, CancellationToken cancellationToken)
{
var service = document.GetRequiredLanguageService<IMetadataAsSourceService>();
return service.AddSourceToAsync(document, symbolCompilation, symbol, cancellationToken);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
return await service.AddSourceToAsync(document, symbolCompilation, symbol, formattingOptions, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Generates formatted source code containing general information about the symbol's
/// containing assembly, and the public, protected, and protected-or-internal interface of
/// which the given ISymbol is or is a part of into the given document
/// </summary>
/// <param name="document">The document to generate source into</param>
/// <param name="symbolCompilation">The <see cref="Compilation"/> in which <paramref name="symbol"/> is resolved.</param>
/// <param name="symbol">The symbol to generate source for</param>
/// <param name="formattingOptions">Options to use to format the document.</param>
/// <param name="cancellationToken">To cancel document operations</param>
/// <returns>The updated document</returns>
public static Task<Document> AddSourceToAsync(Document document, Compilation symbolCompilation, ISymbol symbol, OmniSharpSyntaxFormattingOptionsWrapper formattingOptions, CancellationToken cancellationToken)
{
var service = document.GetRequiredLanguageService<IMetadataAsSourceService>();
return service.AddSourceToAsync(document, symbolCompilation, symbol, formattingOptions.UnderlyingObject, cancellationToken);
}
}
}

0 comments on commit 6c556e2

Please sign in to comment.