private static Dictionary <string, List <PreviewMatch> > PreviewRenameGlobalMacros(Regex regex, List <TabInfo> tabs, List <string> files) { ProgressBarForm pf = (files.Count > 100) ? new ProgressBarForm(Form.ActiveForm, files.Count, "Идет поиск совпадений в файлах проекта...") : null; Dictionary <string, List <PreviewMatch> > preview = new Dictionary <string, List <PreviewMatch> >(); // file => mathes foreach (var file in files) { if (pf != null) { pf.IncProgress(); } if (TextEditor.CheckTabs(file, tabs)) { continue; // next file } string textContent = System.IO.File.ReadAllText(file); MatchesCollect(ref preview, regex, file, textContent); } if (pf != null) { pf.Dispose(); } return(preview); }
private static void RenameGlobalMacros(Macro macros, string newName, TabInfo cTab, List <TabInfo> tabs, int diff) { Regex s_regex = new Regex(@"\b" + macros.token + @"\b", RegexOptions.None); // preview renamed macros open scripts Dictionary <string, List <PreviewMatch> > matchTabs = PreviewRenameGlobalMacros(s_regex, tabs); // file => mathes List <string> files = Directory.GetFiles(Settings.solutionProjectFolder, "*.ssl", SearchOption.AllDirectories).ToList(); files.AddRange(Directory.GetFiles(Settings.solutionProjectFolder, "*.h", SearchOption.AllDirectories).ToList()); // preview renamed macros open scripts Dictionary <string, List <PreviewMatch> > matchFiles = PreviewRenameGlobalMacros(s_regex, tabs, files); // выбор совпадений var previewForm = new PreviewRename(macros.defname, macros.defname.Replace(macros.token, newName)); previewForm.BuildTreeMatches(matchTabs, matchFiles); matchTabs.Clear(); matchFiles.Clear(); if (previewForm.ShowDialog() != DialogResult.OK) { return; } previewForm.GetSelectedMatches(ref matchTabs, ref matchFiles); previewForm.Dispose(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int isAdjustSpaces = diff; cTab.DisableParseAndStatusChange = true; int replaceLen = macros.token.Length; foreach (var tabMatches in matchTabs) { TabInfo tab = null; int replace_count = 0; foreach (var matches in tabMatches.Value) { tab = matches.tab; Match match = matches.match; int offset = (diff * replace_count) + match.Index; tab.textEditor.Document.Replace(offset, replaceLen, newName); replace_count++; } if (tab != null) { var document = tab.textEditor.Document; if (isAdjustSpaces != 0 && string.Equals(tab.filepath, macros.fdeclared, StringComparison.OrdinalIgnoreCase)) { DefineMacroAdjustSpaces(macros, document, diff); isAdjustSpaces = 0; } document.UndoStack.ClearAll(); tab.SaveInternal(document.TextContent, tab.textEditor.Encoding); // сохранить изменения в файл } } if (matchFiles.Count == 0) { cTab.DisableParseAndStatusChange = false; return; } // замена в файлах проекта ProgressBarForm pf = (matchFiles.Count > 100) ? new ProgressBarForm(Form.ActiveForm, matchFiles.Count, "Идет замена в файлах проекта...") : null; int total = 0; foreach (var fileMatches in matchFiles) { string textContent = System.IO.File.ReadAllText(fileMatches.Key); total += fileMatches.Value.Count; int replace_count = 0; foreach (var matches in fileMatches.Value) { int offset = (diff * replace_count) + matches.match.Index; textContent = textContent.Remove(offset, matches.match.Length); textContent = textContent.Insert(offset, newName); replace_count++; } if (isAdjustSpaces != 0 && string.Equals(fileMatches.Key, macros.fdeclared, StringComparison.OrdinalIgnoreCase)) { DefineMacroAdjustSpaces(macros, newName, ref textContent, diff); isAdjustSpaces = 0; } if (replace_count > 0) { File.WriteAllText(fileMatches.Key, textContent, (Settings.saveScriptUTF8) ? new UTF8Encoding(false) : Encoding.Default); } if (pf != null) { pf.IncProgress(); } } if (pf != null) { pf.Dispose(); } //MessageBox.Show(String.Format("Произведено переименование {0} макросов, в {1} файлах.", total, previewFiles.Count)); cTab.DisableParseAndStatusChange = false; }