Exemple #1
0
        /// <summary>
        /// Enumerates a sequence of extensions, omitting any extensions that throw MEF exceptions.
        /// </summary>
        /// <typeparam name="T">The type of extension.</typeparam>
        /// <param name="extensions">The collection of extensions.</param>
        /// <param name="onlyCreatedValues">
        /// <c>true</c> to only enumerate extensions from Lazy's that have previously been created.
        /// This is useful in Dispose methods to avoid MEF ObjectDisposedExceptions from accidentally
        /// creating values during a container disposal.
        /// </param>
        /// <returns>The safely constructed sequence of extensions.</returns>
        internal static IEnumerable <T> ExtensionValues <T>(this IEnumerable <Lazy <T> > extensions, bool onlyCreatedValues = false)
        {
            Requires.NotNull(extensions, nameof(extensions));
            var traceErrorMessage = "Roslyn project system extension rejected due to exception: {0}";

            foreach (var extension in extensions)
            {
                T value;
                try
                {
                    if (onlyCreatedValues && !extension.IsValueCreated)
                    {
                        continue;
                    }

                    value = extension.Value;
                }
                catch (CompositionContractMismatchException ex)
                {
                    TraceUtilities.TraceError(traceErrorMessage, ex);
                    continue;
                }
                catch (CompositionException ex)
                {
                    TraceUtilities.TraceError(traceErrorMessage, ex);
                    continue;
                }

                yield return(value);
            }
        }
Exemple #2
0
        public async Task <bool> IsVSFromPreviewChannelAsync()
        {
            await _threadingService.SwitchToUIThread();

            try
            {
                ISetupConfiguration vsSetupConfig = new SetupConfiguration();
                var setupInstance = vsSetupConfig.GetInstanceForCurrentProcess();
                // NOTE: this explicit cast is necessary for the subsequent COM QI to succeed.
                var setupInstanceCatalog = (ISetupInstanceCatalog)setupInstance;
                return(setupInstanceCatalog.IsPrerelease());
            }
            catch (COMException ex)
            {
                TraceUtilities.TraceError("Failed to determine whether setup instance catalog is prerelease: {0}", ex.ToString());
                return(false);
            }
        }
Exemple #3
0
        public async Task <IDependenciesTreeConfiguredProjectSearchContext?> ForConfiguredProjectAsync(ConfiguredProject configuredProject, CancellationToken cancellationToken = default)
        {
            Requires.NotNull(configuredProject, nameof(configuredProject));

            IProjectTree targetRootNode;

            if (_dependenciesNode.FindChildWithFlags(DependencyTreeFlags.TargetNode) == null)
            {
                // Tree does not show any target nodes
                targetRootNode = _dependenciesNode;
            }
            else
            {
                if (configuredProject.Services.ProjectSubscription == null)
                {
                    return(null);
                }

                IProjectSubscriptionUpdate subscriptionUpdate = (await configuredProject.Services.ProjectSubscription.ProjectRuleSource.GetLatestVersionAsync(configuredProject, cancellationToken: cancellationToken)).Value;

                if (!subscriptionUpdate.CurrentState.TryGetValue(ConfigurationGeneral.SchemaName, out IProjectRuleSnapshot configurationGeneralSnapshot) ||
                    !configurationGeneralSnapshot.Properties.TryGetValue(ConfigurationGeneral.TargetFrameworkProperty, out string tf))
                {
                    return(null);
                }

                IProjectTree?targetNode = _dependenciesNode.FindChildWithFlags(ProjectTreeFlags.Create("$TFM:" + tf));

                if (targetNode == null)
                {
                    TraceUtilities.TraceError("Should not fail to find the target node.");
                    return(null);
                }

                targetRootNode = targetNode;
            }

            return(new DependenciesTreeConfiguredProjectSearchContext(_inner, targetRootNode, _hierarchyItemManager, _projectVsServices, _relationProvider));
        }
Exemple #4
0
        public void SubmitResult(IRelatableItem?item)
        {
            if (item == null)
            {
                return;
            }

            item = DeduplicateItem(item);

            PopulateAncestors(item);

            _inner.SubmitResult(item);

            void PopulateAncestors(IRelatableItem childItem)
            {
                if (childItem.ContainedByCollection != null)
                {
                    // We've already populated this item's ancestors. It's likely an ancestor of
                    // another search result. This also prevents runaway in case of cycles.
                    return;
                }

                ImmutableArray <IRelation> containedByRelations = _relationProvider.GetContainedByRelationsFor(childItem.GetType());

                if (containedByRelations.IsEmpty)
                {
                    // We should never have a scenario where an item type does not have a parent.
                    TraceUtilities.TraceError($"No IRelation exports exist that provide parent (ContainedBy) items for type {childItem.GetType()}.");
                    return;
                }

                var allParentItems = new List <object>();

                foreach (IRelation relation in containedByRelations)
                {
                    IEnumerable <IRelatableItem>?relationParentItems = relation.CreateContainedByItems(childItem);

                    if (relationParentItems != null)
                    {
                        foreach (IRelatableItem parentItem in relationParentItems)
                        {
                            IRelatableItem deduplicateItem = DeduplicateItem(parentItem);
                            allParentItems.Add(deduplicateItem);

                            if (deduplicateItem.TryGetProjectNode(_targetRootNode, parentItem, out IProjectTree? projectNode))
                            {
                                uint             itemId        = (uint)projectNode.Identity.ToInt32();
                                IVsHierarchyItem hierarchyItem = _hierarchyItemManager.GetHierarchyItem(_projectVsServices.VsHierarchy, itemId);
                                allParentItems.Add(hierarchyItem);
                            }

                            if (deduplicateItem.ContainedByCollection == null)
                            {
                                PopulateAncestors(deduplicateItem);
                            }
                        }
                    }
                }

                childItem.ContainedByCollection = new AggregateContainedByRelationCollection(allParentItems);

                return;
            }

            IRelatableItem DeduplicateItem(IRelatableItem item)
            {
                object key = GetItemKey(item);

                if (_itemByKey.TryGetValue(key, out IRelatableItem existingItem))
                {
                    return(existingItem);
                }

                _itemByKey.Add(key, item);
                return(item);
            }
        }