Exemplo n.º 1
0
        private static void GetReferencesInModule(DModule ast, StringBuilder refs, DNode n, ResolutionContext ctxt)
        {
            var res = ReferencesFinder.SearchModuleForASTNodeReferences(ast, n, ctxt);

            int cnt = res.Count();

            foreach (var r in res)
            {
                var rfilename = ast.FileName;
                var rloc      = r.Location;
                var ln        = String.Format("{0},{1},{2},{3}:{4}\n", rloc.Line, rloc.Column - 1, rloc.Line, rloc.Column, rfilename);
                refs.Append(ln);
            }
        }
Exemplo n.º 2
0
        public override Task <LocationContainer> Handle(ReferenceParams request, CancellationToken cancellationToken)
        {
            var workDone = ProgressManager.WorkDone(request, new WorkDoneProgressBegin
            {
                Title      = "Begin resolving references",
                Percentage = 0
            });

            var editorData       = DResolverWrapper.CreateEditorData(request, cancellationToken);
            var nodeSymbolToFind = DResolverWrapper
                                   .ResolveHoveredCodeLoosely(editorData, out LooseResolution.NodeResolutionAttempt _, out ISyntaxRegion _)
                                   .Select(ExpressionTypeEvaluation.GetResultMember)
                                   .FirstOrDefault();

            if (nodeSymbolToFind == null)
            {
                workDone.OnNext(new WorkDoneProgressReport()
                {
                    Message    = "No symbol found",
                    Percentage = 100
                });
                workDone.OnCompleted();
                return(Task.FromResult(new LocationContainer()));
            }

            var modules = editorData.ParseCache.EnumRootPackagesSurroundingModule(editorData.SyntaxTree)
                          .SelectMany(package => (IEnumerable <DModule>)package)
                          .Where(module => module != null)
                          .OrderBy(module => module.FileName)
                          .ToList();

            var progress           = ProgressManager.For(request, cancellationToken);
            var allFoundReferences = new List <Location>();
            var ctxt          = ResolutionContext.Create(editorData, true);
            int progressIndex = 0;

            foreach (var module in modules)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                try
                {
                    var references = ReferencesFinder.SearchModuleForASTNodeReferences(module, nodeSymbolToFind, ctxt, request.Context.IncludeDeclaration)
                                     .Where(region => region != null)
                                     .OrderBy(region => region.Location)
                                     .Select(region => ToLocation(region, module))
                                     .Where(location => location != default)
                                     .ToList();
                    progressIndex++;
                    if (references.Count > 0)
                    {
                        workDone.OnNext(new WorkDoneProgressReport
                        {
                            Message    = "Scanned " + module.FileName,
                            Percentage = ((double)progressIndex / modules.Count) * 100.0
                        });
                        allFoundReferences.AddRange(references);
                        progress?.OnNext(new Container <Location>(allFoundReferences));
                    }
                }
                catch (Exception ex)
                {
                    workDone.OnError(ex);
                    if (progress != null)
                    {
                        progress.OnError(ex);
                        return(Task.FromResult(new LocationContainer()));
                    }
                    return(Task.FromException <LocationContainer>(ex));
                }
            }

            workDone.OnCompleted();
            progress?.OnCompleted();

            // if $progress sent, result must be empty
            return(Task.FromResult(progress != null ? new LocationContainer()
                : new LocationContainer(allFoundReferences)));
        }
Exemplo n.º 3
0
        public override Task <DocumentHighlightContainer> Handle(DocumentHighlightParams request,
                                                                 CancellationToken cancellationToken)
        {
            var workDone = ProgressManager.WorkDone(request, new WorkDoneProgressBegin
            {
                Title      = "Begin finding highlights",
                Percentage = 0
            });

            var editorData       = DResolverWrapper.CreateEditorData(request, cancellationToken);
            var nodeSymbolToFind = DResolverWrapper
                                   .ResolveHoveredCodeLoosely(editorData, out LooseResolution.NodeResolutionAttempt _,
                                                              out ISyntaxRegion _)
                                   .Select(GetResultMember)
                                   .FirstOrDefault();

            if (nodeSymbolToFind == null)
            {
                workDone.OnNext(new WorkDoneProgressReport()
                {
                    Message    = "No symbol found",
                    Percentage = 100
                });
                workDone.OnCompleted();
                return(Task.FromResult(new DocumentHighlightContainer()));
            }

            var progress = ProgressManager.For(request, cancellationToken);
            List <DocumentHighlight> allFoundReferences;
            var ctxt = ResolutionContext.Create(editorData, true);

            try
            {
                allFoundReferences = cancellationToken.IsCancellationRequested
                    ? new List <DocumentHighlight>()
                    : ReferencesFinder
                                     .SearchModuleForASTNodeReferences(editorData.SyntaxTree, nodeSymbolToFind, ctxt)
                                     .Where(region => region != null)
                                     .OrderBy(region => region.Location)
                                     .Select(ToDocumentHighlight)
                                     .ToList();
            }
            catch (Exception ex)
            {
                workDone.OnError(ex);
                if (progress != null)
                {
                    progress.OnError(ex);
                    return(Task.FromResult(new DocumentHighlightContainer()));
                }

                return(Task.FromException <DocumentHighlightContainer>(ex));
            }

            if (allFoundReferences.Count > 0)
            {
                progress?.OnNext(new DocumentHighlightContainer(allFoundReferences));
            }

            workDone.OnCompleted();
            progress?.OnCompleted();

            return(Task.FromResult(progress != null
                ? new DocumentHighlightContainer()
                : new DocumentHighlightContainer(allFoundReferences)));
        }
Exemplo n.º 4
0
        static IEnumerable <SearchResult> FindReferences(
            AbstractDProject project,
            INode member,
            ISearchProgressMonitor monitor = null)
        {
            var searchResults = new List <SearchResult>();

            var modules = new List <DModule>();

            if (project != null)
            {
                foreach (var p in project.GetSourcePaths(IdeApp.Workspace.ActiveConfiguration))
                {
                    modules.AddRange(GlobalParseCache.EnumModulesRecursively(p, null));
                }
            }
            else
            {
                modules.Add(IdeApp.Workbench.ActiveDocument.GetDAst());
            }

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

            List <ISyntaxRegion> references = null;
            var ctxt = ResolutionContext.Create(DResolverWrapper.CreateParseCacheView(project), null);

            foreach (var mod in modules)
            {
                if (mod == null)
                {
                    continue;
                }
                try
                {
                    references = ReferencesFinder.SearchModuleForASTNodeReferences(mod, member, ctxt).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 = 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 = reference.Location;
                        }
                        else if (reference is TemplateParameter)
                        {
                            loc = (reference as TemplateParameter).NameLocation;
                        }
                        else if (reference is INode)
                        {
                            loc = (reference as INode).NameLocation;
                        }
                        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);
        }