in src/Microsoft.VisualStudio.SDK.Analyzers.CodeFixes/VSSDK002PackageRegistrationMatchesBaseTypeCodeFix.cs [47:81]
private async Task<Document> UpdateAttributeAsync(CodeFixContext context, Diagnostic diagnostic, CancellationToken cancellationToken)
{
bool isAsyncPackage = diagnostic.Properties[VSSDK002PackageRegistrationMatchesBaseTypeAnalyzer.BaseTypeDiagnosticPropertyName] == Types.AsyncPackage.TypeName;
LiteralExpressionSyntax appropriateArgument = SyntaxFactory.LiteralExpression(isAsyncPackage ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression);
SyntaxNode root = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
switch (root.FindNode(diagnostic.Location.SourceSpan))
{
case AttributeSyntax packageRegistrationSyntax:
// We need to add an argument to this attribute
root = root.ReplaceNode(
packageRegistrationSyntax,
packageRegistrationSyntax.AddArgumentListArguments(
SyntaxFactory.AttributeArgument(appropriateArgument).WithNameEquals(SyntaxFactory.NameEquals(Types.PackageRegistrationAttribute.AllowsBackgroundLoading))));
break;
case AttributeArgumentSyntax allowBackgroundLoadingSyntax:
if (isAsyncPackage)
{
// We need to update this argument on the attribute.
AttributeSyntax originalAttribute = allowBackgroundLoadingSyntax.FirstAncestorOrSelf<AttributeSyntax>();
root = root.ReplaceNode(allowBackgroundLoadingSyntax, allowBackgroundLoadingSyntax.WithExpression(appropriateArgument));
}
else
{
// Let's just remove it since false is its default value.
root = root.RemoveNode(allowBackgroundLoadingSyntax, SyntaxRemoveOptions.KeepNoTrivia);
}
break;
case SyntaxNode node:
throw new InvalidOperationException("Unexpected syntax type: " + node.GetType().Name);
}
return context.Document.WithSyntaxRoot(root);
}