public bool SuppressUsageInspectionsOnElement(IDeclaredElement element, out ImplicitUseKindFlags flags)
        {
            flags = ImplicitUseKindFlags.Default;
            ITypeElement typeElement = element as ITypeElement;

            if (typeElement == null)
            {
                return(false);
            }

            var            patternManager = typeElement.GetSolution().GetComponent <IPatternManager>();
            IPsiSourceFile file           = typeElement.GetSingleOrDefaultSourceFile();

            if (file == null)
            {
                return(false);
            }

            IEnumerable <RegistrationInfo> registrations = patternManager.GetRegistrationsForFile(file);

            if (registrations.Any(info => info.Registration.IsSatisfiedBy(typeElement)))
            {
                flags = ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature;
                return(true);
            }

            return(false);
        }
Exemple #2
0
        private static IReadOnlyCollection <ITypeElement> GetLinkedTypesInternal(ITypeElement source)
        {
            var settings      = GetSettings(source.GetSolution());
            var derivedNames  = GetDerivedNames(source, settings.NamingSuffixes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
            var attributeName = settings.TypeofAttributeName.TrimFromEnd("Attribute");

            var typeofs = GetAttributeLinkedTypes(source, attributeName);

            if (typeofs != null)
            {
                return(typeofs.ToList());
            }

            var psiServices = source.GetPsiServices();

            var symbolCache = psiServices.Symbols.GetSymbolScope(LibrarySymbolScope.NONE, caseSensitive: true);
            var linkedTypes = derivedNames.SelectMany(x => symbolCache.GetElementsByShortName(x)).ToList();

            var wordIndex    = psiServices.WordIndex;
            var sourceFiles  = wordIndex.GetFilesContainingAllWords(new[] { source.ShortName });
            var typesInFiles = sourceFiles
                               .SelectMany(x => psiServices.Symbols.GetTypesAndNamespacesInFile(x))
                               .OfType <ClassLikeTypeElement>()
                               .Where(x => GetAttributeLinkedTypes(x, attributeName)?.Contains(source) ?? false);

            linkedTypes.AddRange(typesInFiles);

            return(linkedTypes.Where(x => !x.Equals(source)).Cast <ITypeElement>().ToList());
        }
Exemple #3
0
        private static MethodSignature GetMethodSignature(ITypeElement owningType, IType fieldType)
        {
            var cache          = owningType.GetSolution().GetComponent <IPredefinedTypeCache>();
            var predefinedType = cache.GetOrCreatePredefinedType(owningType.Module);
            var @void          = predefinedType.Void;

            return(new MethodSignature(@void, null, new[] { fieldType }, new[] { "value" }));
        }
        public static bool DerivesFrom([CanBeNull] this ITypeElement candidate, IClrTypeName baseTypeName)
        {
            if (candidate == null)
            {
                return(false);
            }
            var knownTypesCache = candidate.GetSolution().GetComponent <KnownTypesCache>();
            var baseType        = GetTypeElement(baseTypeName, knownTypesCache, candidate.Module);

            return(candidate.IsDescendantOf(baseType));
        }
Exemple #5
0
        public static bool HasPossibleDerivedTypesWithMember(Guid ownerGuid, ITypeElement containingType, IEnumerable <string> memberNames, OneToCompactCountingSet <int, Guid> nameHashToGuids)
        {
            var count = 0;

            foreach (var possibleName in memberNames)
            {
                var values = nameHashToGuids.GetValues(possibleName.GetPlatformIndependentHashCode());
                count += values.Length;
                if (values.Length == 1 && !values[0].Equals(ownerGuid))
                {
                    count++;
                }
            }

            if (count > 1)
            {
                // TODO: drop daemon dependency and inject compoentns in consructor
                var configuration = containingType.GetSolution().GetComponent <SolutionAnalysisConfiguration>();
                if (configuration.Enabled.Value && configuration.CompletedOnceAfterStart.Value &&
                    configuration.Loaded.Value)
                {
                    var service = containingType.GetSolution().GetComponent <SolutionAnalysisService>();
                    var id      = service.GetElementId(containingType);
                    if (id.HasValue && service.UsageChecker is IGlobalUsageChecker checker)
                    {
                        // no inheritors
                        if (checker.GetDerivedTypeElementsCount(id.Value) == 0)
                        {
                            return(false);
                        }
                    }
                }

                return(true);
            }

            return(false);
        }
Exemple #6
0
        public bool IsIndexResultEstimated(string ownerGuid, ITypeElement containingType, IEnumerable <string> possibleNames)
        {
            myShellLocks.AssertReadAccessAllowed();

            // TODO: prefab modifications
            // TODO: drop daemon dependency and inject compoentns in consructor
            var configuration = containingType.GetSolution().GetComponent <SolutionAnalysisConfiguration>();

            if (configuration.Enabled.Value && configuration.CompletedOnceAfterStart.Value && configuration.Loaded.Value)
            {
                var service = containingType.GetSolution().GetComponent <SolutionAnalysisService>();
                var id      = service.GetElementId(containingType);
                if (id.HasValue && service.UsageChecker is IGlobalUsageChecker checker)
                {
                    // no inheritors
                    if (checker.GetDerivedTypeElementsCount(id.Value) == 0)
                    {
                        return(false);
                    }
                }
            }

            var count = 0;

            foreach (var possibleName in possibleNames)
            {
                var values = myNameToGuids.GetValues(possibleName);
                count += values.Length;
                if (values.Length == 1 && !values[0].Equals(ownerGuid))
                {
                    count++;
                }
            }

            return(count > 1);
        }
Exemple #7
0
        public static void TryCreateTestOrProductionClass(ITypeElement sourceType, ITextControl textControl)
        {
            var solution = sourceType.GetSolution();

            var typesNearCaretOrFile = solution.GetComponent <ITypesFromTextControlService>().GetTypesNearCaretOrFile(textControl, solution);
            var templateTypes        = typesNearCaretOrFile.Select(GetLinkedTypeWithDerivedName).WhereNotNull().FirstOrDefault();

            if (templateTypes == null)
            {
                MessageBox.ShowInfo(
                    "Could not find a template to create production/test class from.\r\n" +
                    "There must exist at least one pair of production+test classes for this project.");
                return;
            }

            var templateSourceType = templateTypes.Item1;
            var templateLinkedType = templateTypes.Item2;

            var linkedTypeName       = DerivedNameUtility.GetDerivedName(sourceType.ShortName, templateSourceType.ShortName, templateLinkedType.ShortName);
            var linkedTypeNamespace  = DerivedNameUtility.GetDerivedNamespace(sourceType, templateLinkedType);
            var linkedTypeSourceFile = templateLinkedType.GetSingleOrDefaultSourceFile().NotNull("linkedTypeSourceFile != null");
            var linkedTypeProject    = linkedTypeSourceFile.GetProject().NotNull("linkedTypeProject != null");
            var linkedTypeKind       = !solution.GetComponent <IUnitTestElementStuff>()
                                       .IsElementOfKind(templateLinkedType, UnitTestElementKind.TestContainer)
                ? TypeKind.Production
                : TypeKind.Test;

            if (!MessageBox.ShowYesNo(
                    $"Class: {linkedTypeName}\r\nProject: {linkedTypeProject.Name}\r\nNamespace: {linkedTypeNamespace}\r\n",
                    $"Create {linkedTypeKind} Class for {sourceType.ShortName}?"))
            {
                return;
            }

            var threading = solution.GetComponent <IThreading>();

            threading.ExecuteOrQueueEx(
                nameof(TryCreateTestOrProductionClass),
                () =>
            {
                var linkedTypeProjectFolder = GetLinkedTypeFolder(linkedTypeNamespace, linkedTypeProject);
                var linkedTypeFile          = GetLinkedTypeFile(linkedTypeName, linkedTypeNamespace, templateLinkedType);
                var linkedTypeProjectFile   = AddNewItemHelper.AddFile(linkedTypeProjectFolder,
                                                                       linkedTypeName + ".cs", linkedTypeFile.GetText());
                linkedTypeProjectFile.Navigate(Shell.Instance.GetComponent <IMainWindowPopupWindowContext>().Source,
                                               transferFocus: true);
            });
        }
    public TypeInterfaceDescriptor(ITypeElement typeElement, bool instanceOnly) : base(typeElement.GetSolution())
    {
      AutoExpandSingleChild = true;
      myInstanceOnly = instanceOnly;

      // We use standard presenter, but emphasize root element using adorements
      myPresenter = new TypeInterfacePresenter
                      {
                        DrawElementExtensions = true,
                        ShowOccurenceCount = false,
                        PostfixTypeQualification = true
                      };
      myPresenter.PresentAdorements += PresentAdorements;

      // Wrap typeElement with an envoy, so it can survive code changes
      myTypeElementEnvoy = new DeclaredElementEnvoy<ITypeElement>(typeElement);
      MakeModel();
    }
        private void ProcessAbstractClass(ITypeElement typeElement)
        {
            if (classes.ContainsKey(typeElement))
                return;

            var fixtures = new HashSet<XunitTestClassElement>();
            ISolution solution = typeElement.GetSolution();
            var inheritorsConsumer = new XunitFileExplorerInheritorsConsumer();
            var searchDomain = searchDomainFactory.CreateSearchDomain(solution, true);
            solution.GetPsiServices()
                .Finder.FindInheritors(typeElement, searchDomain, inheritorsConsumer, NullProgressIndicator.Instance);
            var foundElements = inheritorsConsumer.FoundElements;

            foreach (var inheritor in foundElements)
            {
                IProject projectElement = project;
                ProjectModelElementEnvoy projectEnvoy = envoy;
                var declaration = inheritor.GetDeclarations().FirstOrDefault();
                if (declaration != null)
                {
                    projectElement = declaration.GetProject();
                    if (!Equals(projectElement, project))
                        projectEnvoy = ProjectModelElementEnvoy.Create(projectElement);
                }
                var fixtureElement = GetOrCreateClassElement(inheritor, projectElement, projectEnvoy);
                fixtures.Add(fixtureElement);
                foreach (IDeclaredType type in inheritor.GetAllSuperTypes())
                    ProcessSuperType(fixtureElement, type);
            }
            classes.Add(typeElement, fixtures);
        }
        public TypeInterfaceDescriptor(ITypeElement typeElement, bool instanceOnly) : base(typeElement.GetSolution())
        {
            AutoExpandSingleChild = true;
            myInstanceOnly        = instanceOnly;

            // We use standard presenter, but emphasize root element using adorements
            myPresenter = new TypeInterfacePresenter
            {
                DrawElementExtensions    = true,
                ShowOccurenceCount       = false,
                PostfixTypeQualification = true
            };
            myPresenter.PresentAdorements += PresentAdorements;

            // Wrap typeElement with an envoy, so it can survive code changes
            myTypeElementEnvoy = new DeclaredElementEnvoy <ITypeElement>(typeElement);
            MakeModel();
        }