public override List <Change> PerformChanges(RefactoringOptions options, object prop)
        {
            #region Init
            var renameProperties = prop as RenameProperties;
            if (renameProperties == null)
            {
                return(null);
            }

            var changes = new List <Change>();

            var doc = options.Document;
            if (doc == null)
            {
                return(null);
            }

            var ddoc = doc.ParsedDocument as ParsedDModule;
            if (ddoc == null)
            {
                return(null);
            }

            var n = options.SelectedItem as INode;
            if (n == null)
            {
                return(null);
            }

            var project = doc.HasProject ? doc.Project as DProject : null;

            var parseCache = DResolverWrapper.CreateCacheList(project);

            var modules = new List <DModule>();
            if (project == null)
            {
                modules.Add((Ide.IdeApp.Workbench.ActiveDocument.ParsedDocument as ParsedDModule).DDom);
            }
            else
            {
                foreach (var p in project.GetSourcePaths())
                {
                    modules.AddRange(GlobalParseCache.EnumModules(p));
                }
            }

            var ctxt = ResolutionContext.Create(parseCache, null, null);
            #endregion

            // Enumerate references
            foreach (var mod in modules)
            {
                if (mod == null)
                {
                    continue;
                }

                var references = D_Parser.Refactoring.ReferencesFinder.Scan(mod, n, ctxt).ToList();

                if (((DModule)n.NodeRoot).FileName == mod.FileName)
                {
                    references.Insert(0, new IdentifierDeclaration(n.NameHash)
                    {
                        Location = n.NameLocation
                    });
                }

                if (references.Count < 1)
                {
                    continue;
                }

                var txt             = TextFileProvider.Instance.GetEditableTextFile(new FilePath(mod.FileName));
                var prevReplacement = CodeLocation.Empty;
                foreach (ISyntaxRegion reference in references)
                {
                    if (prevReplacement == reference.Location)
                    {
                        continue;
                    }

                    prevReplacement = reference.Location;
                    changes.Add(new TextReplaceChange {
                        FileName     = mod.FileName,
                        InsertedText = renameProperties.NewName,
                        RemovedChars = n.Name.Length,
                        Description  = string.Format(GettextCatalog.GetString("Replace '{0}' with '{1}'"), n.Name, renameProperties.NewName),
                        Offset       = txt.GetPositionFromLineColumn(reference.Location.Line, reference.Location.Column)
                    });
                }
            }

            return(changes);
        }
Exemple #2
0
        static IEnumerable <SearchResult> FindReferences(
            AbstractDProject project,
            INode member,
            ISearchProgressMonitor monitor = null)
        {
            var searchResults = new List <SearchResult>();

            var parseCache = project != null ?
                             project.ParseCache : DCompilerService.Instance.GetDefaultCompiler().GenParseCacheView();

            var modules = new List <DModule>();

            if (project != null)
            {
                foreach (var p in project.GetSourcePaths(IdeApp.Workspace.ActiveConfiguration))
                {
                    modules.AddRange(GlobalParseCache.EnumModules(p, null));
                }
            }
            else
            {
                modules.Add((Ide.IdeApp.Workbench.ActiveDocument.ParsedDocument as MonoDevelop.D.Parser.ParsedDModule).DDom);
            }

            if (monitor != null)
            {
                monitor.BeginStepTask("Scan for references", modules.Count(), 1);
            }

            List <ISyntaxRegion> references = null;

            foreach (var mod in modules)
            {
                if (mod == null)
                {
                    continue;
                }
                try
                {
                    references = ReferencesFinder.Scan(mod, member, ResolutionContext.Create(parseCache, null, null)).ToList();

                    if (references.Count < 1)
                    {
                        if (monitor != null)
                        {
                            monitor.Step(1);
                        }
                        continue;
                    }

                    // Sort the references by code location
                    references.Sort(new IdLocationComparer());

                    // Get actual document code
                    var targetDoc = Ide.TextFileProvider.Instance.GetTextEditorData(new FilePath(mod.FileName));

                    foreach (var reference in references)
                    {
                        CodeLocation loc;

                        if (reference is AbstractTypeDeclaration)
                        {
                            loc = ((AbstractTypeDeclaration)reference).NonInnerTypeDependendLocation;
                        }
                        else if (reference is IExpression)
                        {
                            loc = ((IExpression)reference).Location;
                        }
                        else
                        {
                            continue;
                        }

                        searchResults.Add(new SearchResult(new FileProvider(mod.FileName, project),
                                                           targetDoc.LocationToOffset(loc.Line,
                                                                                      loc.Column),
                                                           member.Name.Length));
                    }
                }
                catch (Exception ex) { LoggingService.LogWarning("Error during reference search", ex); }

                if (monitor != null)
                {
                    monitor.Step(1);
                }
            }

            if (monitor != null)
            {
                monitor.EndTask();
            }

            return(searchResults);
        }