< Summary

Information
Class: Hertzole.ScriptableValues.Generator.NoMarkerAttributeCodeFixProvider
Assembly: Hertzole.ScriptableValues.Generator
File(s): Generator/Hertzole.ScriptableValues.Generator/Code Fixers/NoMarkerAttributeCodeFixProvider.cs
Tag: 52_28197466029
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 86
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
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(...)100%110%
CreateChangedDocument()0%10100%

File(s)

Generator/Hertzole.ScriptableValues.Generator/Code Fixers/NoMarkerAttributeCodeFixProvider.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(NoMarkerAttributeCodeFixProvider))]
 15[Shared]
 16public sealed class NoMarkerAttributeCodeFixProvider : CodeFixProvider
 17{
 018    private static readonly ImmutableArray<string> fixableDiagnosticIds = ImmutableArray.Create(DiagnosticIdentifiers.No
 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];
 36
 037        context.RegisterCodeFix(
 038            CodeAction.Create(Resources.HSV0002CodeFixTitle, token => CreateChangedDocument(context, token), nameof(Reso
 039            diagnostic);
 40
 041        return Task.CompletedTask;
 42    }
 43
 44    private static async Task<Document> CreateChangedDocument(CodeFixContext context, CancellationToken cancellationToke
 45    {
 046        SemanticModel? semanticModel = await context.Document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(fa
 047        if (semanticModel == null)
 48        {
 049            return context.Document;
 50        }
 51
 052        DocumentEditor editor = await DocumentEditor.CreateAsync(context.Document, cancellationToken).ConfigureAwait(fal
 053        SyntaxGenerator generator = editor.Generator;
 54
 055        INamedTypeSymbol? attributeSymbol = semanticModel.Compilation.GetTypeByMetadataName(Types.MARKER_ATTRIBUTE);
 056        if (attributeSymbol == null)
 57        {
 058            return context.Document;
 59        }
 60
 061        SyntaxNode? root = await context.Document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
 062        if (root == null)
 63        {
 064            return context.Document;
 65        }
 66
 067        TextSpan diagnosticSpan = context.Diagnostics[0].Location.SourceSpan;
 068        SyntaxNode node = root.FindNode(diagnosticSpan);
 069        if (node is not AttributeSyntax attribute)
 70        {
 071            return context.Document;
 72        }
 73
 74        // Find the type declaration
 075        TypeDeclarationSyntax? typeDeclaration = attribute.FirstAncestorOrSelf<TypeDeclarationSyntax>();
 076        if (typeDeclaration == null)
 77        {
 078            return context.Document;
 79        }
 80
 081        SyntaxNode attributeDeclaration = generator.Attribute(generator.TypeExpression(attributeSymbol, true));
 082        editor.AddAttribute(typeDeclaration, attributeDeclaration);
 83
 084        return editor.GetChangedDocument();
 085    }
 86}