/// <summary> /// Invoked when the FRSearch completes its search /// </summary> private void FindFinished(FRResults results) { UserInterfaceManager.ProgressDialog.Reset(); UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.ResolvingReferences")); // First filter out any results that don't actually point to our source declaration this.Results = ResolveActualMatches(results, currentTarget); if (this.outputResults) { this.ReportResults(); } UserInterfaceManager.ProgressDialog.Hide(); // Select first match if (this.Results.Count > 0) { foreach (var fileEntries in this.Results) { if (fileEntries.Value.Count > 0 && System.IO.File.Exists(fileEntries.Key)) { SearchMatch entry = fileEntries.Value[0]; PluginBase.MainForm.OpenEditableDocument(fileEntries.Key, false); RefactoringHelper.SelectMatch(PluginBase.MainForm.CurrentDocument.SciControl, entry); break; } } } this.FireOnRefactorComplete(); }
private void FindFinished(FRResults results) { UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.UpdatingReferences")); MessageBar.Locked = true; bool isNotHaxe = !PluginBase.CurrentProject.Language.StartsWith("haxe"); bool packageIsNotEmpty = !string.IsNullOrEmpty(currentTarget.OldFileModel.Package); string targetName = Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); string oldType = (currentTarget.OldFileModel.Package + "." + targetName).Trim('.'); string newType = (currentTarget.NewPackage + "." + targetName).Trim('.'); foreach (KeyValuePair <string, List <SearchMatch> > entry in results) { List <SearchMatch> matches = entry.Value; if (matches.Count == 0) { continue; } string path = entry.Key; UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.Updating") + " \"" + path + "\""); ScintillaControl sci = AssociatedDocumentHelper.LoadDocument(path); if (isNotHaxe && path != currentTarget.NewFilePath && ASContext.Context.CurrentModel.Imports.Search(targetName, FlagType.Class & FlagType.Function & FlagType.Namespace, 0) == null) { ASGenerator.InsertImport(new MemberModel(targetName, newType, FlagType.Import, 0), false); } if (packageIsNotEmpty) { RefactoringHelper.ReplaceMatches(matches, sci, newType, null); } else { foreach (SearchMatch sm in matches) { if (sm.LineText.TrimStart().StartsWith("import")) { RefactoringHelper.SelectMatch(sci, sm); sci.ReplaceSel(newType); } } } foreach (SearchMatch match in matches) { match.LineText = sci.GetLine(match.Line - 1); match.Value = newType; } if (!Results.ContainsKey(path)) { Results[path] = new List <SearchMatch>(); } Results[path].AddRange(matches.ToArray()); PluginBase.MainForm.CurrentDocument.Save(); if (sci.IsModify) { AssociatedDocumentHelper.MarkDocumentToKeep(path); } } UserInterfaceManager.ProgressDialog.Hide(); MessageBar.Locked = false; UpdateReferencesNextTarget(); }
/// <summary> /// Renames the given the set of matched references /// </summary> private void OnFindAllReferencesCompleted(object sender, RefactorCompleteEventArgs<IDictionary<string, List<SearchMatch>>> eventArgs) { UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.UpdatingReferences")); MessageBar.Locked = true; var isParameterVar = (Target.Member?.Flags & FlagType.ParameterVar) > 0; foreach (var entry in eventArgs.Results) { UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.Updating") + " \"" + entry.Key + "\""); // re-open the document and replace all the text var doc = AssociatedDocumentHelper.LoadDocument(entry.Key); var sci = doc.SciControl; var targetMatches = entry.Value; if (isParameterVar) { var lineFrom = Target.Context.ContextFunction.LineFrom; var lineTo = Target.Context.ContextFunction.LineTo; var search = new FRSearch(NewName) {WholeWord = true, NoCase = false, SingleLine = true}; var matches = search.Matches(sci.Text, sci.PositionFromLine(lineFrom), lineFrom); matches.RemoveAll(it => it.Line < lineFrom || it.Line > lineTo); if (matches.Count != 0) { sci.BeginUndoAction(); try { for (var i = 0; i < matches.Count; i++) { var match = matches[i]; var expr = ASComplete.GetExpressionType(sci, sci.MBSafePosition(match.Index) + sci.MBSafeTextLength(match.Value)); if (expr.IsNull() || expr.Context.Value != NewName) continue; string replacement; var flags = expr.Member.Flags; if ((flags & FlagType.Static) > 0) replacement = ASContext.Context.CurrentClass.Name + "." + NewName; else if((flags & FlagType.LocalVar) == 0) replacement = "this." + NewName; else continue; RefactoringHelper.SelectMatch(sci, match); sci.EnsureVisible(sci.LineFromPosition(sci.MBSafePosition(match.Index))); sci.ReplaceSel(replacement); for (var j = 0; j < targetMatches.Count; j++) { var targetMatch = targetMatches[j]; if (targetMatch.Line <= match.Line) continue; FRSearch.PadIndexes(targetMatches, j, match.Value, replacement); if (targetMatch.Line == match.Line + 1) { targetMatch.LineText = sci.GetLine(match.Line); targetMatch.Column += replacement.Length - match.Value.Length; } break; } FRSearch.PadIndexes(matches, i + 1, match.Value, replacement); } } finally { sci.EndUndoAction(); } } } // replace matches in the current file with the new name RefactoringHelper.ReplaceMatches(targetMatches, sci, NewName); //Uncomment if we want to keep modified files //if (sci.IsModify) AssociatedDocumentHelper.MarkDocumentToKeep(entry.Key); doc.Save(); } if (newFileName != null) RenameFile(eventArgs.Results); Results = eventArgs.Results; AssociatedDocumentHelper.CloseTemporarilyOpenedDocuments(); if (OutputResults) ReportResults(); UserInterfaceManager.ProgressDialog.Hide(); MessageBar.Locked = false; FireOnRefactorComplete(); }
private void FindFinished(FRResults results) { UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.UpdatingReferences")); MessageBar.Locked = true; var currentTarget = targets[currentTargetIndex]; string targetName = Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); string oldType = (currentTarget.OldFileModel.Package + "." + targetName).Trim('.'); string newType = (currentTarget.NewPackage + "." + targetName).Trim('.'); foreach (KeyValuePair <string, List <SearchMatch> > entry in results) { List <SearchMatch> matches = entry.Value; if (matches.Count == 0 || entry.Key == currentTarget.OldFilePath || entry.Key == currentTarget.NewFilePath) { continue; } string file = entry.Key; UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.Updating") + " \"" + file + "\""); ITabbedDocument doc; ScintillaControl sci; var actualMatches = new List <SearchMatch>(); foreach (SearchMatch match in entry.Value) { // we have to open/reopen the entry's file // there are issues with evaluating the declaration targets with non-open, non-current files // we have to do it each time as the process of checking the declaration source can change the currently open file! sci = AssociatedDocumentHelper.LoadDocument(file).SciControl; // if the search result does point to the member source, store it if (RefactoringHelper.DoesMatchPointToTarget(sci, match, currentTargetResult, this.AssociatedDocumentHelper)) { actualMatches.Add(match); } } if (actualMatches.Count == 0) { continue; } int currLine = -1; doc = AssociatedDocumentHelper.LoadDocument(file); sci = doc.SciControl; string directory = Path.GetDirectoryName(file); // Let's check if we need to add the import. Check the considerations at the start of the file // directory != currentTarget.OwnerPath -> renamed owner directory, so both files in the same place bool needsImport = directory != Path.GetDirectoryName(currentTarget.NewFilePath) && directory != currentTarget.OwnerPath && ASContext.Context.CurrentModel.Imports.Search(targetName, FlagType.Class & FlagType.Function & FlagType.Namespace, 0) == null; // Replace matches int typeDiff = sci.MBSafeTextLength(oldType) - sci.MBSafeTextLength(targetName); int newTypeDiff = sci.MBSafeTextLength(newType) - sci.MBSafeTextLength(oldType); int accumulatedDiff = 0; int j = 0; for (int i = 0, count = actualMatches.Count; i < count; i++) { var sm = actualMatches[j]; if (currLine == -1) { currLine = sm.Line - 1; } if (sm.LineText.Contains(oldType)) { sm.Index -= typeDiff - accumulatedDiff; sm.Value = oldType; RefactoringHelper.SelectMatch(sci, sm); sm.Column -= typeDiff; sci.ReplaceSel(newType); sm.LineEnd = sci.SelectionEnd; sm.LineText = sm.LineText.Replace(oldType, newType); sm.Length = oldType.Length; sm.Value = newType; if (needsImport) { sm.Line++; } accumulatedDiff += newTypeDiff; j++; } else { actualMatches.RemoveAt(j); } } if (needsImport) { sci.GotoLine(currLine); ASGenerator.InsertImport(new MemberModel(targetName, newType, FlagType.Import, 0), false); int newLine = sci.LineFromPosition(sci.Text.IndexOfOrdinal(newType)); var sm = new SearchMatch(); sm.Line = newLine + 1; sm.LineText = sci.GetLine(newLine); sm.Column = 0; sm.Length = sci.MBSafeTextLength(sm.LineText); sm.Value = sm.LineText; actualMatches.Insert(0, sm); } if (actualMatches.Count == 0) { continue; } if (!Results.ContainsKey(file)) { Results[file] = new List <SearchMatch>(); } Results[file].AddRange(actualMatches); //Do we want to open modified files? //if (sci.IsModify) AssociatedDocumentHelper.MarkDocumentToKeep(file); doc.Save(); } currentTargetIndex++; UserInterfaceManager.ProgressDialog.Hide(); MessageBar.Locked = false; UpdateReferencesNextTarget(); }