-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add analyzer and code fix for making unions partial
=> release
- Loading branch information
Showing
37 changed files
with
369 additions
and
112 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
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
96 changes: 96 additions & 0 deletions
96
Source/Sundew.DiscriminatedUnions.Analyzer/MakePartialMarkerAnalyzer.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,96 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="MakePartialMarkerAnalyzer.cs" company="Sundews"> | ||
// Copyright (c) Sundews. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace Sundew.DiscriminatedUnions.Analyzer; | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Sundew.DiscriminatedUnions.Shared; | ||
|
||
/// <summary> | ||
/// Marks all union types with a diagnostic, so that a code fix can be offered to generate factory methods. | ||
/// </summary> | ||
/// <seealso cref="Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer" /> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class MakePartialMarkerAnalyzer : DiagnosticAnalyzer | ||
{ | ||
/// <summary> | ||
/// Diagnostic id for a union partial. | ||
/// </summary> | ||
public const string MakeUnionPartialDiagnosticId = "PDU0001"; | ||
|
||
/// <summary> | ||
/// The switch should throw in default case rule. | ||
/// </summary> | ||
public static readonly DiagnosticDescriptor MakeUnionPartialRule = | ||
Sundew.DiscriminatedUnions.Analyzer.DiagnosticDescriptorHelper.Create( | ||
MakeUnionPartialDiagnosticId, | ||
nameof(Resources.MakeUnionPartialTitle), | ||
nameof(Resources.MakeUnionPartialMessageFormat), | ||
Category, | ||
DiagnosticSeverity.Info, | ||
true, | ||
nameof(Resources.MakeUnionPartialDescription)); | ||
|
||
private const string Category = "CodeGeneration"; | ||
|
||
/// <summary> | ||
/// Gets a set of descriptors for the diagnostics that this analyzer is capable of producing. | ||
/// </summary> | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(MakeUnionPartialRule); | ||
|
||
/// <summary> | ||
/// Called once at session start to register actions in the analysis context. | ||
/// </summary> | ||
/// <param name="context">The context.</param> | ||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
|
||
context.RegisterSymbolAction( | ||
symbolAnalysisContext => | ||
{ | ||
if (symbolAnalysisContext.Symbol is not INamedTypeSymbol namedTypeSymbol) | ||
{ | ||
return; | ||
} | ||
|
||
if (namedTypeSymbol.IsDiscriminatedUnion() && | ||
(namedTypeSymbol.IsAbstract || namedTypeSymbol.TypeKind == TypeKind.Interface) && !IsPartial(namedTypeSymbol)) | ||
{ | ||
foreach (var location in namedTypeSymbol.Locations) | ||
{ | ||
symbolAnalysisContext.ReportDiagnostic( | ||
Diagnostic.Create( | ||
MakeUnionPartialRule, | ||
location, | ||
DiagnosticSeverity.Info, | ||
null, | ||
null, | ||
namedTypeSymbol)); | ||
} | ||
} | ||
}, | ||
SymbolKind.NamedType); | ||
} | ||
|
||
private static bool IsPartial(INamedTypeSymbol namedTypeSymbol) | ||
{ | ||
var syntax = namedTypeSymbol.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax(); | ||
if (syntax is TypeDeclarationSyntax typeDeclarationSyntax) | ||
{ | ||
return typeDeclarationSyntax.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)); | ||
} | ||
|
||
return false; | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
Source/Sundew.DiscriminatedUnions.Analyzer/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
11 changes: 10 additions & 1 deletion
11
Source/Sundew.DiscriminatedUnions.CodeFixes/CodeFixResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
56 changes: 56 additions & 0 deletions
56
Source/Sundew.DiscriminatedUnions.CodeFixes/MakePartialCodeFixer.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,56 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="MakePartialCodeFixer.cs" company="Sundews"> | ||
// Copyright (c) Sundews. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// </copyright> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
namespace Sundew.DiscriminatedUnions.CodeFixes; | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Editing; | ||
using Microsoft.CodeAnalysis.Formatting; | ||
using Sundew.DiscriminatedUnions.Analyzer; | ||
|
||
internal class MakePartialCodeFixer : ICodeFixer | ||
{ | ||
public string DiagnosticId => MakePartialMarkerAnalyzer.MakeUnionPartialDiagnosticId; | ||
|
||
public CodeFixStatus GetCodeFixState( | ||
SyntaxNode syntaxNode, | ||
SemanticModel semanticModel, | ||
Diagnostic diagnostic, | ||
CancellationToken cancellationToken) | ||
{ | ||
var declaredSymbol = semanticModel.GetDeclaredSymbol(syntaxNode, cancellationToken); | ||
if (declaredSymbol == null) | ||
{ | ||
return new CodeFixStatus.CannotFix(); | ||
} | ||
|
||
var name = string.Format(CodeFixResources.MakePartial, declaredSymbol.Name); | ||
return new CodeFixStatus.CanFix( | ||
name, | ||
nameof(MakePartialCodeFixer)); | ||
} | ||
|
||
public async Task<Document> Fix( | ||
Document document, | ||
SyntaxNode root, | ||
SyntaxNode node, | ||
IReadOnlyList<Location> additionalLocations, | ||
ImmutableDictionary<string, string?> diagnosticProperties, | ||
SemanticModel semanticModel, | ||
CancellationToken cancellationToken) | ||
{ | ||
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); | ||
var generator = editor.Generator; | ||
var declaration = generator.WithModifiers(node, generator.GetModifiers(node).WithPartial(true)).WithAdditionalAnnotations(Formatter.Annotation); | ||
var newNode = root.ReplaceNode(node, declaration); | ||
return document.WithSyntaxRoot(newNode); | ||
} | ||
} |
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
Oops, something went wrong.