Ejemplo n.º 1
0
        //=====================================================================

        /// <summary>
        /// This is used to determine the containing project and settings filename when adding a new spell
        /// checker configuration file.
        /// </summary>
        /// <param name="containingProject">On return, this contains the containing project or null if adding
        /// a solution configuration file.</param>
        /// <param name="settingsFilename">On return, this contains the name of the settings file to be added</param>
        /// <returns>True if a settings file can be added for the item selected in the Solution Explorer window
        /// or false if not.</returns>
        private static bool DetermineContainingProjectAndSettingsFile(out Project containingProject,
                                                                      out string settingsFilename)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            string folderName = null;

            containingProject = null;
            settingsFilename  = null;

            // Only add if a single file is selected
            if (!(Package.GetGlobalService(typeof(SDTE)) is DTE2 dte2) || dte2.SelectedItems.Count != 1)
            {
                return(false);
            }

            SelectedItem item = dte2.SelectedItems.Item(1);

            if (item.Project != null && item.Project.Kind != EnvDTE.Constants.vsProjectKindSolutionItems &&
                item.Project.Kind != EnvDTE.Constants.vsProjectKindUnmodeled &&
                item.Project.Kind != EnvDTE.Constants.vsProjectKindMisc)
            {
                string path = null;

                // Looks like a project.  Not all of them implement properties though.
                if (!String.IsNullOrWhiteSpace(item.Project.FullName) && item.Project.FullName.EndsWith(
                        "proj", StringComparison.OrdinalIgnoreCase))
                {
                    path = item.Project.FullName;
                }

                if (path == null && item.Project.Properties != null)
                {
                    Property fullPath;

                    try
                    {
                        fullPath = item.Project.Properties.Item("FullPath");
                    }
                    catch
                    {
                        // C++ projects use a different property name and throw an exception above
                        try
                        {
                            fullPath = item.Project.Properties.Item("ProjectFile");
                        }
                        catch
                        {
                            // If that fails, give up
                            fullPath = null;
                        }
                    }

                    if (fullPath != null && fullPath.Value != null)
                    {
                        path = (string)fullPath.Value;
                    }
                }

                if (!String.IsNullOrWhiteSpace(path))
                {
#pragma warning disable VSTHRD010
                    var project = dte2.Solution.EnumerateProjects().FirstOrDefault(p => p.Name == item.Name);
#pragma warning restore VSTHRD010

                    if (project != null)
                    {
                        containingProject = project;
                        settingsFilename  = project.FullName;

                        // Website projects are named after the folder rather than a file
                        if (settingsFilename.Length > 1 && settingsFilename[settingsFilename.Length - 1] == '\\')
                        {
                            folderName        = settingsFilename;
                            settingsFilename += item.Name;
                        }
                    }
                }
            }
            else
            if (item.ProjectItem == null || item.ProjectItem.ContainingProject == null)
            {
                // Looks like a solution
                if (Path.GetFileNameWithoutExtension(dte2.Solution.FullName) == item.Name)
                {
                    settingsFilename = dte2.Solution.FullName;
                }
            }
            else
            if (item.ProjectItem.Properties != null)
            {
                // Looks like a folder or file item
                Property fullPath;

                try
                {
                    fullPath = item.ProjectItem.Properties.Item("FullPath");
                }
                catch
                {
                    fullPath = null;
                }

                if (fullPath != null && fullPath.Value != null)
                {
                    string path = (string)fullPath.Value;

                    if (!String.IsNullOrWhiteSpace(path))
                    {
                        containingProject = item.ProjectItem.ContainingProject;

                        // Folder items have a trailing backslash in some project systems, others don't.
                        // We'll put the configuration file in the folder using its name as the filename.
                        if (path[path.Length - 1] == '\\' || (!File.Exists(path) && Directory.Exists(path)))
                        {
                            if (path[path.Length - 1] != '\\')
                            {
                                path += @"\";
                            }

                            folderName       = path;
                            settingsFilename = path + item.Name;
                        }
                        else
                        {
                            settingsFilename = path;
                        }
                    }
                }
            }
            else
            if (item.ProjectItem.Kind == EnvDTE.Constants.vsProjectItemKindSolutionItems)
            {
                // Looks like a solution item
                settingsFilename = item.ProjectItem.get_FileNames(1);
            }

            if (settingsFilename != null)
            {
                if (settingsFilename.EndsWith(".vsspell", StringComparison.OrdinalIgnoreCase) ||
                    ((folderName == null && !File.Exists(settingsFilename)) ||
                     (folderName != null && !Directory.Exists(folderName))))
                {
                    settingsFilename = null;
                }
                else
                if (folderName == null)
                {
                    if (SpellCheckFileInfo.IsBinaryFile(settingsFilename))
                    {
                        settingsFilename = null;
                    }
                    else
                    {
                        settingsFilename += ".vsspell";
                    }
                }
                else
                {
                    settingsFilename += ".vsspell";
                }
            }

            return(settingsFilename != null);
        }
Ejemplo n.º 2
0
        //=====================================================================

        /// <summary>
        /// This is used to determine the containing project and settings filename when adding a new spell
        /// checker configuration file.
        /// </summary>
        /// <param name="containingProject">On return, this contains the containing project or null if adding
        /// a solution configuration file.</param>
        /// <param name="settingsFilename">On return, this contains the name of the settings file to be added</param>
        /// <returns>True if a settings file can be added for the item selected in the Solution Explorer window
        /// or false if not.</returns>
        private static bool DetermineContainingProjectAndSettingsFile(out Project containingProject,
                                                                      out string settingsFilename)
        {
            string folderName = null;

            containingProject = null;
            settingsFilename  = null;

            var dte2 = Utility.GetServiceFromPackage <DTE2, SDTE>(true);

            // Only add if a single file is selected
            if (dte2 == null || dte2.SelectedItems.Count != 1)
            {
                return(false);
            }

            SelectedItem item = dte2.SelectedItems.Item(1);

            if (item.Project != null && item.Project.Kind != EnvDTE.Constants.vsProjectKindSolutionItems &&
                item.Project.Kind != EnvDTE.Constants.vsProjectKindUnmodeled &&
                item.Project.Kind != EnvDTE.Constants.vsProjectKindMisc)
            {
                // Looks like a project
                Property fullPath = item.Project.Properties.Item("FullPath");

                if (fullPath != null && fullPath.Value != null)
                {
                    string path = (string)fullPath.Value;

                    if (!String.IsNullOrWhiteSpace(path))
                    {
                        var project = dte2.Solution.EnumerateProjects().FirstOrDefault(p => p.Name == item.Name);

                        if (project != null)
                        {
                            containingProject = project;
                            settingsFilename  = project.FullName;

                            // Website projects are named after the folder rather than a file
                            if (settingsFilename.Length > 1 && settingsFilename[settingsFilename.Length - 1] == '\\')
                            {
                                folderName        = settingsFilename;
                                settingsFilename += item.Name;
                            }
                        }
                    }
                }
            }
            else
            if (item.ProjectItem == null || item.ProjectItem.ContainingProject == null)
            {
                // Looks like a solution
                if (Path.GetFileNameWithoutExtension(dte2.Solution.FullName) == item.Name)
                {
                    settingsFilename = dte2.Solution.FullName;
                }
            }
            else
            if (item.ProjectItem.Properties != null)
            {
                // Looks like a folder or file item
                Property fullPath = item.ProjectItem.Properties.Item("FullPath");

                if (fullPath != null && fullPath.Value != null)
                {
                    string path = (string)fullPath.Value;

                    if (!String.IsNullOrWhiteSpace(path))
                    {
                        containingProject = item.ProjectItem.ContainingProject;

                        // Folder items have a trailing backslash.  We'll put the configuration file in
                        // the folder using its name as the filename.
                        if (path[path.Length - 1] == '\\')
                        {
                            folderName       = path;
                            settingsFilename = path + item.Name;
                        }
                        else
                        {
                            settingsFilename = path;
                        }
                    }
                }
            }
            else
            if (item.ProjectItem.Kind == EnvDTE.Constants.vsProjectItemKindSolutionItems)
            {
                // Looks like a solution item
                settingsFilename = item.ProjectItem.get_FileNames(1);
            }

            if (settingsFilename != null)
            {
                if (settingsFilename.EndsWith(".vsspell", StringComparison.OrdinalIgnoreCase) ||
                    ((folderName == null && !File.Exists(settingsFilename)) ||
                     (folderName != null && !Directory.Exists(folderName))))
                {
                    settingsFilename = null;
                }
                else
                if (folderName == null)
                {
                    if (SpellCheckFileInfo.IsBinaryFile(settingsFilename))
                    {
                        settingsFilename = null;
                    }
                    else
                    {
                        settingsFilename += ".vsspell";
                    }
                }
                else
                {
                    settingsFilename += ".vsspell";
                }
            }

            return(settingsFilename != null);
        }