From 1d7240bc82acaa542173781d5737c32b558beea2 Mon Sep 17 00:00:00 2001 From: "Vladimir.Krasnotsvetov" Date: Thu, 18 Jul 2024 19:00:04 +0200 Subject: [PATCH] fix RIDER-107227 GitOrigin-RevId: 03899e6b3ddb00180c024d70b73786cec0f22c39 --- .../UsageInspectionsSuppressor.cs | 12 ++++- .../Odin/Attributes/OdinKnownAttributes.cs | 3 ++ .../UnityEditorIntegration/Api/UnityApi.cs | 49 +++++++++++++++++++ .../Unity/Utils/DeclaredElementExtensions.cs | 6 +++ 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/resharper/resharper-unity/src/Unity/CSharp/Daemon/UsageChecking/UsageInspectionsSuppressor.cs b/resharper/resharper-unity/src/Unity/CSharp/Daemon/UsageChecking/UsageInspectionsSuppressor.cs index be320b657..91a7852b6 100644 --- a/resharper/resharper-unity/src/Unity/CSharp/Daemon/UsageChecking/UsageInspectionsSuppressor.cs +++ b/resharper/resharper-unity/src/Unity/CSharp/Daemon/UsageChecking/UsageInspectionsSuppressor.cs @@ -59,7 +59,8 @@ public bool SuppressUsageInspectionsOnElement(IDeclaredElement element, out Impl { case IClass cls when unityApi.IsUnityType(cls) || cls.IsDotsImplicitlyUsedType() || - IsUxmlFactory(cls): + IsUxmlFactory(cls) || + unityApi.IsOdinType(cls): flags = ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature; return true; case IStruct @struct when unityApi.IsUnityType(@struct) || @@ -105,6 +106,10 @@ public bool SuppressUsageInspectionsOnElement(IDeclaredElement element, out Impl case IField field when unityApi.IsSerialisedField(field).HasFlag(SerializedFieldStatus.SerializedField): flags = ImplicitUseKindFlags.Assign; return true; + + case IField field when unityApi.IsOdinInspectorField(field): + flags = ImplicitUseKindFlags.Assign; + return true; case IProperty property when IsEventHandler(unityApi, property.Setter) || IsImplicitlyUsedInterfaceProperty(property) || @@ -112,7 +117,10 @@ public bool SuppressUsageInspectionsOnElement(IDeclaredElement element, out Impl unityApi.IsSerialisedAutoProperty(property, useSwea:true).HasFlag(SerializedFieldStatus.SerializedField): flags = ImplicitUseKindFlags.Assign; return true; - + case IProperty property when unityApi.IsOdinInspectorProperty(property): + flags = ImplicitUseKindFlags.Assign; + return true; + case IParameter parameter when parameter.IsRefMember() && parameter.GetContainingType().IsDotsImplicitlyUsedType(): flags = ImplicitUseKindFlags.Assign; diff --git a/resharper/resharper-unity/src/Unity/Odin/Attributes/OdinKnownAttributes.cs b/resharper/resharper-unity/src/Unity/Odin/Attributes/OdinKnownAttributes.cs index 7e84b5f6f..f6aee7b9a 100644 --- a/resharper/resharper-unity/src/Unity/Odin/Attributes/OdinKnownAttributes.cs +++ b/resharper/resharper-unity/src/Unity/Odin/Attributes/OdinKnownAttributes.cs @@ -71,6 +71,9 @@ public static class OdinKnownAttributes public static readonly IClrTypeName OdinSerializedComponent = new ClrTypeName("Sirenix.OdinInspector.SerializedComponent"); public static readonly IClrTypeName OdinSerializedStateMachineBehaviour = new ClrTypeName("Sirenix.OdinInspector.SerializedStateMachineBehaviour"); public static readonly IClrTypeName OdinSerializedUnityObject= new ClrTypeName("Sirenix.OdinInspector.SerializedUnityObject"); + + public static readonly IClrTypeName PropertyGroupAttribute = new ClrTypeName("Sirenix.OdinInspector.PropertyGroupAttribute"); + public static readonly IClrTypeName OdinDrawer = new ClrTypeName("Sirenix.OdinInspector.Editor.OdinDrawer"); public static readonly Dictionary AttributesWithMemberCompletion = new() diff --git a/resharper/resharper-unity/src/Unity/UnityEditorIntegration/Api/UnityApi.cs b/resharper/resharper-unity/src/Unity/UnityEditorIntegration/Api/UnityApi.cs index a1e850823..12c79406b 100644 --- a/resharper/resharper-unity/src/Unity/UnityEditorIntegration/Api/UnityApi.cs +++ b/resharper/resharper-unity/src/Unity/UnityEditorIntegration/Api/UnityApi.cs @@ -81,6 +81,17 @@ public UnityApi(UnityVersion unityVersion, UnityTypeCache unityTypeCache, UnityT public bool IsUnityType([NotNullWhen(true)] ITypeElement? type) => type != null && myUnityTypeCache.IsUnityType(type); + + public bool IsOdinType([NotNullWhen(true)] ITypeElement? type) + { + if (type == null) + return false; + + if (!OdinAttributeUtil.HasOdinSupport(myTechnologyDescriptionCollector)) + return false; + + return type.DerivesFromOdinDrawer(); + } // A serialised field cannot be abstract or generic, but a type declaration that will be serialised can be. This // method differentiates between a type declaration and a type usage. Consider renaming if we ever need to @@ -173,6 +184,44 @@ public SerializedFieldStatus IsSerialisedField(IField? field, bool useSwea = tru return status; } + + public bool IsOdinInspectorField(IField? field) + { + if (field == null) + return false; + + if (!OdinAttributeUtil.HasOdinSupport(myTechnologyDescriptionCollector)) + return false; + + foreach (var attribute in field.GetAttributeInstances(AttributesSource.Self)) + { + if (attribute.GetAttributeType().GetTypeElement().DerivesFrom(OdinKnownAttributes.PropertyGroupAttribute)) + { + return true; + } + } + + return false; + } + + public bool IsOdinInspectorProperty(IProperty? property) + { + if (property == null) + return false; + + if (!OdinAttributeUtil.HasOdinSupport(myTechnologyDescriptionCollector)) + return false; + + foreach (var attribute in property.GetBackingFieldAttributeInstances()) + { + if (attribute.GetAttributeType().GetTypeElement().DerivesFrom(OdinKnownAttributes.PropertyGroupAttribute)) + { + return true; + } + } + + return false; + } private SerializedFieldStatus IsSerialisedFieldByOdinRules(IField? field) { diff --git a/resharper/resharper-unity/src/Unity/Utils/DeclaredElementExtensions.cs b/resharper/resharper-unity/src/Unity/Utils/DeclaredElementExtensions.cs index 5673b4237..0f175728a 100644 --- a/resharper/resharper-unity/src/Unity/Utils/DeclaredElementExtensions.cs +++ b/resharper/resharper-unity/src/Unity/Utils/DeclaredElementExtensions.cs @@ -2,6 +2,7 @@ using JetBrains.Metadata.Reader.API; using JetBrains.ProjectModel; using JetBrains.ReSharper.Plugins.Unity.Core.ProjectModel; +using JetBrains.ReSharper.Plugins.Unity.Odin.Attributes; using JetBrains.ReSharper.Plugins.Unity.UnityEditorIntegration.Api; using JetBrains.ReSharper.Psi; using JetBrains.ReSharper.Psi.CSharp; @@ -56,6 +57,11 @@ public static bool DerivesFromMonoBehaviour([CanBeNull] this ITypeElement candid { return candidate.DerivesFrom(KnownTypes.MonoBehaviour); } + + public static bool DerivesFromOdinDrawer([CanBeNull] this ITypeElement candidate) + { + return candidate.DerivesFrom(OdinKnownAttributes.OdinDrawer); + } public static bool DerivesFromScriptableObject([CanBeNull] this ITypeElement candidate) {