Ejemplo n.º 1
0
        private void LoadSettings()
        {
            this.SettingsSets = new List <SettingsSet>();

            if (!File.Exists(this.Fp))
            {
                Debug.WriteLine("No settings overrides {0}", this.Fp);
                return;
            }

            var doc = new XmlDocument();

            Debug.WriteLine("Read settings overrides from {0}", this.Fp);
            try {
                doc.Load(this.Fp);
            }
            catch (Exception x) {
                Debug.WriteLine("Error {0}", x);
            }

            var nodes = doc.SelectNodes("RenameVSWindowTitle/SettingsSet");

            foreach (XmlElement node in nodes)
            {
                var settingsSet = new SettingsSet {
                    Paths = new List <string>()
                };

                // read paths (Path attribute and Path child elements)
                var path = node.GetAttribute(Globals.PathTag);
                if (!string.IsNullOrEmpty(path))
                {
                    settingsSet.Paths.Add(path);
                }

                var paths = node.GetElementsByTagName(Globals.PathTag);
                if (paths.Count > 0)
                {
                    foreach (XmlElement elem in paths)
                    {
                        path = elem.InnerText;
                        if (!string.IsNullOrEmpty(path))
                        {
                            settingsSet.Paths.Add(path);
                        }
                    }
                }

                TryUpdateSetting(ref settingsSet.SolutionName, node, Globals.SolutionNameTag);
                TryUpdateSetting(ref settingsSet.ClosestParentDepth, node, Globals.ClosestParentDepthTag);
                TryUpdateSetting(ref settingsSet.FarthestParentDepth, node, Globals.FarthestParentDepthTag);
                TryUpdateSetting(ref settingsSet.AppendedString, node, Globals.AppendedStringTag);
                TryUpdateSetting(ref settingsSet.PatternIfRunningMode, node, Globals.PatternIfRunningModeTag);
                TryUpdateSetting(ref settingsSet.PatternIfBreakMode, node, Globals.PatternIfBreakModeTag);
                TryUpdateSetting(ref settingsSet.PatternIfDesignMode, node, Globals.PatternIfDesignModeTag);

                this.SettingsSets.Add(settingsSet);
            }
        }
        public static AvailableInfo GetCurrent(string ideName, Solution solution, SettingsSet cfg, GlobalSettingsPageGrid globalSettings)
        {
            var info = new AvailableInfo {
                IdeName         = ideName,
                Solution        = solution,
                GlobalSettings  = globalSettings,
                Cfg             = cfg,
                ElevationSuffix = CustomizeVSWindowTitle.CurrentPackage.ElevationSuffix
            };

            try {
                info.ActiveDocument = Globals.DTE.ActiveDocument;
            }
            catch {
                // Do nothing
            }
            try {
                info.ActiveWindow = Globals.DTE.ActiveWindow;
            }
            catch {
                // Do nothing
            }
            var solutionFp = info.Solution?.FullName;

            if (info.ActiveDocument == null && string.IsNullOrEmpty(solutionFp))
            {
                if (info.ActiveWindow == null || info.ActiveWindow.Caption == Globals.DTE.MainWindow.Caption)
                {
                    return(null);
                }
            }
            info.DocumentName = DocumentHelper.GetActiveDocumentNameOrEmpty(info.ActiveDocument);
            info.DocumentPath = DocumentHelper.GetActiveDocumentPathOrEmpty(info.ActiveDocument);
            info.WindowName   = DocumentHelper.GetActiveWindowNameOrEmpty(info.ActiveWindow);

            if (!string.IsNullOrEmpty(solutionFp))
            {
                info.Path = solutionFp;
            }
            else
            {
                info.Path = info.DocumentPath;
            }

            info.PathParts = SplitPath(info.Path);
            if (!string.IsNullOrEmpty(info.Path))
            {
                info.PathParts[0] = System.IO.Path.GetPathRoot(info.Path).Replace("\\", "");
            }

            info.DocumentPathParts = SplitPath(info.DocumentPath);
            if (!string.IsNullOrEmpty(info.DocumentPath))
            {
                info.DocumentPathParts[0] = System.IO.Path.GetPathRoot(info.DocumentPath).Replace("\\", "");
            }
            return(info);
        }
Ejemplo n.º 3
0
 public void Merge(SettingsSet s)
 {
     // merge all overridable values
     merge(ref SolutionName, s.SolutionName);
     merge(ref ClosestParentDepth, s.ClosestParentDepth);
     merge(ref FarthestParentDepth, s.FarthestParentDepth);
     merge(ref AppendedString, s.AppendedString);
     merge(ref PatternIfRunningMode, s.PatternIfRunningMode);
     merge(ref PatternIfBreakMode, s.PatternIfBreakMode);
     merge(ref PatternIfDesignMode, s.PatternIfDesignMode);
 }
        private void ClearSettingsCache()
        {
            if (this.GlobalSettings.EnableDebugMode)
            {
                WriteOutput("ClearSettingsCache.");
            }

            this.SolutionSettingsWatcher.Clear();
            this.GlobalSettingsWatcher.Clear();
            this.CurrentSettingsOverride = null;
        }
 public bool Update(SettingsSet settings)
 {
     if (this.IsReloadingNeeded)
     {
         this.TryReloadSettings();
     }
     foreach (var settingsSet in this.SettingsSets)
     {
         bool bMatched;
         if (!this.IsGlobalConfig)
         {
             // ignore paths
             bMatched = true;
         }
         else
         {
             bMatched = false;
             foreach (var path in settingsSet.Paths)
             {
                 bMatched = (-1 == path.IndexOfAny(PathSeparators) ?
                             path.Equals(settings.SolutionFileName, StringComparison.CurrentCultureIgnoreCase) :
                             path.Equals(settings.SolutionFilePath, StringComparison.CurrentCultureIgnoreCase)) ||
                            (path.Contains("*") || path.Contains("?")) && new Wildcard(path, RegexOptions.IgnoreCase).IsMatch(settings.SolutionFilePath);
                 if (bMatched)
                 {
                     break;
                 }
             }
         }
         if (bMatched)
         {
             try {
                 settings.Merge(settingsSet);
                 return(true);
             }
             catch (Exception ex) {
                 try {
                     if (CustomizeVSWindowTitle.CurrentPackage.UiSettings.EnableDebugMode)
                     {
                         CustomizeVSWindowTitle.WriteOutput("settings.Merge(settingsSet) exception: " + ex);
                     }
                 }
                 catch {
                     // ignored
                 }
                 return(false);
             }
         }
     }
     return(false);
 }
        internal SettingsSet GetSettings(string solutionFp)
        {
            this.GlobalSettingsWatcher.Update(this.SettingsOverrides.GlobalSolutionSettingsOverridesFp);
            this.SolutionSettingsWatcher.Update(string.IsNullOrEmpty(solutionFp) ? null : solutionFp + Globals.SolutionSettingsOverrideExtension);

            // config already loaded, use cache
            if (this.CurrentSettingsOverride != null && this.CurrentSettingsOverride.SolutionFilePath == solutionFp)
            {
                return(this.CurrentSettingsOverride);
            }

            var settings = this.GlobalSettings;

            // init values from settings
            this.CurrentSettingsOverride = new SettingsSet {
                ClosestParentDepth   = settings.ClosestParentDepth,
                FarthestParentDepth  = settings.FarthestParentDepth,
                AppendedString       = settings.AppendedString,
                PatternIfBreakMode   = settings.PatternIfBreakMode,
                PatternIfDesignMode  = settings.PatternIfDesignMode,
                PatternIfRunningMode = settings.PatternIfRunningMode,
            };
            if (string.IsNullOrEmpty(solutionFp))
            {
                return(this.CurrentSettingsOverride);
            }

            this.CurrentSettingsOverride.SolutionFilePath = solutionFp;
            this.CurrentSettingsOverride.SolutionFileName = Path.GetFileName(solutionFp);
            this.CurrentSettingsOverride.SolutionName     = Path.GetFileNameWithoutExtension(solutionFp);

            // no override allowed, return
            if (!this.SettingsOverrides.AllowSolutionSettingsOverrides)
            {
                return(this.CurrentSettingsOverride);
            }

            // check global override file
            if (this.GlobalSettingsWatcher.Update(this.CurrentSettingsOverride))
            {
                return(this.CurrentSettingsOverride);
            }

            // check solution override file
            if (this.SolutionSettingsWatcher.Update(this.CurrentSettingsOverride))
            {
                return(this.CurrentSettingsOverride);
            }
            return(this.CurrentSettingsOverride);
        }
        internal SettingsSet GetSettings(string solutionFp)
        {
            this.GlobalSettingsWatcher.Update(this.UiSettingsOverridesOptions.GlobalSolutionSettingsOverridesFp);
            this.SolutionSettingsWatcher.Update(string.IsNullOrEmpty(solutionFp) ? null : solutionFp + Globals.SolutionSettingsOverrideExtension);

            // config already loaded, use cache
            if (this.CachedSettings != null && this.CachedSettings.SolutionFilePath == solutionFp)
            {
                return(this.CachedSettings);
            }

            // init values from settings
            var settings = new SettingsSet {
                ClosestParentDepth   = this.UiSettings.ClosestParentDepth,
                FarthestParentDepth  = this.UiSettings.FarthestParentDepth,
                AppendedString       = this.UiSettings.AppendedString,
                PatternIfBreakMode   = this.UiSettings.PatternIfBreakMode,
                PatternIfDesignMode  = this.UiSettings.PatternIfDesignMode,
                PatternIfRunningMode = this.UiSettings.PatternIfRunningMode,
            };

            if (!string.IsNullOrEmpty(solutionFp))
            {
                settings.SolutionFilePath = solutionFp;
                settings.SolutionFileName = Path.GetFileName(solutionFp);
                settings.SolutionName     = Path.GetFileNameWithoutExtension(solutionFp);

                if (!this.UiSettingsOverridesOptions.AllowSolutionSettingsOverrides)
                {
                    // Do nothing
                }
                else if (this.GlobalSettingsWatcher.Update(settings))
                {
                    // Do nothing
                }
                else if (this.SolutionSettingsWatcher.Update(settings))
                {
                    // Do nothing
                }
            }
            this.CachedSettings = settings;
            return(settings);
        }
        private string GetPattern(string solutionFp, bool useDefault, SettingsSet settingsOverride)
        {
            var settings = this.UiSettings;

            if (string.IsNullOrEmpty(solutionFp))
            {
                var document = Globals.DTE.ActiveDocument;
                var window   = Globals.DTE.ActiveWindow;
                if (string.IsNullOrEmpty(document?.FullName) && string.IsNullOrEmpty(window?.Caption))
                {
                    return(useDefault ? DefaultPatternIfNothingOpen : settings.PatternIfNothingOpen);
                }
                return(useDefault ? DefaultPatternIfDocumentButNoSolutionOpen : settings.PatternIfDocumentButNoSolutionOpen);
            }
            string designModePattern  = null;
            string breakModePattern   = null;
            string runningModePattern = null;

            if (!useDefault)
            {
                designModePattern  = settingsOverride?.PatternIfDesignMode ?? settings.PatternIfDesignMode;
                breakModePattern   = settingsOverride?.PatternIfBreakMode ?? settings.PatternIfBreakMode;
                runningModePattern = settingsOverride?.PatternIfRunningMode ?? settings.PatternIfRunningMode;
            }
            if (Globals.DTE.Debugger == null || Globals.DTE.Debugger.CurrentMode == dbgDebugMode.dbgDesignMode)
            {
                return(designModePattern ?? DefaultPatternIfDesignMode);
            }
            if (Globals.DTE.Debugger.CurrentMode == dbgDebugMode.dbgBreakMode)
            {
                return(breakModePattern ?? DefaultPatternIfBreakMode);
            }
            if (Globals.DTE.Debugger.CurrentMode == dbgDebugMode.dbgRunMode)
            {
                return(runningModePattern ?? DefaultPatternIfRunningMode);
            }
            throw new Exception("No matching state found");
        }
Ejemplo n.º 9
0
 public bool Update(SettingsSet settings)
 {
     if (this.IsReloadingNeeded)
     {
         this.TryReloadSettings();
     }
     foreach (var settingsSet in this.SettingsSets)
     {
         bool bMatched;
         if (!this.IsGlobalConfig)
         {
             // ignore paths
             bMatched = true;
         }
         else
         {
             bMatched = false;
             foreach (var path in settingsSet.Paths)
             {
                 bMatched = (-1 == path.IndexOfAny(PathSeparators) ?
                             path.Equals(settings.SolutionFileName, StringComparison.CurrentCultureIgnoreCase) :
                             path.Equals(settings.SolutionFilePath, StringComparison.CurrentCultureIgnoreCase)) ||
                            (path.Contains("*") || path.Contains("?")) && new Wildcard(path).IsMatch(settings.SolutionFilePath);
                 if (bMatched)
                 {
                     break;
                 }
             }
         }
         if (bMatched)
         {
             settings.Merge(settingsSet);
             return(true);
         }
     }
     return(false);
 }
        internal string GetNewTitle(Solution solution, string pattern, SettingsSet cfg)
        {
            var info = AvailableInfo.GetCurrent(ideName: this.IDEName, solution: solution, cfg: cfg, globalSettings: this.UiSettings);

            if (info == null)
            {
                return(this.IDEName);
            }

            pattern = TagRegex.Replace(pattern, match => {
                try {
                    var tag = match.Groups[1].Value;
                    try {
                        if (this.SimpleTagResolvers.TryGetValue(tag, out var resolver))
                        {
                            return(resolver.Resolve(info: info));
                        }
                        foreach (var tagResolver in this.TagResolvers)
                        {
                            if (tagResolver.TryResolve(tag: tag, info: info, s: out var value))
                            {
                                return(value);
                            }
                        }
        internal string GetNewTitle(Solution solution, string pattern, SettingsSet cfg)
        {
            Document activeDocument = null;
            Window   activeWindow   = null;

            try {
                activeDocument = Globals.DTE.ActiveDocument;
            }
            catch {
                // Do nothing
            }
            try {
                activeWindow = Globals.DTE.ActiveWindow;
            }
            catch {
                // Do nothing
            }
            var solutionFp = solution?.FullName;

            if (activeDocument == null && string.IsNullOrEmpty(solutionFp))
            {
                if (activeWindow == null || activeWindow.Caption == Globals.DTE.MainWindow.Caption)
                {
                    return(this.IDEName);
                }
            }
            string path;
            var    documentName = Globals.GetActiveDocumentNameOrEmpty(activeDocument);
            var    documentPath = Globals.GetActiveDocumentPathOrEmpty(activeDocument);
            var    windowName   = Globals.GetActiveWindowNameOrEmpty(activeWindow);

            if (!string.IsNullOrEmpty(solutionFp))
            {
                path = solutionFp;
            }
            else
            {
                path = documentPath;
            }

            var pathParts = this.SplitPath(path);

            if (!string.IsNullOrEmpty(path))
            {
                pathParts[0] = Path.GetPathRoot(path).Replace("\\", "");
            }

            var documentPathParts = this.SplitPath(documentPath);

            if (!string.IsNullOrEmpty(documentPath))
            {
                documentPathParts[0] = Path.GetPathRoot(documentPath).Replace("\\", "");
            }

            pattern = this.TagRegex.Replace(pattern, match => {
                try {
                    var tag = match.Groups[1].Value;
                    try {
                        switch (tag)
                        {
                        case "configurationName":
                            return(Globals.GetActiveConfigurationNameOrEmpty(solution));

                        case "platformName":
                            return(Globals.GetPlatformNameOrEmpty(solution));

                        case "projectName":
                            return(Globals.GetActiveProjectNameOrEmpty());

                        case "solutionName":
                            return(cfg.SolutionName ?? string.Empty);

                        case "gitBranchName":
                            Globals.UpdateGitExecFp(this.GlobalSettings.GitDirectory);     // there is likely a better way to adjust the git path
                            return(Globals.GetGitBranchNameOrEmpty(solution));

                        case "workspaceName":
                            return(Globals.GetWorkspaceNameOrEmpty(solution));

                        case "workspaceOwnerName":
                            return(Globals.GetWorkspaceOwnerNameOrEmpty(solution));

                        case "documentName":
                            return(string.IsNullOrEmpty(documentName) ? windowName : documentName);

                        case "documentProjectName":
                            return(Globals.GetActiveDocumentProjectNameOrEmpty(activeDocument: activeDocument));

                        case "documentProjectFileName":
                            return(Globals.GetActiveDocumentProjectFileNameOrEmpty(activeDocument: activeDocument));

                        case "documentPath":
                            return(string.IsNullOrEmpty(documentName) ? windowName : documentPath);

                        case "vsMajorVersion":
                            return(Globals.VsMajorVersion.ToString(CultureInfo.InvariantCulture));

                        case "vsMajorVersionYear":
                            return(Globals.VsMajorVersionYear.ToString(CultureInfo.InvariantCulture));

                        case "ideName":
                            return(this.IDEName ?? string.Empty);

                        case "path":
                            return(string.IsNullOrEmpty(path) ? windowName : path);

                        case "parentPath":
                            return(GetParentPath(pathParts, cfg?.ClosestParentDepth ?? this.GlobalSettings.ClosestParentDepth, cfg?.FarthestParentDepth ?? this.GlobalSettings.FarthestParentDepth) ?? string.Empty);

                        default:
                            if (tag.StartsWith("parent"))
                            {
                                var m = RangeRegex.Match(tag.Substring("parent".Length));
                                if (m.Success)
                                {
                                    if (!pathParts.Any())
                                    {
                                        return(string.Empty);
                                    }
                                    var startIndex = Math.Min(pathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["startIndex"].Value, CultureInfo.InvariantCulture)));
                                    var endIndex   = Math.Min(pathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["endIndex"].Value, CultureInfo.InvariantCulture)));
                                    var pathRange  = pathParts.GetRange(startIndex: pathParts.Length - 1 - startIndex, endIndex: pathParts.Length - 1 - endIndex).ToArray();
                                    return(GetPathForTitle(pathRange));
                                }
                                m = IndexRegex.Match(tag.Substring("parent".Length));
                                if (m.Success)
                                {
                                    if (!pathParts.Any())
                                    {
                                        return(string.Empty);
                                    }
                                    var index = Math.Min(pathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["index"].Value, CultureInfo.InvariantCulture)));
                                    return(pathParts[pathParts.Length - 1 - index]);
                                }
                            }
                            if (tag.StartsWith("path"))
                            {
                                var m = RangeRegex.Match(tag.Substring("path".Length));
                                if (m.Success)
                                {
                                    if (!pathParts.Any())
                                    {
                                        return(string.Empty);
                                    }
                                    var startIndex = Math.Min(pathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["startIndex"].Value, CultureInfo.InvariantCulture)));
                                    var endIndex   = Math.Min(pathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["endIndex"].Value, CultureInfo.InvariantCulture)));
                                    var pathRange  = pathParts.GetRange(startIndex: startIndex, endIndex: endIndex).ToArray();
                                    return(GetPathForTitle(pathRange));
                                }
                                m = IndexRegex.Match(tag.Substring("path".Length));
                                if (m.Success)
                                {
                                    if (!pathParts.Any())
                                    {
                                        return(string.Empty);
                                    }
                                    var index = Math.Min(pathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["index"].Value, CultureInfo.InvariantCulture)));
                                    return(pathParts[index]);
                                }
                            }
                            if (tag.StartsWith("documentPath"))
                            {
                                var m = RangeRegex.Match(tag.Substring("documentPath".Length));
                                if (m.Success)
                                {
                                    if (!documentPathParts.Any())
                                    {
                                        return(string.Empty);
                                    }
                                    var startIndex = Math.Min(documentPathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["startIndex"].Value, CultureInfo.InvariantCulture)));
                                    var endIndex   = Math.Min(documentPathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["endIndex"].Value, CultureInfo.InvariantCulture)));
                                    var pathRange  = documentPathParts.GetRange(startIndex: startIndex, endIndex: endIndex).ToArray();
                                    return(GetPathForTitle(pathRange));
                                }
                                m = IndexRegex.Match(tag.Substring("documentPath".Length));
                                if (m.Success)
                                {
                                    if (!documentPathParts.Any())
                                    {
                                        return(string.Empty);
                                    }
                                    var index = Math.Min(documentPathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["index"].Value, CultureInfo.InvariantCulture)));
                                    return(documentPathParts[index]);
                                }
                            }
                            if (tag.StartsWith("documentParentPath"))
                            {
                                var m = RangeRegex.Match(tag.Substring("documentParentPath".Length));
                                if (m.Success)
                                {
                                    if (!documentPathParts.Any())
                                    {
                                        return(string.Empty);
                                    }
                                    var startIndex = Math.Min(documentPathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["startIndex"].Value, CultureInfo.InvariantCulture)));
                                    var endIndex   = Math.Min(documentPathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["endIndex"].Value, CultureInfo.InvariantCulture)));
                                    var pathRange  = documentPathParts.GetRange(startIndex: documentPathParts.Length - 1 - startIndex, endIndex: documentPathParts.Length - 1 - endIndex).ToArray();
                                    return(GetPathForTitle(pathRange));
                                }
                                m = IndexRegex.Match(tag.Substring("documentParentPath".Length));
                                if (m.Success)
                                {
                                    if (!documentPathParts.Any())
                                    {
                                        return(string.Empty);
                                    }
                                    var index = Math.Min(documentPathParts.Length - 1, Math.Max(0, int.Parse(m.Groups["index"].Value, CultureInfo.InvariantCulture)));
                                    return(documentPathParts[documentPathParts.Length - 1 - index]);
                                }
                            }
                            break;
                        }
                        return(match.Value);
                    }
                    catch (Exception ex) {
                        if (this.GlobalSettings.EnableDebugMode)
                        {
                            WriteOutput("ReplaceTag (" + tag + ") failed: " + ex);
                        }
                        throw;
                    }
                }
                catch {
                    return("");
                }
            });
            var appendedString = cfg?.AppendedString ?? this.GlobalSettings.AppendedString;

            return(pattern + " " + appendedString);
        }
 private void OnSettingsCleared()
 {
     this.CurrentSettingsOverride = null; // force reload
 }