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 @@ -4,6 +4,7 @@
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand All @@ -21,8 +22,12 @@ public class AddGeneratedComClassFixer : ConvertToSourceGeneratedInteropFixer

protected override string BaseEquivalenceKey => nameof(AddGeneratedComClassFixer);

private static Task AddGeneratedComClassAsync(DocumentEditor editor, SyntaxNode node)
private static async Task AddGeneratedComClassAsync(SolutionEditor solutionEditor, DocumentId documentId, SyntaxNode node, CancellationToken ct)
{
var editor = await solutionEditor.GetDocumentEditorAsync(documentId, ct).ConfigureAwait(false);

var declaringType = editor.SemanticModel.GetDeclaredSymbol(node, ct) as INamedTypeSymbol;

editor.ReplaceNode(node, (node, gen) =>
{
var attribute = gen.Attribute(gen.TypeExpression(editor.SemanticModel.Compilation.GetBestTypeByMetadataName(TypeNames.GeneratedComClassAttribute)).WithAdditionalAnnotations(Simplifier.AddImportsAnnotation));
Expand All @@ -37,12 +42,36 @@ private static Task AddGeneratedComClassAsync(DocumentEditor editor, SyntaxNode

MakeNodeParentsPartial(editor, node);

return Task.CompletedTask;
if (declaringType is not null)
{
var comVisibleAttributeType = editor.SemanticModel.Compilation.GetBestTypeByMetadataName(TypeNames.System_Runtime_InteropServices_ComVisibleAttribute);
if (comVisibleAttributeType is not null)
{
var comVisibleAttributes = declaringType.GetAttributes().Where(attr =>
SymbolEqualityComparer.Default.Equals(attr.AttributeClass, comVisibleAttributeType)
&& attr.ConstructorArguments.Length == 1
&& attr.ConstructorArguments[0].Value is true).ToArray();

foreach (var comVisibleAttr in comVisibleAttributes)
{
if (comVisibleAttr.ApplicationSyntaxReference is { } syntaxRef)
{
var comVisibleAttrSyntax = await syntaxRef.GetSyntaxAsync(ct).ConfigureAwait(false);
var attrDocumentId = solutionEditor.OriginalSolution.GetDocumentId(syntaxRef.SyntaxTree);
if (attrDocumentId is not null)
{
var attrEditor = await solutionEditor.GetDocumentEditorAsync(attrDocumentId, ct).ConfigureAwait(false);
attrEditor.RemoveNode(comVisibleAttrSyntax);
}
}
}
}
}
}

protected override Func<DocumentEditor, CancellationToken, Task> CreateFixForSelectedOptions(SyntaxNode node, ImmutableDictionary<string, Option> selectedOptions)
protected override Func<SolutionEditor, DocumentId, CancellationToken, Task> CreateFixForSelectedOptions(SyntaxNode node, ImmutableDictionary<string, Option> selectedOptions)
{
return (editor, _) => AddGeneratedComClassAsync(editor, node);
return (solutionEditor, documentId, ct) => AddGeneratedComClassAsync(solutionEditor, documentId, node, ct);
}

protected override string GetDiagnosticTitle(ImmutableDictionary<string, Option> selectedOptions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ protected override string GetDiagnosticTitle(ImmutableDictionary<string, Option>
: SR.ConvertToGeneratedComInterfaceTitle;
}

protected override Func<DocumentEditor, CancellationToken, Task> CreateFixForSelectedOptions(SyntaxNode node, ImmutableDictionary<string, Option> selectedOptions)
protected override Func<SolutionEditor, DocumentId, CancellationToken, Task> CreateFixForSelectedOptions(SyntaxNode node, ImmutableDictionary<string, Option> selectedOptions)
{
bool mayRequireAdditionalWork = selectedOptions.TryGetValue(Option.MayRequireAdditionalWork, out Option mayRequireAdditionalWorkOption) && mayRequireAdditionalWorkOption is Option.Bool(true);
bool addStringMarshalling = selectedOptions.TryGetValue(AddStringMarshallingOption, out Option addStringMarshallingOption) && addStringMarshallingOption is Option.Bool(true);

return (editor, ct) => ConvertComImportToGeneratedComInterfaceAsync(editor, node, mayRequireAdditionalWork, addStringMarshalling, ct);
return async (solutionEditor, documentId, ct) =>
{
var editor = await solutionEditor.GetDocumentEditorAsync(documentId, ct).ConfigureAwait(false);
await ConvertComImportToGeneratedComInterfaceAsync(editor, node, mayRequireAdditionalWork, addStringMarshalling, ct).ConfigureAwait(false);
};
}

protected override ImmutableDictionary<string, Option> ParseOptionsFromDiagnostic(Diagnostic diagnostic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected virtual IEnumerable<ConvertToSourceGeneratedInteropFix> CreateAllFixes
yield return new ConvertToSourceGeneratedInteropFix(CreateFixForSelectedOptions(node, options), options);
}

protected abstract Func<DocumentEditor, CancellationToken, Task> CreateFixForSelectedOptions(SyntaxNode node, ImmutableDictionary<string, Option> selectedOptions);
protected abstract Func<SolutionEditor, DocumentId, CancellationToken, Task> CreateFixForSelectedOptions(SyntaxNode node, ImmutableDictionary<string, Option> selectedOptions);

protected abstract string GetDiagnosticTitle(ImmutableDictionary<string, Option> selectedOptions);

Expand Down Expand Up @@ -103,11 +103,10 @@ private ImmutableDictionary<string, Option> GetOptionsForIndividualFix(Immutable
return CombineOptions(fixAllOptions, ParseOptionsFromDiagnostic(diagnostic));
}

private static async Task<Solution> ApplyActionAndEnableUnsafe(Solution solution, DocumentId documentId, Func<DocumentEditor, CancellationToken, Task> documentBasedFix, CancellationToken ct)
private static async Task<Solution> ApplyActionAndEnableUnsafe(Solution solution, DocumentId documentId, Func<SolutionEditor, DocumentId, CancellationToken, Task> solutionBasedFix, CancellationToken ct)
{
var editor = new SolutionEditor(solution);
var docEditor = await editor.GetDocumentEditorAsync(documentId, ct).ConfigureAwait(false);
await documentBasedFix(docEditor, ct).ConfigureAwait(false);
await solutionBasedFix(editor, documentId, ct).ConfigureAwait(false);

var docProjectId = documentId.ProjectId;
var updatedSolution = editor.GetChangedSolution();
Expand Down Expand Up @@ -148,11 +147,9 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
GetDiagnosticTitle(fix.SelectedOptions),
async ct =>
{
DocumentEditor editor = await DocumentEditor.CreateAsync(doc, ct).ConfigureAwait(false);

await fix.ApplyFix(editor, ct).ConfigureAwait(false);

return editor.GetChangedDocument();
var solutionEditor = new SolutionEditor(doc.Project.Solution);
await fix.ApplyFix(solutionEditor, doc.Id, ct).ConfigureAwait(false);
return solutionEditor.GetChangedSolution();
},
Option.CreateEquivalenceKeyFromOptions(BaseEquivalenceKey, fix.SelectedOptions)),
diagnostic);
Expand All @@ -161,7 +158,7 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
}
}

protected record struct ConvertToSourceGeneratedInteropFix(Func<DocumentEditor, CancellationToken, Task> ApplyFix, ImmutableDictionary<string, Option> SelectedOptions);
protected record struct ConvertToSourceGeneratedInteropFix(Func<SolutionEditor, DocumentId, CancellationToken, Task> ApplyFix, ImmutableDictionary<string, Option> SelectedOptions);

private sealed class CustomFixAllProvider : FixAllProvider
{
Expand Down Expand Up @@ -196,14 +193,13 @@ private sealed class CustomFixAllProvider : FixAllProvider
continue;
}
DocumentId documentId = solutionEditor.OriginalSolution.GetDocumentId(diagnostic.Location.SourceTree)!;
DocumentEditor editor = await solutionEditor.GetDocumentEditorAsync(documentId, ct).ConfigureAwait(false);
SyntaxNode root = await diagnostic.Location.SourceTree.GetRootAsync(ct).ConfigureAwait(false);

SyntaxNode node = root.FindNode(diagnostic.Location.SourceSpan);

var documentBasedFix = codeFixProvider.CreateFixForSelectedOptions(node, codeFixProvider.GetOptionsForIndividualFix(options, diagnostic));
var solutionBasedFix = codeFixProvider.CreateFixForSelectedOptions(node, codeFixProvider.GetOptionsForIndividualFix(options, diagnostic));

await documentBasedFix(editor, ct).ConfigureAwait(false);
await solutionBasedFix(solutionEditor, documentId, ct).ConfigureAwait(false);

// Record this project as a project we need to allow unsafe blocks in.
projectsToAddUnsafe.Add(solutionEditor.OriginalSolution.GetDocument(documentId).Project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ protected override IEnumerable<ConvertToSourceGeneratedInteropFix> CreateAllFixe
var selectedOptions = options.Remove(CharSetOption);

yield return new ConvertToSourceGeneratedInteropFix(
(editor, ct) =>
ConvertToLibraryImport(
async (solutionEditor, documentId, ct) =>
{
var editor = await solutionEditor.GetDocumentEditorAsync(documentId, ct).ConfigureAwait(false);
await ConvertToLibraryImport(
editor,
node,
warnForAdditionalWork,
null,
ct),
ct).ConfigureAwait(false);
},
selectedOptions);

if (charSet is not null)
Expand All @@ -102,41 +105,50 @@ protected override IEnumerable<ConvertToSourceGeneratedInteropFix> CreateAllFixe
if (charSet is CharSet.None or CharSet.Ansi or CharSet.Auto)
{
yield return new ConvertToSourceGeneratedInteropFix(
(editor, ct) =>
ConvertToLibraryImport(
async (solutionEditor, documentId, ct) =>
{
var editor = await solutionEditor.GetDocumentEditorAsync(documentId, ct).ConfigureAwait(false);
await ConvertToLibraryImport(
editor,
node,
warnForAdditionalWork,
'A',
ct),
ct).ConfigureAwait(false);
},
selectedOptions.Add(SelectedSuffixOption, new Option.String("A")));
}
if (charSet is CharSet.Unicode or CharSet.Auto)
{
yield return new ConvertToSourceGeneratedInteropFix(
(editor, ct) =>
ConvertToLibraryImport(
async (solutionEditor, documentId, ct) =>
{
var editor = await solutionEditor.GetDocumentEditorAsync(documentId, ct).ConfigureAwait(false);
await ConvertToLibraryImport(
editor,
node,
warnForAdditionalWork,
'W',
ct),
ct).ConfigureAwait(false);
},
selectedOptions.Add(SelectedSuffixOption, new Option.String("W")));
}
}
}

protected override Func<DocumentEditor, CancellationToken, Task> CreateFixForSelectedOptions(SyntaxNode node, ImmutableDictionary<string, Option> selectedOptions)
protected override Func<SolutionEditor, DocumentId, CancellationToken, Task> CreateFixForSelectedOptions(SyntaxNode node, ImmutableDictionary<string, Option> selectedOptions)
{
bool warnForAdditionalWork = selectedOptions.TryGetValue(Option.MayRequireAdditionalWork, out Option mayRequireAdditionalWork) && mayRequireAdditionalWork is Option.Bool(true);
char? suffix = selectedOptions.TryGetValue(SelectedSuffixOption, out Option selectedSuffixOption) && selectedSuffixOption is Option.String(string selectedSuffix) ? selectedSuffix[0] : null;
return (editor, ct) =>
ConvertToLibraryImport(
return async (solutionEditor, documentId, ct) =>
{
var editor = await solutionEditor.GetDocumentEditorAsync(documentId, ct).ConfigureAwait(false);
await ConvertToLibraryImport(
editor,
node,
warnForAdditionalWork,
suffix,
ct);
ct).ConfigureAwait(false);
};
}

private static string AppendSuffix(string entryPoint, char? entryPointSuffix)
Expand Down
Loading
Loading