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; 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; // replace matches in the current file with the new name RefactoringHelper.ReplaceMatches(entry.Value, 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(); }
/// <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(); }
void OnHaxeCompleteResultHandler(HaxeComplete hc, List <HaxePositionResult> result, HaxeCompleteStatus status) { var results = new FRResults(); switch (status) { case HaxeCompleteStatus.ERROR: TraceManager.AddAsync(hc.Errors, -3); break; case HaxeCompleteStatus.USAGE: if (!IgnoreDeclarationSource) { var sci = ASContext.CurSciControl; var path = sci.FileName; if (!results.ContainsKey(path)) { results.Add(path, new List <SearchMatch>()); } var index = hc.Expr.PositionExpression; var line = sci.LineFromPosition(index); var lineStart = sci.PositionFromLine(line); var lineEnd = sci.LineEndPosition(line); var lineText = sci.GetLine(line); var value = hc.Expr.Value; results[path].Add(new SearchMatch { Column = index - lineStart, Index = index, Length = value.Length, Line = line + 1, LineStart = lineStart, LineEnd = lineEnd, LineText = lineText, Value = value }); } foreach (var it in result) { var path = it.Path; if (!results.ContainsKey(path)) { results.Add(path, new List <SearchMatch>()); } var matches = results[path]; var sci = AssociatedDocumentHelper.LoadDocument(path).SciControl; var line = it.LineStart - 1; var lineStart = sci.PositionFromLine(line); var lineEnd = sci.LineEndPosition(line); var lineText = sci.GetLine(line); var value = lineText.Substring(it.CharacterStart, it.CharacterEnd - it.CharacterStart); matches.Add(new SearchMatch { Column = it.CharacterStart, Index = lineStart + it.CharacterStart, Length = value.Length, Line = line + 1, LineStart = lineStart, LineEnd = lineEnd, LineText = lineText, Value = value }); } break; } FindFinished(results); }
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(); }
private void UpdateReferencesNextTarget() { if (currentTargetIndex < targets.Count) { var currentTarget = targets[currentTargetIndex]; FileModel oldFileModel = currentTarget.OldFileModel; FRSearch search; string oldType; if (string.IsNullOrEmpty(oldFileModel.Package)) { search = new FRSearch("package"); search.WholeWord = true; oldType = Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } else { search = new FRSearch("package\\s+(" + oldFileModel.Package + ")"); oldType = oldFileModel.Package + "." + Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } search.IsRegex = true; search.Filter = SearchFilter.None; oldType = oldType.Trim('.'); MessageBar.Locked = true; string newFilePath = currentTarget.NewFilePath; var doc = AssociatedDocumentHelper.LoadDocument(currentTarget.TmpFilePath ?? newFilePath); ScintillaControl sci = doc.SciControl; search.SourceFile = sci.FileName; List <SearchMatch> matches = search.Matches(sci.Text); string packageReplacement = "package"; if (currentTarget.NewPackage != "") { packageReplacement += " " + currentTarget.NewPackage; } RefactoringHelper.ReplaceMatches(matches, sci, packageReplacement); int offset = "package ".Length; foreach (SearchMatch match in matches) { match.Column += offset; match.LineText = sci.GetLine(match.Line - 1); match.Value = currentTarget.NewPackage; } if (matches.Count > 0) { if (!Results.ContainsKey(newFilePath)) { Results[newFilePath] = new List <SearchMatch>(); } Results[newFilePath].AddRange(matches); } else if (sci.ConfigurationLanguage == "haxe") { // haxe modules don't need to specify a package if it's empty sci.InsertText(0, packageReplacement + ";\n\n"); } //Do we want to open modified files? //if (sci.IsModify) AssociatedDocumentHelper.MarkDocumentToKeep(file); doc.Save(); MessageBar.Locked = false; UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.FindingReferences")); UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.SearchingFiles")); currentTargetResult = RefactoringHelper.GetRefactorTargetFromFile(oldFileModel.FileName, AssociatedDocumentHelper); if (currentTargetResult != null) { RefactoringHelper.FindTargetInFiles(currentTargetResult, UserInterfaceManager.ProgressDialog.UpdateProgress, FindFinished, true, true, true); } else { currentTargetIndex++; UserInterfaceManager.ProgressDialog.Hide(); UpdateReferencesNextTarget(); } } else { MoveRefactoredFiles(); ReopenInitialFiles(); FireOnRefactorComplete(); } }
/// <summary> /// /// </summary> private void UpdateReferencesNextTarget() { if (targets.Count > 0) { currentTarget = targets[0]; targets.Remove(currentTarget); FileModel oldFileModel = currentTarget.OldFileModel; FRSearch search; string newType; if (string.IsNullOrEmpty(oldFileModel.Package)) { search = new FRSearch("(package)+\\s*"); newType = Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } else { search = new FRSearch("(package)+\\s+(" + oldFileModel.Package + ")\\s*"); newType = oldFileModel.Package + "." + Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } search.IsRegex = true; search.Filter = SearchFilter.None; newType = newType.Trim('.'); MessageBar.Locked = true; string newFilePath = currentTarget.NewFilePath; ScintillaControl sci = AssociatedDocumentHelper.LoadDocument(newFilePath); List <SearchMatch> matches = search.Matches(sci.Text); RefactoringHelper.ReplaceMatches(matches, sci, "package " + currentTarget.NewPackage + " ", null); int offset = "package ".Length; foreach (SearchMatch match in matches) { match.Column += offset; match.LineText = sci.GetLine(match.Line - 1); match.Value = currentTarget.NewPackage; } if (!Results.ContainsKey(newFilePath)) { Results[newFilePath] = new List <SearchMatch>(); } Results[newFilePath].AddRange(matches.ToArray()); PluginBase.MainForm.CurrentDocument.Save(); if (sci.IsModify) { AssociatedDocumentHelper.MarkDocumentToKeep(currentTarget.OldFilePath); } MessageBar.Locked = false; UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.FindingReferences")); UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.SearchingFiles")); ASResult target = new ASResult() { Member = new MemberModel(newType, newType, FlagType.Import, 0) }; RefactoringHelper.FindTargetInFiles(target, UserInterfaceManager.ProgressDialog.UpdateProgress, FindFinished, true); } else { if (outputResults) { ReportResults(); } FireOnRefactorComplete(); } }
private void RenameFile(IDictionary <string, List <SearchMatch> > results) { ASResult target = findAllReferencesCommand.CurrentTarget; Boolean isEnum = target.Type.IsEnum(); Boolean isClass = false; Boolean isConstructor = false; if (!isEnum) { Boolean isVoid = target.Type.IsVoid(); isClass = !isVoid && target.IsStatic && target.Member == null; isConstructor = !isVoid && !isClass && RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Constructor); } Boolean isGlobalFunction = false; Boolean isGlobalNamespace = false; if (!isEnum && !isClass && !isConstructor && (target.InClass == null || target.InClass.IsVoid())) { isGlobalFunction = RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Function); isGlobalNamespace = RefactoringHelper.CheckFlag(target.Member.Flags, FlagType.Namespace); } if (!isEnum && !isClass && !isConstructor && !isGlobalFunction && !isGlobalNamespace) { return; } FileModel inFile = null; String originName = null; if (isEnum || isClass) { inFile = target.Type.InFile; originName = target.Type.Name; } else { inFile = target.Member.InFile; originName = target.Member.Name; } if (inFile == null) { return; } String oldFileName = inFile.FileName; String oldName = Path.GetFileNameWithoutExtension(oldFileName); if (!string.IsNullOrEmpty(oldName) && !oldName.Equals(originName)) { return; } String fullPath = Path.GetFullPath(inFile.FileName); fullPath = Path.GetDirectoryName(fullPath); String newFileName = Path.Combine(fullPath, NewName + Path.GetExtension(oldFileName)); if (string.IsNullOrEmpty(oldFileName) || oldFileName.Equals(newFileName)) { return; } foreach (ITabbedDocument doc in PluginBase.MainForm.Documents) { if (doc.FileName.Equals(oldFileName)) { doc.Save(); doc.Close(); break; } } RefactoringHelper.Move(oldFileName, newFileName); AssociatedDocumentHelper.LoadDocument(newFileName); if (results.ContainsKey(oldFileName)) { results[newFileName] = results[oldFileName]; results.Remove(oldFileName); } }
protected override void ExecutionImplementation() { String oldFileName = Path.GetFileNameWithoutExtension(oldPath); String newFileName = Path.GetFileNameWithoutExtension(newPath); String msg = TextHelper.GetString("Info.RenamingFile"); String title = String.Format(TextHelper.GetString("Title.RenameDialog"), oldFileName); if (MessageBox.Show(msg, title, MessageBoxButtons.YesNo) == DialogResult.Yes) { Int32 line = 0; ScintillaControl sci = AssociatedDocumentHelper.LoadDocument(oldPath); if (sci == null) { return; // Should not happen... } List <ClassModel> classes = ASContext.Context.CurrentModel.Classes; if (classes.Count > 0) { foreach (ClassModel classModel in classes) { if (classModel.Name.Equals(oldFileName)) { line = classModel.LineFrom; break; } } } else { foreach (MemberModel member in ASContext.Context.CurrentModel.Members) { if (member.Name.Equals(oldFileName)) { line = member.LineFrom; break; } } } if (line > 0) { sci.SelectText(oldFileName, sci.PositionFromLine(line)); Rename command = new Rename(RefactoringHelper.GetDefaultRefactorTarget(), true, newFileName); command.Execute(); return; } } // refactor failed or was refused if (Path.GetFileName(oldPath).Equals(newPath, StringComparison.OrdinalIgnoreCase)) { // name casing changed string tmpPath = oldPath + "$renaming$"; File.Move(oldPath, tmpPath); oldPath = tmpPath; } if (!Path.IsPathRooted(newPath)) { newPath = Path.Combine(Path.GetDirectoryName(oldPath), newPath); } File.Move(oldPath, newPath); PluginCore.Managers.DocumentManager.MoveDocuments(oldPath, newPath); }