Skip to content

Commit

Permalink
Merge pull request #132 from VeyronSakai/feature/comment-code-fix-pro…
Browse files Browse the repository at this point in the history
…vider

comment CodeFixProvider
  • Loading branch information
VeyronSakai authored Jan 7, 2025
2 parents 3f9f077 + 3e6bf3e commit 3637e97
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 143 deletions.
90 changes: 45 additions & 45 deletions VContainerAnalyzer.Test/InjectAttributeCodeFixProviderTest.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

using System.Threading.Tasks;
using NUnit.Framework;
using VerifyField =
Microsoft.CodeAnalysis.CSharp.Testing.NUnit.CodeFixVerifier<VContainerAnalyzer.Analyzers.FieldAnalyzer,
VContainerAnalyzer.CodeFixProviders.InjectAttributeCodeFixProvider>;
using VerifyProperty =
Microsoft.CodeAnalysis.CSharp.Testing.NUnit.CodeFixVerifier<VContainerAnalyzer.Analyzers.PropertyAnalyzer,
VContainerAnalyzer.CodeFixProviders.InjectAttributeCodeFixProvider>;

namespace VContainerAnalyzer.Test;

[TestFixture]
public class InjectAttributeCodeFixProviderTest
{
[Test]
public async Task RemoveInjectAttribute_FieldHasInjectAttribute_CodeFixed()
{
var (source, _) = Helper.GetJoinedFilesContentText("FieldInjectionClass.cs", "EmptyClassStub.cs");
var (fixedSource, offset) =
Helper.GetJoinedFilesContentText("FieldInjectionClassFixed.txt", "EmptyClassStub.cs");

var expected = VerifyField.Diagnostic()
.WithSpan(6 + offset, 10, 6 + offset, 16)
.WithArguments("_field1");

await VerifyField.VerifyCodeFixAsync(source, expected, fixedSource);
}

[Test]
public async Task RemoveInjectAttribute_PropertyHasInjectAttribute_CodeFixed()
{
var (source, _) = Helper.GetJoinedFilesContentText("PropertyInjectionClass.cs", "EmptyClassStub.cs");
var (fixedSource, offset) =
Helper.GetJoinedFilesContentText("PropertyInjectionClassFixed.txt", "EmptyClassStub.cs");

var expected = VerifyProperty.Diagnostic()
.WithSpan(6 + offset, 10, 6 + offset, 16)
.WithArguments("Property1");

await VerifyProperty.VerifyCodeFixAsync(source, expected, fixedSource);
}
}
// // Copyright (c) 2020-2024 VeyronSakai.
// // This software is released under the MIT License.
//
// using System.Threading.Tasks;
// using NUnit.Framework;
// using VerifyField =
// Microsoft.CodeAnalysis.CSharp.Testing.NUnit.CodeFixVerifier<VContainerAnalyzer.Analyzers.FieldAnalyzer,
// VContainerAnalyzer.CodeFixProviders.InjectAttributeCodeFixProvider>;
// using VerifyProperty =
// Microsoft.CodeAnalysis.CSharp.Testing.NUnit.CodeFixVerifier<VContainerAnalyzer.Analyzers.PropertyAnalyzer,
// VContainerAnalyzer.CodeFixProviders.InjectAttributeCodeFixProvider>;
//
// namespace VContainerAnalyzer.Test;
//
// [TestFixture]
// public class InjectAttributeCodeFixProviderTest
// {
// [Test]
// public async Task RemoveInjectAttribute_FieldHasInjectAttribute_CodeFixed()
// {
// var (source, _) = Helper.GetJoinedFilesContentText("FieldInjectionClass.cs", "EmptyClassStub.cs");
// var (fixedSource, offset) =
// Helper.GetJoinedFilesContentText("FieldInjectionClassFixed.txt", "EmptyClassStub.cs");
//
// var expected = VerifyField.Diagnostic()
// .WithSpan(6 + offset, 10, 6 + offset, 16)
// .WithArguments("_field1");
//
// await VerifyField.VerifyCodeFixAsync(source, expected, fixedSource);
// }
//
// [Test]
// public async Task RemoveInjectAttribute_PropertyHasInjectAttribute_CodeFixed()
// {
// var (source, _) = Helper.GetJoinedFilesContentText("PropertyInjectionClass.cs", "EmptyClassStub.cs");
// var (fixedSource, offset) =
// Helper.GetJoinedFilesContentText("PropertyInjectionClassFixed.txt", "EmptyClassStub.cs");
//
// var expected = VerifyProperty.Diagnostic()
// .WithSpan(6 + offset, 10, 6 + offset, 16)
// .WithArguments("Property1");
//
// await VerifyProperty.VerifyCodeFixAsync(source, expected, fixedSource);
// }
// }
2 changes: 1 addition & 1 deletion VContainerAnalyzer.Test/VContainerAnalyzer.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>latest</LangVersion>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
194 changes: 97 additions & 97 deletions VContainerAnalyzer/CodeFixProviders/InjectAttributeCodeFixProvider.cs
Original file line number Diff line number Diff line change
@@ -1,97 +1,97 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SyntaxFactory = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace VContainerAnalyzer.CodeFixProviders;

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(InjectAttributeCodeFixProvider)), Shared]
public sealed class InjectAttributeCodeFixProvider : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(Rules.Rule0002.Id);

public override FixAllProvider GetFixAllProvider()
{
return WellKnownFixAllProviders.BatchFixer;
}

public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var root = await context
.Document
.GetSyntaxRootAsync(context.CancellationToken)
.ConfigureAwait(false);

var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var declaration = root?
.FindToken(diagnosticSpan.Start)
.Parent?
.AncestorsAndSelf()
.OfType<MemberDeclarationSyntax>()
.FirstOrDefault();

if (declaration == null)
{
return;
}

if (declaration is not FieldDeclarationSyntax && declaration is not PropertyDeclarationSyntax)
{
return;
}

context.RegisterCodeFix(
CodeAction.Create(
"Remove InjectAttribute",
cancellationToken =>
RemoveInjectAttribute(context.Document, declaration, cancellationToken),
FixableDiagnosticIds.Single()),
context.Diagnostics);
}

private static async Task<Document> RemoveInjectAttribute(Document document, MemberDeclarationSyntax declaration,
CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var newAttributeLists = new List<AttributeListSyntax>();

foreach (var attributeList in declaration.AttributeLists)
{
var nodesToRemove = new List<AttributeSyntax>();

foreach (var attribute in attributeList.Attributes)
{
var attributeType = model?.GetTypeInfo(attribute).Type;
if (attributeType != null && attributeType.IsVContainerInjectAttribute())
{
nodesToRemove.Add(attribute);
}
}

var newAttributes = attributeList.RemoveNodes(nodesToRemove, SyntaxRemoveOptions.KeepNoTrivia);
if (newAttributes.Attributes.Any())
{
newAttributeLists.Add(newAttributes);
}
}

var newDeclaration = declaration
.WithAttributeLists(SyntaxFactory.List(newAttributeLists))
.WithLeadingTrivia(declaration.GetLeadingTrivia());

var newRoot = root?.ReplaceNode(declaration, newDeclaration);
return newRoot == null ? document : document.WithSyntaxRoot(newRoot);
}
}
// // Copyright (c) 2020-2024 VeyronSakai.
// // This software is released under the MIT License.
//
// using System.Collections.Generic;
// using System.Collections.Immutable;
// using System.Composition;
// using System.Linq;
// using System.Threading;
// using System.Threading.Tasks;
// using Microsoft.CodeAnalysis;
// using Microsoft.CodeAnalysis.CodeActions;
// using Microsoft.CodeAnalysis.CodeFixes;
// using Microsoft.CodeAnalysis.CSharp.Syntax;
// using SyntaxFactory = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
//
// namespace VContainerAnalyzer.CodeFixProviders;
//
// [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(InjectAttributeCodeFixProvider)), Shared]
// public sealed class InjectAttributeCodeFixProvider : CodeFixProvider
// {
// public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(Rules.Rule0002.Id);
//
// public override FixAllProvider GetFixAllProvider()
// {
// return WellKnownFixAllProviders.BatchFixer;
// }
//
// public override async Task RegisterCodeFixesAsync(CodeFixContext context)
// {
// var root = await context
// .Document
// .GetSyntaxRootAsync(context.CancellationToken)
// .ConfigureAwait(false);
//
// var diagnostic = context.Diagnostics.First();
// var diagnosticSpan = diagnostic.Location.SourceSpan;
// var declaration = root?
// .FindToken(diagnosticSpan.Start)
// .Parent?
// .AncestorsAndSelf()
// .OfType<MemberDeclarationSyntax>()
// .FirstOrDefault();
//
// if (declaration == null)
// {
// return;
// }
//
// if (declaration is not FieldDeclarationSyntax && declaration is not PropertyDeclarationSyntax)
// {
// return;
// }
//
// context.RegisterCodeFix(
// CodeAction.Create(
// "Remove InjectAttribute",
// cancellationToken =>
// RemoveInjectAttribute(context.Document, declaration, cancellationToken),
// FixableDiagnosticIds.Single()),
// context.Diagnostics);
// }
//
// private static async Task<Document> RemoveInjectAttribute(Document document, MemberDeclarationSyntax declaration,
// CancellationToken cancellationToken)
// {
// var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
// var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
// var newAttributeLists = new List<AttributeListSyntax>();
//
// foreach (var attributeList in declaration.AttributeLists)
// {
// var nodesToRemove = new List<AttributeSyntax>();
//
// foreach (var attribute in attributeList.Attributes)
// {
// var attributeType = model?.GetTypeInfo(attribute).Type;
// if (attributeType != null && attributeType.IsVContainerInjectAttribute())
// {
// nodesToRemove.Add(attribute);
// }
// }
//
// var newAttributes = attributeList.RemoveNodes(nodesToRemove, SyntaxRemoveOptions.KeepNoTrivia);
// if (newAttributes.Attributes.Any())
// {
// newAttributeLists.Add(newAttributes);
// }
// }
//
// var newDeclaration = declaration
// .WithAttributeLists(SyntaxFactory.List(newAttributeLists))
// .WithLeadingTrivia(declaration.GetLeadingTrivia());
//
// var newRoot = root?.ReplaceNode(declaration, newDeclaration);
// return newRoot == null ? document : document.WithSyntaxRoot(newRoot);
// }
// }

0 comments on commit 3637e97

Please sign in to comment.