Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        private MoveTargetHelper GetMoveTarget(string oldFilePath, string newPath)
        {
            MoveTargetHelper result = new MoveTargetHelper();

            result.OldFilePath  = oldFilePath;
            result.OldFileModel = ASContext.Context.GetFileModel(oldFilePath);
            result.NewFilePath  = newPath;
            IProject project    = PluginBase.CurrentProject;
            string   newPackage = project.GetAbsolutePath(Path.GetDirectoryName(newPath));

            if (!string.IsNullOrEmpty(newPackage))
            {
                foreach (PathModel pathModel in ASContext.Context.Classpath)
                {
                    string path = project.GetAbsolutePath(pathModel.Path);
                    if (path == newPackage)
                    {
                        newPackage = "";
                        break;
                    }
                    else if (newPackage.StartsWith(path))
                    {
                        newPackage = newPackage.Substring((path + "\\").Length).Replace("\\", ".");
                        break;
                    }
                }
            }
            result.NewPackage = newPackage;
            return(result);
        }
Esempio n. 2
0
        private MoveTargetHelper GetMoveTarget(string oldFilePath, string newPath, string ownerPath)
        {
            MoveTargetHelper result = new MoveTargetHelper();

            result.OldFilePath  = oldFilePath;
            result.OldFileModel = ASContext.Context.GetFileModel(oldFilePath);
            result.NewFilePath  = newPath;
            result.OwnerPath    = ownerPath;
            IProject project    = PluginBase.CurrentProject;
            string   newPackage = result.NewPackage = project.GetAbsolutePath(Path.GetDirectoryName(newPath));

            if (!string.IsNullOrEmpty(newPackage))
            {
                var basePaths   = project.SourcePaths.Length == 0 ? new[] { Path.GetDirectoryName(project.ProjectPath) } : project.SourcePaths;
                var lookupPaths = basePaths.
                                  Concat(ProjectManager.PluginMain.Settings.GetGlobalClasspaths(project.Language)).
                                  Select(project.GetAbsolutePath).Distinct();

                foreach (string path in lookupPaths)
                {
                    if (path == newPackage)
                    {
                        newPackage = "";
                        break;
                    }
                    if (newPackage.StartsWithOrdinal(path))
                    {
                        newPackage = newPackage.Substring((path + "\\").Length).Replace("\\", ".");
                        break;
                    }
                }
            }

            if (result.NewPackage == newPackage)
            {
                targetsOutsideClasspath = true;
                return(null);
            }
            if (newPackage == result.OldFileModel.FullPackage && Path.GetFileName(result.OldFileModel.FileName) == Path.GetFileName(newPath))
            {
                // moving file to another classpath at the same level, no need to refactor
                return(null);
            }
            result.NewPackage = newPackage;
            return(result);
        }
Esempio n. 3
0
        private MoveTargetHelper GetMoveTarget(string oldFilePath, string newPath, string ownerPath)
        {
            MoveTargetHelper result = new MoveTargetHelper();

            result.OldFilePath  = oldFilePath;
            result.OldFileModel = ASContext.Context.GetFileModel(oldFilePath);
            result.NewFilePath  = newPath;
            result.OwnerPath    = ownerPath;
            IProject project    = PluginBase.CurrentProject;
            string   newPackage = result.NewPackage = project.GetAbsolutePath(Path.GetDirectoryName(newPath));

            if (!string.IsNullOrEmpty(newPackage))
            {
                // NOTE: Could be simplified in .NET 3.5 with LINQ .Concat.Select.Distinct
                var visited = new Dictionary <string, byte>();
                foreach (string sourcePath in project.SourcePaths)
                {
                    string path = project.GetAbsolutePath(sourcePath);
                    if (visited.ContainsKey(path))
                    {
                        continue;
                    }
                    visited[path] = 1;
                    if (path == newPackage)
                    {
                        newPackage = "";
                        break;
                    }
                    else if (newPackage.StartsWith(path))
                    {
                        newPackage = newPackage.Substring((path + "\\").Length).Replace("\\", ".");
                        break;
                    }
                }
                if (result.NewPackage == newPackage)
                {
                    foreach (string globalPath in ProjectManager.PluginMain.Settings.GetGlobalClasspaths(project.Language))
                    {
                        string path = project.GetAbsolutePath(globalPath);
                        if (visited.ContainsKey(path))
                        {
                            continue;
                        }
                        visited[path] = 1;
                        if (path == newPackage)
                        {
                            newPackage = "";
                            break;
                        }
                        else if (newPackage.StartsWith(path))
                        {
                            newPackage = newPackage.Substring((path + "\\").Length).Replace("\\", ".");
                            break;
                        }
                    }
                }
            }

            if (result.NewPackage == newPackage)
            {
                targetsOutsideClasspath = true;
                return(null);
            }
            if (newPackage == result.OldFileModel.FullPackage && Path.GetFileName(result.OldFileModel.FileName) == Path.GetFileName(newPath))
            {
                // moving file to another classpath at the same level, no need to refactor
                return(null);
            }
            result.NewPackage = newPackage;
            return(result);
        }
Esempio n. 4
0
 /// <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();
     }
 }