/// <summary>
        /// Extension method designed to look in the parent project folder, or project of the current source document and find a target code file in that location.
        /// </summary>
        /// <param name="source">The source model.</param>
        /// <param name="targetFilePath">The fully qualified path to the target model.</param>
        /// <returns>The loaded source or null if the source could not be loaded.</returns>
        public static async Task <CsSource> GetCsSourceDocumentFromParent(this VsCSharpSource source, string targetFilePath)
        {
            if (source == null)
            {
                return(null);
            }
            if (string.IsNullOrEmpty(targetFilePath))
            {
                return(null);
            }

            var parentData = await source.GetCSharpSourceDocumentParentAsync();

            var parent = parentData.ParentModel;

            if (parent == null)
            {
                throw new CodeFactoryException("Source document is not hosted in a project or project folder.");
            }

            VsCSharpSource sourceDocument = null;

            if (!parentData.IsProject)
            {
                var parentFolder = parent as VsProjectFolder;

                if (parentFolder == null)
                {
                    throw new CodeFactoryException("Cannot access the parent of the source code document");
                }

                var children = await parentFolder.GetChildrenAsync(false, true);

                sourceDocument = children.Where(c => c.ModelType == VisualStudioModelType.CSharpSource)
                                 .Cast <VsCSharpSource>()
                                 .FirstOrDefault(s => s.SourceCode.SourceDocument == targetFilePath);
            }
            else
            {
                var parentProject = parent as VsProject;

                if (parentProject == null)
                {
                    throw new CodeFactoryException("Cannot access the parent of the source code document, cannot update subscription");
                }

                var children = await parentProject.GetChildrenAsync(false, true);

                sourceDocument = children.Where(c => c.ModelType == VisualStudioModelType.CSharpSource)
                                 .Cast <VsCSharpSource>()
                                 .FirstOrDefault(s => s.SourceCode.SourceDocument == targetFilePath);;
            }

            return(sourceDocument?.SourceCode);
        }
        /// <summary>
        /// Adds a new C# document to the parent project or folder of the current c# document.
        /// </summary>
        /// <param name="source">C# source document.</param>
        /// <param name="sourceCode">The source code to be added to the new code file.</param>
        /// <param name="targetFileName">The target file name of the new document.</param>
        /// <returns></returns>
        public static async Task <CsSource> AddCSharpCodeFileToParentAsync(this VsCSharpSource source, string sourceCode,
                                                                           string targetFileName)
        {
            if (source == null)
            {
                throw new CodeFactoryException("No visual studio c# source was provided cannot add C# code file.");
            }
            if (string.IsNullOrEmpty(targetFileName))
            {
                throw new CodeFactoryException("No filename was provided cannot add the C# code file.");
            }

            var parent = await source.GetCSharpSourceDocumentParentAsync();

            if (parent.ParentModel == null)
            {
                throw new CodeFactoryException("No project or project folder was found, cannot add the C# code file.");
            }


            VsDocument document = null;

            if (parent.IsProject)
            {
                var project = parent.ParentModel as VsProject;

                if (project == null)
                {
                    throw new CodeFactoryException("Could load the project information, cannot add the C# code file.");
                }

                document = await project.AddDocumentAsync(targetFileName, sourceCode);
            }
            else
            {
                var projectFolder = parent.ParentModel as VsProjectFolder;

                if (projectFolder == null)
                {
                    throw new CodeFactoryException("Could load the project folder information, cannot add the C# code file.");
                }

                document = await projectFolder.AddDocumentAsync(targetFileName, sourceCode);
            }

            var csDocument = await document.GetCSharpSourceModelAsync();

            return(csDocument);
        }