private void updatePreview() { this.BackColor = SystemColors.Control; var solution = Globals.DTE.Solution; var solutionFp = solution?.FullName; var activeDocument = Globals.DTE.ActiveDocument; var activeWindow = Globals.DTE.ActiveWindow; switch (this.PreviewRequires) { case PreviewRequiresAttribute.Requirement.Document: if (!(solution == null || string.IsNullOrEmpty(solutionFp))) { this.lbPreview.Text = "please close solution to enable preview!"; return; } var ad = Globals.GetActiveDocumentNameOrEmpty(activeDocument); if (string.IsNullOrEmpty(ad)) { ad = Globals.GetActiveWindowNameOrEmpty(activeWindow); } if (string.IsNullOrEmpty(ad)) { this.lbPreview.Text = "please load any document or open any window to enable preview!"; return; } break; case PreviewRequiresAttribute.Requirement.Solution: if (solution == null || string.IsNullOrEmpty(solutionFp)) { this.lbPreview.Text = "please load any solution to enable preview!"; return; } break; } var settings = RenameVSWindowTitle.CurrentPackage.GetSettings(solutionFp); var pattern = this.txEdit.Text; this.lbPreview.Text = "preview: " + RenameVSWindowTitle.CurrentPackage.GetNewTitle(solution, pattern, settings); }
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); }