Esempio n. 1
0
 /// <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();
 }
Esempio n. 2
0
        /// <summary>
        /// Entry point to execute renaming.
        /// </summary>
        protected override void ExecutionImplementation()
        {
            if (isRenamePackage)
            {
                renamePackage.RegisterDocumentHelper(AssociatedDocumentHelper);
                renamePackage.OnRefactorComplete += OnRenamePackageComplete;
                renamePackage.Execute();
            }
            else
            {
                // To get the initial open documents, finding all references will interfere if we try later
                // We may already have an AssociatedDocumentHelper
                RegisterDocumentHelper(AssociatedDocumentHelper);

                // Targets have to be validated before getting and modifying all references, otherwise we may end with some bad state
                if (ValidateTargets())
                {
                    findAllReferencesCommand.Execute();
                }
                else
                {
                    AssociatedDocumentHelper.CloseTemporarilyOpenedDocuments();
                    FireOnRefactorComplete();
                }
            }
        }
Esempio n. 3
0
 /// <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();
 }
Esempio n. 4
0
        private void MoveRefactoredFiles()
        {
            MessageBar.Locked = true;
            AssociatedDocumentHelper.CloseTemporarilyOpenedDocuments();
            foreach (var target in targets)
            {
                File.Delete(target.OldFilePath);

                if (target.OwnerPath == null)
                {
                    OldPathToNewPath.Remove(target.OldFilePath);
                }

                // Casing changes, we cannot move directly here, there may be conflicts, better leave it to the next step
                if (target.TmpFilePath != null)
                {
                    RefactoringHelper.Move(target.TmpFilePath, target.NewFilePath, true, target.OldFilePath);
                }
                else
                {
                    RefactoringHelper.RaiseMoveEvent(target.OldFilePath, target.NewFilePath);
                }
            }
            // Move non-source files and whole folders
            foreach (KeyValuePair <string, string> item in OldPathToNewPath)
            {
                string oldPath = item.Key;
                string newPath = item.Value;
                if (File.Exists(oldPath))
                {
                    newPath = Path.Combine(newPath, Path.GetFileName(oldPath));
                    if (!Path.IsPathRooted(newPath))
                    {
                        newPath = Path.Combine(Path.GetDirectoryName(oldPath), newPath);
                    }
                    RefactoringHelper.Move(oldPath, newPath, true);
                }
                else if (Directory.Exists(oldPath))
                {
                    newPath = renaming ? Path.Combine(Path.GetDirectoryName(oldPath), newPath) : Path.Combine(newPath, Path.GetFileName(oldPath));

                    // Look for document class changes
                    // Do not use RefactoringHelper to avoid possible dialogs that we don't want
                    Project project          = (Project)PluginBase.CurrentProject;
                    string  newDocumentClass = null;
                    string  searchPattern    = project.DefaultSearchFilter;
                    foreach (string pattern in searchPattern.Split(';'))
                    {
                        foreach (string file in Directory.GetFiles(oldPath, pattern, SearchOption.AllDirectories))
                        {
                            if (project.IsDocumentClass(file))
                            {
                                newDocumentClass = file.Replace(oldPath, newPath);
                                break;
                            }
                        }
                        if (newDocumentClass != null)
                        {
                            break;
                        }
                    }

                    // Check if this is a name casing change
                    if (oldPath.Equals(newPath, StringComparison.OrdinalIgnoreCase))
                    {
                        string tmpPath = oldPath + "$renaming$";
                        FileHelper.ForceMoveDirectory(oldPath, tmpPath);
                        DocumentManager.MoveDocuments(oldPath, tmpPath);
                        oldPath = tmpPath;
                    }

                    // Move directory contents to final location
                    FileHelper.ForceMoveDirectory(oldPath, newPath);
                    DocumentManager.MoveDocuments(oldPath, newPath);

                    if (!string.IsNullOrEmpty(newDocumentClass))
                    {
                        project.SetDocumentClass(newDocumentClass, true);
                        project.Save();
                    }
                    RefactoringHelper.RaiseMoveEvent(oldPath, newPath);
                }
            }

            MessageBar.Locked = false;
        }