Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        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();
        }