< Summary

Information
Class: Hertzole.ScriptableValues.Generator.TypeNotSupportedForCallbackAttributeCodeFixProvider
Assembly: Hertzole.ScriptableValues.Generator
File(s): Generator/Hertzole.ScriptableValues.Generator/Code Fixers/TypeNotSupportedForCallbackAttributeCodeFixProvider.cs
Tag: 52_28197466029
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 95
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
GetFixAllProvider()100%110%
RegisterCodeFixesAsync(...)0%440%
CreateChangedDocument()0%12120%

File(s)

Generator/Hertzole.ScriptableValues.Generator/Code Fixers/TypeNotSupportedForCallbackAttributeCodeFixProvider.cs

#LineLine coverage
 1using System.Collections.Immutable;
 2using System.Composition;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Microsoft.CodeAnalysis;
 6using Microsoft.CodeAnalysis.CodeActions;
 7using Microsoft.CodeAnalysis.CodeFixes;
 8using Microsoft.CodeAnalysis.CSharp.Syntax;
 9using Microsoft.CodeAnalysis.Editing;
 10using Microsoft.CodeAnalysis.Text;
 11
 12namespace Hertzole.ScriptableValues.Generator;
 13
 14[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(TypeNotSupportedForCallbackAttributeCodeFixProvider))]
 15[Shared]
 16public sealed class TypeNotSupportedForCallbackAttributeCodeFixProvider : CodeFixProvider
 17{
 018    private static readonly ImmutableArray<string> fixableDiagnosticIds = ImmutableArray.Create(DiagnosticIdentifiers.Ty
 19
 20    /// <inheritdoc />
 21    public override ImmutableArray<string> FixableDiagnosticIds
 22    {
 023        get { return fixableDiagnosticIds; }
 24    }
 25
 26    /// <inheritdoc />
 27    public override FixAllProvider? GetFixAllProvider()
 28    {
 029        return WellKnownFixAllProviders.BatchFixer;
 30    }
 31
 32    /// <inheritdoc />
 33    public override Task RegisterCodeFixesAsync(CodeFixContext context)
 34    {
 035        Diagnostic diagnostic = context.Diagnostics[0];
 036        if (!diagnostic.Properties.TryGetValue("correctAttributeName", out string? attributeName) ||
 037            string.IsNullOrEmpty(attributeName))
 38        {
 039            return Task.CompletedTask;
 40        }
 41
 042        context.RegisterCodeFix(CodeAction.Create(
 043            string.Format(Resources.HSV0100CodeFixTitle, attributeName),
 044            token => CreateChangedDocument(context, token),
 045            nameof(Resources.HSV0100CodeFixTitle)), diagnostic);
 46
 047        return Task.CompletedTask;
 48    }
 49
 50    private static async Task<Document> CreateChangedDocument(CodeFixContext context, CancellationToken cancellationToke
 51    {
 052        Diagnostic diagnostic = context.Diagnostics[0];
 053        if (!diagnostic.Properties.TryGetValue("correctAttributeName", out string? attributeName) ||
 054            string.IsNullOrEmpty(attributeName))
 55        {
 056            return context.Document;
 57        }
 58
 059        SyntaxNode? root = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
 060        if (root == null)
 61        {
 062            return context.Document;
 63        }
 64
 065        TextSpan diagnosticSpan = context.Diagnostics[0].Location.SourceSpan;
 066        SyntaxNode node = root.FindNode(diagnosticSpan);
 67
 068        if (node is not AttributeSyntax attribute)
 69        {
 070            return context.Document;
 71        }
 72
 073        SemanticModel? semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(fa
 074        if (semanticModel == null)
 75        {
 076            return context.Document;
 77        }
 78
 79        // Get symbol for the new attribute
 080        string fullName = "Hertzole.ScriptableValues." + attributeName;
 081        INamedTypeSymbol? newAttributeSymbol = semanticModel.Compilation.GetTypeByMetadataName(fullName);
 082        if (newAttributeSymbol == null)
 83        {
 84            Log.Info("Could not find symbol for new attribute: " + fullName);
 085            return context.Document;
 86        }
 87
 088        DocumentEditor? editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(fa
 089        SyntaxNode newAttribute = editor.Generator.Attribute(editor.Generator.TypeExpression(newAttributeSymbol));
 90
 091        editor.ReplaceNode(attribute, newAttribute);
 92
 093        return editor.GetChangedDocument();
 094    }
 95}