private bool GetCoroutineMustUseReturnValueAttribute(IClrDeclaredElement element, out ICollection <IAttributeInstance> collection) { collection = EmptyList <IAttributeInstance> .Instance; var method = element as IMethod; var type = method?.GetContainingType(); if (type == null || !myUnityApi.IsUnityType(type)) { return(false); } var returnType = method.ReturnType; var predefinedType = myPredefinedTypeCache.GetOrCreatePredefinedType(element.Module); if (!Equals(returnType, predefinedType.IEnumerator)) { return(false); } collection = new[] { new SpecialAttributeInstance( ourMustUseReturnValueAttributeFullName, myAnnotationsPsiModule, () => new[] { new AttributeValue( new ConstantValue("Coroutine will not continue if return value is ignored", predefinedType.String)), }) }; return(true); }
private bool GetCoroutineMustUseReturnValueAttribute(IClrDeclaredElement element, out ICollection <IAttributeInstance> collection) { collection = EmptyList <IAttributeInstance> .Instance; var method = element as IMethod; var type = method?.GetContainingType(); if (type == null || !myUnityApi.IsUnityType(type)) { return(false); } var returnType = method.ReturnType; var predefinedType = myPredefinedTypeCache.GetOrCreatePredefinedType(element.Module); if (!Equals(returnType, predefinedType.IEnumerator)) { return(false); } // The ctorArguments lambda result is not cached, so let's allocate everything up front var args = new[] { new AttributeValue( new ConstantValue("Coroutine will not continue if return value is ignored", predefinedType.String)) }; collection = new[] { new SpecialAttributeInstance(ourMustUseReturnValueAttributeFullName, GetModule(element), () => args) }; return(true); }
public UnityEventFunctionQuickDocPresenter(UnityEventFunction eventFunction, IClrDeclaredElement element, QuickDocTypeMemberProvider quickDocTypeMemberProvider, ITheming theming, HelpSystem helpSystem) : this(eventFunction, null, element, quickDocTypeMemberProvider, theming, helpSystem) { myQuickDocTypeMemberProvider = quickDocTypeMemberProvider; }
private static bool IsDefinedInDependency(IClrDeclaredElement te) { var containingModule = te.Module.ContainingProjectModule; var asm = containingModule as IAssembly; return(asm != null); }
public UnityEventFunctionQuickDocPresenter(UnityEventFunction eventFunction, IClrDeclaredElement element, QuickDocTypeMemberProvider quickDocTypeMemberProvider, ITheming theming, HelpSystem helpSystem) : this(eventFunction, null, element, quickDocTypeMemberProvider, theming, helpSystem) { myQuickDocTypeMemberProvider = quickDocTypeMemberProvider; }
public UnityMessageQuickDocPresenter(UnityMessage message, IClrDeclaredElement element, QuickDocTypeMemberProvider quickDocTypeMemberProvider, ITheming theming, HelpSystem helpSystem) : this(message, null, element, quickDocTypeMemberProvider, theming, helpSystem) { myQuickDocTypeMemberProvider = quickDocTypeMemberProvider; }
public UnityEventFunctionQuickDocPresenter(UnityEventFunction eventFunction, IClrDeclaredElement element, QuickDocTypeMemberProvider quickDocTypeMemberProvider, XmlDocHtmlPresenter xmlDocHtmlPresenter, HelpSystem helpSystem) : this(eventFunction, null, element, quickDocTypeMemberProvider, xmlDocHtmlPresenter, helpSystem) { myQuickDocTypeMemberProvider = quickDocTypeMemberProvider; }
private static string GetFullyQualifiedUnityName(IClrDeclaredElement element) { var moduleName = element.Module.Name; if (moduleName.StartsWith("UnityEngine") || moduleName.StartsWith("UnityEditor")) { return(DeclaredElementPresenter.Format(KnownLanguage.ANY, MSDN_STYLE, element)); } return(string.Empty); }
public UnityEventFunctionQuickDocPresenter(UnityEventFunction eventFunction, string parameterName, IClrDeclaredElement element, QuickDocTypeMemberProvider quickDocTypeMemberProvider, ITheming theming, HelpSystem helpSystem) { myEventFunction = eventFunction; myParameterName = parameterName; myQuickDocTypeMemberProvider = quickDocTypeMemberProvider; myTheming = theming; myHelpSystem = helpSystem; myEnvoy = new DeclaredElementEnvoy<IClrDeclaredElement>(element); }
public UnityEventFunctionQuickDocPresenter(UnityEventFunction eventFunction, string parameterName, IClrDeclaredElement element, QuickDocTypeMemberProvider quickDocTypeMemberProvider, ITheming theming, HelpSystem helpSystem) { myEventFunction = eventFunction; myParameterName = parameterName; myQuickDocTypeMemberProvider = quickDocTypeMemberProvider; myTheming = theming; myHelpSystem = helpSystem; myEnvoy = new DeclaredElementEnvoy <IClrDeclaredElement>(element); }
public ICollection <IAttributeInstance> GetSpecialAttributeInstances(IClrDeclaredElement element) { if (GetCoroutineMustUseReturnValueAttribute(element, out var collection)) { return(collection); } if (GetPublicAPIImplicitlyUsedAttribute(element, out collection)) { return(collection); } return(EmptyList <IAttributeInstance> .Instance); }
internal MemberWithAccess(IClrDeclaredElement declaredElement, AccessRights typeAccessRights, MemberType memberType, AccessRights memberAccessRights) { Contract.Requires(declaredElement != null); _declaredElement = declaredElement; MemberName = declaredElement.ShortName; TypeAccessRights = typeAccessRights; MemberType = memberType; _memberAccessRights = memberAccessRights; }
private static bool B(IClrDeclaredElement element1, IClrDeclaredElement element2) { var element1SoureFile = element1.GetSourceFiles().FirstOrDefault(); var element2SourceFile = element2.GetSourceFiles().FirstOrDefault(); if (element1SoureFile == null || element2SourceFile == null) { return(element1.ToString() == element2.ToString()); } return(element1SoureFile.DisplayName == element2SourceFile.DisplayName); }
private static AccessRights GetAccessRights(IClrDeclaredElement element) { ITypeElement parentTypeElement = element.GetContainingType(); IAccessRightsOwner accessRightsOwner = element as IAccessRightsOwner; if (accessRightsOwner == null) return AccessRights.PRIVATE; IAccessRightsOwner parentAccessRightsOwner = parentTypeElement as IAccessRightsOwner; if (parentAccessRightsOwner == null) return accessRightsOwner.GetAccessRights(); return parentAccessRightsOwner.GetAccessRights(); }
private SpecialAttributeInstance[] CreateRangeAttributeInstance(IClrDeclaredElement element, PredefinedType predefinedType, long from, long to) { var args = new[] { new AttributeValue(new ConstantValue(from, predefinedType.Long)), new AttributeValue(new ConstantValue(to, predefinedType.Long)) }; return(new[] { new SpecialAttributeInstance(ourValueRangeAttributeFullName, GetModule(element), () => args) }); }
internal MemberWithAccess(IClrDeclaredElement declaredElement, AccessRights typeAccessRights, MemberType memberType, AccessRights memberAccessRights) { Contract.Requires(declaredElement != null); _declaredElement = declaredElement; MemberName = declaredElement.ShortName; TypeAccessRights = typeAccessRights; MemberType = memberType; _memberAccessRights = memberAccessRights; }
public UnityComponentRelatedReferenceExpressionFinder([NotNull] IReferenceExpression referenceExpression, bool ignoreNotComponentInvocations = false) { ReferenceExpression = referenceExpression; myIgnoreNotComponentInvocations = ignoreNotComponentInvocations; DeclaredElement = ReferenceExpression.Reference.Resolve().DeclaredElement as IClrDeclaredElement; Assertion.Assert(DeclaredElement != null, "DeclaredElement != null"); ContainingType = DeclaredElement.GetContainingType(); Assertion.Assert(ContainingType != null, "ContainingType != null"); ComponentReferenceExpression = referenceExpression.QualifierExpression as IReferenceExpression; }
private static void AppendMemberBase(this StringBuilder identifier, IClrDeclaredElement member, ISubstitution substitution, IType valueType, IDictionary <DeclaredElementInstance, IName> seenElements) { identifier.AppendType(valueType, seenElements); identifier.Append(' '); var containingType = member.GetContainingType(); identifier.AppendType(containingType, substitution, seenElements); identifier.Append('.'); identifier.Append(member.ShortName); }
public static void SetRequiredQualifiers([NotNull] FSharpSymbolReference reference, [NotNull] IClrDeclaredElement declaredElement) { var containingType = declaredElement.GetContainingType(); if (containingType == null) { return; } if (containingType.IsModule() && !containingType.RequiresQualifiedAccess()) { return; } reference.SetQualifier(containingType); }
private void CheckCommonCostlyMethods([NotNull] IInvocationExpression invocationExpressionParam, [NotNull] HotMethodAnalyzerContext context, [NotNull] IClrDeclaredElement declaredElement, [NotNull] ITypeElement containingType, [NotNull] IReference reference) { ISet <string> knownCostlyMethods = null; var clrTypeName = containingType.GetClrName(); if (clrTypeName.Equals(KnownTypes.Component)) { knownCostlyMethods = ourKnownComponentCostlyMethods; } if (clrTypeName.Equals(KnownTypes.GameObject)) { knownCostlyMethods = ourKnownGameObjectCostlyMethods; } if (clrTypeName.Equals(KnownTypes.Resources)) { knownCostlyMethods = ourKnownResourcesCostlyMethods; } if (clrTypeName.Equals(KnownTypes.Object)) { knownCostlyMethods = ourKnownObjectCostlyMethods; } if (clrTypeName.Equals(KnownTypes.Transform)) { knownCostlyMethods = ourKnownTransformCostlyMethods; } if (knownCostlyMethods == null) { return; } var shortName = declaredElement.ShortName; if (knownCostlyMethods.Contains(shortName)) { context.MarkCurrentAsCostlyReachable(); myConsumer.AddHighlighting(new PerformanceCriticalCodeInvocationHighlighting(reference)); } }
protected override bool IsReferenceExpressionNotRelated([NotNull] IReferenceExpression currentReference, IClrDeclaredElement currentElement, ITypeElement currentContainingType) { if (base.IsReferenceExpressionNotRelated(currentReference, currentElement, currentContainingType)) { return(true); } if (!currentContainingType.GetClrName().Equals(KnownTypes.Transform)) { return(true); } if (ourTransformConflicts.ContainsKey(DeclaredElement.ShortName)) { var conflicts = ourTransformConflicts[DeclaredElement.ShortName]; return(!conflicts.Contains(currentElement.ShortName)); } return(true); }
private IAttributeInstance[] CreateRangeAttributeInstance(IClrDeclaredElement element, PredefinedType predefinedType, long from, long to) { var args = new[] { new AttributeValue(new ConstantValue(from, predefinedType.Long)), new AttributeValue(new ConstantValue(to, predefinedType.Long)) }; // We need a project for the resolve context. It's not actually used, but we still need it. The requested // element will be a source element, so it will definitely have a project if (!(element.Module.ContainingProjectModule is IProject project)) { return(EmptyArray <IAttributeInstance> .Instance); } return(new IAttributeInstance[] { new AnnotationAttributeInstance(ourValueRangeAttributeFullName, GetModule(element), project, args) }); }
// Unity ships annotations, but they're out of date. Specifically, the [PublicAPI] attribute is // defined with [MeansImplicitUse] instead of [MeansImplicitUse(ImplicitUseTargetFlags.WithMembers)] // So if applied to a class, only the class is marked as in use, while the members aren't. This // provider will apply [UsedImplicitly(ImplicitUseTargetFlags.WithMembers)] to any type that // has the old [PublicAPI] applied private static bool GetPublicAPIImplicitlyUsedAttribute(IClrDeclaredElement element, out ICollection <IAttributeInstance> collection) { collection = EmptyList <IAttributeInstance> .InstanceList; if (!(element is ITypeElement type) || !element.IsFromUnityProject()) { return(false); } foreach (var attributeInstance in type.GetAttributeInstances(ourPublicAPIAttribute, false)) { var attributeType = attributeInstance.GetAttributeType(); if (!(attributeType.Resolve().DeclaredElement is ITypeElement typeElement)) { continue; } var meansImplicitUse = typeElement.GetAttributeInstances(ourMeansImplicitUseAttribute, false) .FirstOrDefault(); if (meansImplicitUse?.Constructor == null || !meansImplicitUse.Constructor.IsDefault) { continue; } var flagsType = TypeFactory.CreateTypeByCLRName(ourImplicitUseTargetFlags, element.Module); collection = new[] { new SpecialAttributeInstance( ourUsedImplicitlyAttributeFullName, element.Module, () => new[] { new AttributeValue(new ConstantValue(3, flagsType)) } ) }; return(true); } return(false); }
public ICollection <IAttributeInstance> GetSpecialAttributeInstances(IClrDeclaredElement element) { var method = element as IMethod; var type = method?.GetContainingType(); if (type != null && myUnityApi.IsUnityType(type)) { var returnType = method.ReturnType; var predefinedType = myPredefinedTypeCache.GetOrCreatePredefinedType(element.Module); if (Equals(returnType, predefinedType.IEnumerator)) { var @string = predefinedType.String; return(new[] { new SpecialAttributeInstance( OurMustUseReturnValueAttributeFullName, myAnnotationsPsiModule, () => new[] { new AttributeValue(new ConstantValue("Coroutine will not continue if return value is ignored", @string)), }) }); } } return(EmptyList <IAttributeInstance> .Instance); }
public void SetQualifier(IClrDeclaredElement declaredElement) => this.SetQualifier(this.CreateElementFactory().CreateTypeReferenceName, declaredElement);
public static void SetQualifier([NotNull] this IFSharpQualifiableReferenceOwner referenceOwner, [NotNull] Func <string, IFSharpQualifiableReferenceOwner> factory, [NotNull] IClrDeclaredElement declaredElement) { var identifier = referenceOwner.FSharpIdentifier; Assertion.Assert(identifier != null, "referenceOwner.FSharpIdentifier != null"); // todo: type args var name = FSharpReferenceBindingUtil.SuggestShortReferenceName(declaredElement, referenceOwner.Language); var delimiter = ModificationUtil.AddChildBefore(identifier, FSharpTokenType.DOT.CreateLeafElement()); var qualifier = ModificationUtil.AddChildBefore(delimiter, factory(name)); FSharpReferenceBindingUtil.SetRequiredQualifiers(qualifier.Reference, declaredElement); }
protected virtual bool IsDeclaredElementVisible(IClrDeclaredElement element) { return true; }
//------------------------------------------------------------------------------------------------------------------------ private static IList <SimpleMenuItem> DescribeFilesAssociatedWithDeclaredElement(Lifetime lifetime, DocumentManager documentManager, IClrDeclaredElement declaredElement, Func <IProjectFile, Action> clickAction) { IList <SimpleMenuItem> menuItems = new List <SimpleMenuItem>(); var projectFiles = GetProjectFiles(documentManager, declaredElement); foreach (var projectFile in projectFiles) { IProjectFile currentProjectFile = projectFile; var np = new ProjectFileNavigationPoint(currentProjectFile); var result = new SimpleMenuItemForProjectItem(np.GetPresentationText() , np.GetPresentationImage() , ResharperHelper.ProtectActionFromReEntry(lifetime, "TestingMenuNavigation", clickAction.Invoke(projectFile)) , projectFile, declaredElement ); result.ShortcutText = np.GetSecondaryPresentationText(); result.Style = MenuItemStyle.Enabled; result.Tag = projectFile.Location.FullPath; menuItems.Add(result); } return(menuItems); }
public static IDeclaredType CreateTypeInContextOf(this IClrTypeName clrTypeName, IClrDeclaredElement contextElement) { return(TypeFactory.CreateTypeByCLRName(clrTypeName, contextElement.Module)); }
public static TName GetName <TName>([NotNull] this IClrDeclaredElement element) where TName : class, IName { return(element.GetName <TName>(element.IdSubstitution)); }
public static IName GetName([NotNull] this IClrDeclaredElement element) { return(element.GetName <IName>()); }
private static TypeAndNamespace GetAccessableTypeElementAndNamespace(ICSharpTypeMemberDeclaration declaration, ISolution solution, ICSharpFile file, IClrDeclaredElement element, IdentifierLookupScopes scope) { //IPsiModule module = element.Module; IXmlDocIdOwner idOwner = element as IXmlDocIdOwner; string docId = idOwner == null ? element.ShortName : idOwner.XMLDocId; // Get the defining type. ITypeElement typeElement = element as ITypeElement ?? element.GetContainingType(); if (typeElement == null) return null; // Create the result TypeAndNamespace result = new TypeAndNamespace { XmlDocId = docId, TypeElement = element }; // Get the namespace it belongs to. INamespace namespaceElement = typeElement.GetContainingNamespace(); string namespaceName = namespaceElement.QualifiedName; // Check if we're ignoring this namespace foreach (string namespacePrefix in NamespacePrefixesToIgnore) { if (namespaceName.StartsWith(namespacePrefix)) return null; } // Check if it would be possible to access the type AccessRights elementAccessRights = GetAccessRights(element); if (elementAccessRights == AccessRights.PRIVATE) { return null; } // Check if the type is defined in this solution IList<IDeclaration> declarations = element.GetDeclarations(); if (declarations.Count == 0) { // Assembly is an import so no internal things allowed if (elementAccessRights == AccessRights.INTERNAL) return null; } // Check if the given namespace is already imported in this file. if (UsingUtil.CheckAlreadyImported(file, new DeclaredElementInstance(namespaceElement)) || declaration.GetContainingNamespaceDeclaration().QualifiedName.StartsWith(namespaceName)) { string newDocId = docId[1] == ':' ? docId.Substring(2) : docId; if (newDocId.StartsWith(namespaceName + ".")) newDocId = newDocId.Substring(namespaceName.Length + 1); result.XmlDocId = newDocId; return result; } // If we require it to be in project or using scope then this is not a match if (scope == IdentifierLookupScopes.ProjectAndUsings || scope == IdentifierLookupScopes.ProjectOnly) { return null; } // No - so add in the namespace. result.RequiredNamespaceImport = namespaceElement; return result; }
private IPsiModule GetModule(IClrDeclaredElement element) { return(myExternalAnnotationsModuleFactory.GetPsiModule(element.Module.TargetFrameworkId) ?? element.Module); }
// Treat Unity's RangeAttribute as ReSharper's ValueRangeAttribute annotation private bool GetValueRangeAttribute(IClrDeclaredElement element, out ICollection <IAttributeInstance> collection) { collection = EmptyList <IAttributeInstance> .InstanceList; if (!(element is IField field) || !element.IsFromUnityProject()) { return(false); } if (!myUnityApi.IsSerialisedField(field)) { return(false); } // Integer value analysis only works on integers, but it will make use of annotations applied to values that // are convertible to int, such as byte/sbyte and short/ushort. It doesn't currently use values applied to // uint, or long/ulong, but it is planned, so we'll apply to all sizes of integer. var predefinedType = myPredefinedTypeCache.GetOrCreatePredefinedType(element.Module); if (!Equals(field.Type, predefinedType.Int) && !Equals(field.Type, predefinedType.Uint) && !Equals(field.Type, predefinedType.Long) && !Equals(field.Type, predefinedType.Ulong) && !Equals(field.Type, predefinedType.Short) && !Equals(field.Type, predefinedType.Ushort) && !Equals(field.Type, predefinedType.Byte) && !Equals(field.Type, predefinedType.Sbyte)) { return(false); } foreach (var attributeInstance in field.GetAttributeInstances(KnownTypes.RangeAttribute, false)) { // Values are floats, but applied to an integer field. Convert to integer values var unityFrom = attributeInstance.PositionParameter(0); var unityTo = attributeInstance.PositionParameter(1); if (!unityFrom.IsConstant || !unityFrom.ConstantValue.IsFloat() || !unityTo.IsConstant || !unityTo.ConstantValue.IsFloat()) { continue; } // The check above means this is not null. We take the floor, because that's how Unity works. // E.g. Unity's Inspector treats [Range(1.7f, 10.9f)] as between 1 and 10 inclusive var from = Convert.ToInt64(Math.Floor((float)unityFrom.ConstantValue.Value.NotNull())); var to = Convert.ToInt64(Math.Floor((float)unityTo.ConstantValue.Value.NotNull())); collection = CreateRangeAttributeInstance(element, predefinedType, from, to); return(true); } foreach (var attributeInstance in field.GetAttributeInstances(KnownTypes.MinAttribute, false)) { var unityMinValue = attributeInstance.PositionParameter(0); if (!unityMinValue.IsConstant || !unityMinValue.ConstantValue.IsFloat()) { continue; } // Even though the constructor for ValueRange takes long, it only works with int.MaxValue var min = Convert.ToInt64(Math.Floor((float)unityMinValue.ConstantValue.Value.NotNull())); var max = int.MaxValue; collection = CreateRangeAttributeInstance(element, predefinedType, min, max); return(true); } return(false); }
protected ITypeConversionRule GetTypeConversionRule(IClrDeclaredElement declaredElement) { return new CSharpTypeConversionRule(declaredElement.Module); }