-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move CodeHelpers to their own thing, add unit test for fixup
- Loading branch information
Showing
20 changed files
with
219 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" /> | ||
<ProjectReference Include="..\CodeHelpers\WPILib.CodeHelpers.csproj"/> | ||
<ProjectReference Include="..\..\src\thirdparty\Stereologue\Stereologue.csproj" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
using WPILib.CodeHelpers.LogGenerator.Analyzer; | ||
using Verify = Microsoft.CodeAnalysis.CSharp.Testing.XUnit.CodeFixVerifier<WPILib.CodeHelpers.LogGenerator.Analyzer.LogGeneratorAnalyzer, WPILib.CodeHelpers.LogGenerator.CodeFixer.LogGeneratorFixer>; | ||
|
||
namespace CodeHelpers.Test; | ||
|
||
public class LogGeneratorFixerTest | ||
{ | ||
const string InternalTypes = @" | ||
namespace Stereologue | ||
{ | ||
[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Method | System.AttributeTargets.Field, Inherited = false, AllowMultiple = false)] | ||
public sealed class LogAttribute : System.Attribute | ||
{ | ||
} | ||
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct)] | ||
public sealed class GenerateLogAttribute : System.Attribute | ||
{ | ||
} | ||
} | ||
"; | ||
[Fact] | ||
public async void Test1() | ||
{ | ||
string testString = @" | ||
using Stereologue; | ||
public partial class MyNewClass | ||
{ | ||
[Log] | ||
public int Variable { get; } | ||
} | ||
"; | ||
string fixedCode = @" | ||
using Stereologue; | ||
[GenerateLog] | ||
public partial class MyNewClass | ||
{ | ||
[Log] | ||
public int Variable { get; } | ||
}"; | ||
testString += InternalTypes; | ||
fixedCode += InternalTypes; | ||
var expected = Verify.Diagnostic(LoggerDiagnostics.MissingGenerateLog).WithLocation(3, 22).WithArguments(["Variable", "MyNewClass"]); | ||
await Verify.VerifyCodeFixAsync(testString, expected, fixedCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
codehelp/CodeHelpers/LogGenerator/CodeFixer/LogGeneratorFixer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using WPILib.CodeHelpers.LogGenerator.Analyzer; | ||
|
||
namespace WPILib.CodeHelpers.LogGenerator.CodeFixer; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp)] | ||
public class LogGeneratorFixer : CodeFixProvider | ||
{ | ||
private const string title = "Make constant"; | ||
|
||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create([ | ||
LoggerDiagnostics.Ids.MissingGenerateLog, | ||
]); | ||
|
||
public sealed override FixAllProvider GetFixAllProvider() | ||
{ | ||
return WellKnownFixAllProviders.BatchFixer; | ||
} | ||
|
||
public sealed 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; | ||
|
||
// Find the type declaration identified by the diagnostic. | ||
var declaration = root!.FindToken(diagnosticSpan.Start).Parent!.AncestorsAndSelf().OfType<TypeDeclarationSyntax>().First(); | ||
|
||
// Register a code action that will invoke the fix. | ||
context.RegisterCodeFix( | ||
CodeAction.Create( | ||
title: title, | ||
createChangedDocument: c => AddGenerateLogAttribute(context.Document, declaration!, c), | ||
equivalenceKey: title), | ||
diagnostic); | ||
} | ||
|
||
private async Task<Document> AddGenerateLogAttribute(Document document, TypeDeclarationSyntax typeSyntax, CancellationToken cancellationToken) | ||
{ | ||
var root = await document.GetSyntaxRootAsync(cancellationToken)!; | ||
var attributes = typeSyntax.AttributeLists.Add( | ||
SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList( | ||
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("GenerateLog")) | ||
))); | ||
|
||
return document.WithSyntaxRoot( | ||
root!.ReplaceNode( | ||
typeSyntax, | ||
typeSyntax.WithAttributeLists(attributes).NormalizeWhitespace() | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ologueSourceGenerator/LogAttributeInfo.cs → ...eHelpers/LogGenerator/LogAttributeInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...tereologueSourceGenerator/LogGenerator.cs → ...Generator/SourceGenerator/LogGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ion/StereologueSourceGenerator/Strings.cs → codehelp/CodeHelpers/LogGenerator/Strings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
namespace Stereologue.SourceGenerator; | ||
namespace WPILib.CodeHelpers.LogGenerator; | ||
|
||
public static class Strings | ||
{ | ||
public const string GenerateLogAttributeName = "Stereologue.GenerateLogAttribute"; | ||
public const string LogAttributeName = "Stereologue.LogAttribute"; | ||
public const string ILoggedName = "Stereologue.ILogged"; | ||
public const string UpdateStereologueName = "UpdateStereologue"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace WPILib.CodeHelpers.LogGenerator; | ||
|
||
public static class SymbolExtensions | ||
{ | ||
public static bool HasLogAttribute(this ISymbol symbol) | ||
{ | ||
return symbol.GetAttributes().Where(x => x.AttributeClass?.ToDisplayString() == Strings.LogAttributeName).Any(); | ||
} | ||
|
||
public static bool HasGenerateLogAttribute(this ISymbol symbol) | ||
{ | ||
return symbol.GetAttributes().Where(x => x.AttributeClass?.ToDisplayString() == Strings.GenerateLogAttributeName).Any(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...eSourceGenerator/SpecialTypeExtensions.cs → ...help/CodeHelpers/SpecialTypeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.