/// <summary>
        /// Extension method that checks a <see cref="IVsCSharpSource"/> model and determines if the classes and structures in the source have any missing interface members.
        /// </summary>
        /// <param name="source">The source implementation to validate.</param>
        /// <returns>The list of missing members by target container, or an empty list if nothing is missing.</returns>
        public static IReadOnlyList <KeyValuePair <CsContainer, IReadOnlyList <CsMember> > > SourceMissingInterfaceMembers(
            this VsCSharpSource source)
        {
            if (source == null)
            {
                return(ImmutableList <KeyValuePair <CsContainer, IReadOnlyList <CsMember> > > .Empty);
            }
            if (!source.IsLoaded)
            {
                return(ImmutableList <KeyValuePair <CsContainer, IReadOnlyList <CsMember> > > .Empty);
            }

            if (!source.SourceCode.IsLoaded)
            {
                return(ImmutableList <KeyValuePair <CsContainer, IReadOnlyList <CsMember> > > .Empty);
            }

            var results = new List <KeyValuePair <CsContainer, IReadOnlyList <CsMember> > >();

            if (source.SourceCode.Classes.Any())
            {
                results.AddRange(from codeClass in source.SourceCode.Classes let members = codeClass.MissingInterfaceMembers() where members.Any() select new KeyValuePair <CsContainer, IReadOnlyList <CsMember> >(codeClass, members));
            }

            if (source.SourceCode.Structures.Any())
            {
                results.AddRange(from codeStructure in source.SourceCode.Structures let members = codeStructure.MissingInterfaceMembers() where members.Any() select new KeyValuePair <CsContainer, IReadOnlyList <CsMember> >(codeStructure, members));
            }

            return(results.ToImmutableList());
        }
예제 #2
0
        /// <summary>
        /// Extension method that loads the hosting project for the <see cref="VsCSharpSource"/> document.
        /// </summary>
        /// <param name="source">Target document to load the parent from.</param>
        /// <returns>The project model or null if the project could not be loaded.</returns>
        public static async Task <VsProject> GetHostingProjectAsync(this VsCSharpSource source)
        {
            //Bounds check if no instance of the model is provided returning null.
            if (source == null)
            {
                return(null);
            }

            //Loading the project system version of the document.
            var document = await source.LoadDocumentModelAsync();

            //If the project system version of the document could not be loaded then return null.
            if (document == null)
            {
                return(null);
            }

            //Models to store information about lookup results.
            VsProject result       = null;
            VsModel   currentModel = document;

            while (result == null)
            {
                //Confirming a model was returned otherwise there is no parent project to return, so break out of the while loop.
                if (currentModel == null)
                {
                    break;
                }

                switch (currentModel.ModelType)
                {
                //switching between each standard model types. loading model data.
                case VisualStudioModelType.Project:
                    result = currentModel as VsProject;

                    break;

                case VisualStudioModelType.ProjectFolder:

                    currentModel = currentModel is VsProjectFolder projectFolder ? await projectFolder.GetParentAsync() : null;

                    break;

                case VisualStudioModelType.Document:

                    currentModel = currentModel is VsDocument documentModel ? await documentModel.GetParentAsync() : null;

                    break;

                default:
                    currentModel = null;
                    break;
                }
            }

            return(result);
        }