Browse Source

Code Refactoring

CPKreuz 1 year ago
parent
commit
46a96c1eac

+ 0 - 1
src/PixiEditor.Extensions.CommonApi.Diagnostics/Diagnostics/UseGenericEnumerableForListArrayDiagnostic.cs

@@ -1,5 +1,4 @@
 using System.Collections.Immutable;
-using System.Linq;
 using System.Threading;
 using Microsoft.CodeAnalysis;
 using Microsoft.CodeAnalysis.CSharp;

+ 27 - 24
src/PixiEditor.Extensions.CommonApi.Diagnostics/Diagnostics/UseNonOwnedDiagnostic.cs

@@ -1,5 +1,4 @@
-using System;
-using System.Collections.Immutable;
+using System.Collections.Immutable;
 using System.Linq;
 using Microsoft.CodeAnalysis;
 using Microsoft.CodeAnalysis.CSharp;
@@ -42,32 +41,20 @@ public class UseNonOwnedDiagnostic : DiagnosticAnalyzer
         }
         
         // TODO: Also handle => new()
-        if (declaration.Initializer is not { Value: BaseObjectCreationExpressionSyntax { ArgumentList.Arguments: { Count: > 0 } arguments } initializerExpression })
+        if (declaration.Initializer is not { Value: BaseObjectCreationExpressionSyntax { ArgumentList.Arguments: { Count: > 0 } arguments } initializerExpression } ||
+            DoesNotMatch(semantics, arguments, declaration))
         {
             return;
         }
+        
+        var diagnostic = GetDiagnostic(arguments, declaration, semantics, initializerExpression, typeInfo);
 
-        var nameArgument = arguments.First();
-
-        var operation = semantics.GetOperation(nameArgument.Expression);
+        context.ReportDiagnostic(diagnostic);
+    }
 
-        if (operation?.ConstantValue.Value is not string constant)
-        {
-            return;
-        }
-        
-        if (DiagnosticHelpers.GetKey(constant) is not { } key)
-        {
-            return;
-        }
-        
-        var declarationName = declaration.Identifier.ValueText;
-        
-        if (key != declarationName)
-        {
-            return;
-        }
-        
+    private static Diagnostic GetDiagnostic(SeparatedSyntaxList<ArgumentSyntax> arguments, PropertyDeclarationSyntax declaration,
+        SemanticModel semantics, BaseObjectCreationExpressionSyntax initializerExpression, TypeInfo typeInfo)
+    {
         var genericType = string.Empty;
 
         var fallbackValueArgument = arguments.Skip(1).FirstOrDefault();
@@ -83,7 +70,23 @@ public class UseNonOwnedDiagnostic : DiagnosticAnalyzer
         var diagnostic = Diagnostic.Create(Descriptor, initializerExpression.GetLocation(),
             typeInfo.Type?.Name, // LocalSetting or Synced Setting
             genericType);
+        return diagnostic;
+    }
+
+    private static bool DoesNotMatch(SemanticModel semantics, SeparatedSyntaxList<ArgumentSyntax> arguments,
+        PropertyDeclarationSyntax declaration)
+    {
+        var nameArgument = arguments.First();
+
+        var operation = semantics.GetOperation(nameArgument.Expression);
+
+        if (operation?.ConstantValue.Value is not string constant || DiagnosticHelpers.GetKey(constant) is not { } key)
+        {
+            return true;
+        }
         
-        context.ReportDiagnostic(diagnostic);
+        var declarationName = declaration.Identifier.ValueText;
+        
+        return key != declarationName;
     }
 }

+ 19 - 13
src/PixiEditor.Extensions.CommonApi.Diagnostics/Diagnostics/UseOwnedDiagnostic.cs

@@ -41,22 +41,19 @@ public class UseOwnedDiagnostic : DiagnosticAnalyzer
         }
         
         // TODO: Also handle => new()
-        if (declaration.Initializer is not { Value: BaseObjectCreationExpressionSyntax { ArgumentList.Arguments: { Count: > 0 } arguments } initializerExpression })
+        if (declaration.Initializer is not { Value: BaseObjectCreationExpressionSyntax { ArgumentList.Arguments: { Count: > 0 } arguments } initializerExpression } ||
+            DoesNotMatch(semantics, arguments, declaration))
         {
             return;
         }
 
-        var nameArgument = arguments.First();
-
-        var operation = semantics.GetOperation(nameArgument.Expression);
-        var constant = operation?.ConstantValue.Value as string;
-        var declarationName = declaration.Identifier.ValueText;
+        var diagnostic = GetDiagnostic(arguments, declaration, semantics, initializerExpression, typeInfo);
+        context.ReportDiagnostic(diagnostic);
+    }
 
-        if (constant != declarationName)
-        {
-            return;
-        }
-        
+    private static Diagnostic GetDiagnostic(SeparatedSyntaxList<ArgumentSyntax> arguments, PropertyDeclarationSyntax declaration,
+        SemanticModel semantics, BaseObjectCreationExpressionSyntax initializerExpression, TypeInfo typeInfo)
+    {
         var genericType = string.Empty;
 
         var fallbackValueArgument = arguments.Skip(1).FirstOrDefault();
@@ -72,7 +69,16 @@ public class UseOwnedDiagnostic : DiagnosticAnalyzer
         var diagnostic = Diagnostic.Create(Descriptor, initializerExpression.GetLocation(),
             typeInfo.Type?.Name, // LocalSetting or Synced Setting
             genericType);
-        
-        context.ReportDiagnostic(diagnostic);
+        return diagnostic;
+    }
+
+    private static bool DoesNotMatch(SemanticModel semantics, SeparatedSyntaxList<ArgumentSyntax> arguments, PropertyDeclarationSyntax declaration)
+    {
+        var nameArgument = arguments.First();
+        var operation = semantics.GetOperation(nameArgument.Expression);
+        var constant = operation?.ConstantValue.Value as string;
+        var declarationName = declaration.Identifier.ValueText;
+
+        return constant != declarationName;
     }
 }

+ 13 - 7
src/PixiEditor.Extensions.CommonApi.Diagnostics/Fixes/UseNonOwnedFix.cs

@@ -33,6 +33,18 @@ public class UseNonOwnedFix : CodeFixProvider
 
     private static async Task<Document> CreateChangedDocument(Document document, PropertyDeclarationSyntax declaration,
         CancellationToken token)
+    {
+        var invocationExpression = await GetNewInvocation(document, declaration, token);
+        var root = await document.GetSyntaxRootAsync(token);
+
+        var newRoot = root!.ReplaceNode(declaration.Initializer!.Value, invocationExpression);
+
+        // TODO: The initializer part does not have it's generic type replaced
+        return document.WithSyntaxRoot(newRoot);
+    }
+
+    private static async Task<InvocationExpressionSyntax> GetNewInvocation(Document document, PropertyDeclarationSyntax declaration,
+        CancellationToken token)
     {
         var settingType = (GenericNameSyntax)declaration.Type;
         var originalInvocation = (BaseObjectCreationExpressionSyntax)declaration.Initializer!.Value;
@@ -47,13 +59,7 @@ public class UseNonOwnedFix : CodeFixProvider
         var arguments = SkipArgumentAndAdd(originalInvocation.ArgumentList!, prefixArgument);
 
         var invocationExpression = SyntaxFactory.InvocationExpression(accessExpression, arguments);
-
-        var root = await document.GetSyntaxRootAsync(token);
-
-        var newRoot = root!.ReplaceNode(declaration.Initializer.Value, invocationExpression);
-
-        // TODO: The initializer part does not have it's generic type replaced
-        return document.WithSyntaxRoot(newRoot);
+        return invocationExpression;
     }
 
     private static async ValueTask<ArgumentSyntax> GetPrefixArgument(Document document, CancellationToken token,

+ 12 - 7
src/PixiEditor.Extensions.CommonApi.Diagnostics/Fixes/UseOwnedFix.cs

@@ -31,6 +31,17 @@ public class UseOwnedFix : CodeFixProvider
     }
 
     private static async Task<Document> CreateChangedDocument(Document document, PropertyDeclarationSyntax declaration, CancellationToken token)
+    {
+        var invocationExpression = GetNewInvocation(declaration);
+        var root = await document.GetSyntaxRootAsync(token);
+
+        var newRoot = root!.ReplaceNode(declaration.Initializer.Value, invocationExpression);
+
+        // TODO: The initializer part does not have it's generic type replaced
+        return document.WithSyntaxRoot(newRoot);
+    }
+
+    private static InvocationExpressionSyntax GetNewInvocation(PropertyDeclarationSyntax declaration)
     {
         var settingType = (GenericNameSyntax)declaration.Type;
         var originalInvocation = (BaseObjectCreationExpressionSyntax)declaration.Initializer!.Value;
@@ -40,13 +51,7 @@ public class UseOwnedFix : CodeFixProvider
         
         var accessExpression = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, classIdentifier, ownedIdentifier);
         var invocationExpression = SyntaxFactory.InvocationExpression(accessExpression, SkipArgument(originalInvocation.ArgumentList!));
-
-        var root = await document.GetSyntaxRootAsync(token);
-
-        var newRoot = root!.ReplaceNode(declaration.Initializer.Value, invocationExpression);
-
-        // TODO: The initializer part does not have it's generic type replaced
-        return document.WithSyntaxRoot(newRoot);
+        return invocationExpression;
     }
 
     private static ArgumentListSyntax SkipArgument(ArgumentListSyntax original)