Esempio n. 1
0
        /// <summary>
        /// Find corresponding file and open it
        ///
        /// First, get the list of possible file endings
        /// Then, look in same folder
        /// Then, look in project folder and use the first match
        /// </summary>
        public static void SwitchToRelated(EnvDTE.Document document)
        {
            AutoUpdate.OnFeatureUsed();

            // This should never occur, but let's make sure it doesn't crash
            if (document == null)
            {
                return;
            }

            string path     = document.Path;
            string name     = document.Name;
            string ext      = GetExtensionExt(name);
            string name_new = null;

            List <string> list;

            // If the following fails, we don't know what we're looking for
            // There is (surprisingly!) no entry in the dictionary for that ending
            if (!Dict.TryGetValue(ext, out list))
            {
                return;
            }

            // First, look in the same folder and try all the endings we have
            foreach (string ext_new in list)
            {
                name_new = ChangeExtensionExt(name, ext_new);

                if (File.Exists(path + name_new))
                {
                    document.DTE.ItemOperations.OpenFile(path + name_new, Constants.vsViewKindAny);
                    return;
                }
            }

            Project prj = document.ProjectItem.ContainingProject;

            // If the file does not belong to a project, we don't know where to look
            if (prj == null)
            {
                return;
            }

            // Then, look in project folder and use the first match
            foreach (string ext_new in list)
            {
                name_new = Path.ChangeExtension(name, ext_new);

                foreach (ProjectItem projectItem in prj.ProjectItems)
                {
                    ProjectItem found = FindProjectItemByName(projectItem, name_new);
                    if (found != null)
                    {
                        found.Open(Constants.vsViewKindCode);
                        return;
                    }
                }
            }
        }