Пример #1
0
            public override void Rename(ISymbol symbol, string name)
            {
                if (symbol.SymbolKind == SymbolKind.Variable || symbol.SymbolKind == SymbolKind.Parameter)
                {
                    Rename(symbol as IVariable, name);
                    return;
                }

                FindReferences refFinder = new FindReferences();

                foreach (var fileContext in context.projectContexts)
                {
                    using (var newScript = (TestScript)fileContext.StartScript()) {
                        refFinder.FindReferencesInFile(refFinder.GetSearchScopes(symbol),
                                                       fileContext.UnresolvedFile,
                                                       fileContext.RootNode as SyntaxTree,
                                                       fileContext.Compilation,
                                                       (n, r) => newScript.Rename(n, name),
                                                       context.CancellationToken);
                    }
                }
            }
        private static List <AstNode> ComputeMatchNodes(RefactoringContext context, VariableInitializer firstOrNullObject)
        {
            var referenceFinder = new FindReferences();
            var matchedNodes    = new List <AstNode>();

            var resolveResult = context.Resolver.Resolve(firstOrNullObject);
            var member        = resolveResult as MemberResolveResult;

            if (member == null)//not a member is unexpected case, so is better to return no match than to break the code
            {
                return(matchedNodes);
            }

            FoundReferenceCallback callback = (matchNode, result) => matchedNodes.Add(matchNode);

            var searchScopes = referenceFinder.GetSearchScopes(member.Member);

            referenceFinder.FindReferencesInFile(searchScopes,
                                                 context.UnresolvedFile,
                                                 context.RootNode as SyntaxTree,
                                                 context.Compilation, callback,
                                                 context.CancellationToken);
            return(matchedNodes);
        }
Пример #3
0
        void TestFindReferences(IEntity entity)
        {
            if (IgnoreEntity(entity))
            {
                return;
            }
            FindReferences fr = new FindReferences();

            fr.FindTypeReferencesEvenIfAliased = true;

            Stopwatch w            = new Stopwatch();
            var       searchScopes = fr.GetSearchScopes(entity);

            foreach (var project in solution.Projects)
            {
                w.Restart();
                HashSet <AstNode> foundReferences = new HashSet <AstNode>();
                var interestingFiles = new HashSet <CSharpFile>();
                foreach (var searchScope in searchScopes)
                {
                    foreach (var unresolvedFile in fr.GetInterestingFiles(searchScope, project.Compilation))
                    {
                        var file = project.Files.Single(f => f.FileName == unresolvedFile.FileName);
                        Debug.Assert(file.UnresolvedTypeSystemForFile == unresolvedFile);

                        // Skip file if it doesn't contain the search term
                        if (searchScope.SearchTerm != null && file.OriginalText.IndexOf(searchScope.SearchTerm, StringComparison.Ordinal) < 0)
                        {
                            continue;
                        }

                        interestingFiles.Add(file);
                    }
                }
                foreach (var file in interestingFiles)
                {
                    fr.FindReferencesInFile(searchScopes, file.UnresolvedTypeSystemForFile, file.SyntaxTree, project.Compilation,
                                            delegate(AstNode node, ResolveResult result) {
                        foundReferences.Add(node);
                    }, CancellationToken.None);
                }
                w.Stop();
                if (timings.ContainsKey(entity.SymbolKind))
                {
                    timings[entity.SymbolKind] += w.Elapsed;
                }
                else
                {
                    timings[entity.SymbolKind] = w.Elapsed;
                }


                IEntity importedEntity = project.Compilation.Import(entity);

                HashSet <AstNode> expectedReferences;
                if (importedEntity == null || !referenceDict.TryGetValue(importedEntity, out expectedReferences))
                {
                    if (foundReferences.Any())
                    {
                        // There aren't any expected references stored, but we found some references anyways:
                        Console.WriteLine();
                        Console.WriteLine("Entity not in reference dictionary: " + entity);
                    }
                    return;
                }
                if (foundReferences.Except(expectedReferences).Any())
                {
                    Console.WriteLine();
                    Console.WriteLine("Reference mismatch for " + entity + ":");
                    var n = foundReferences.Except(expectedReferences).First();
                    Console.WriteLine("Found unexpected reference " + n + " (" + n.GetRegion() + ")");
                }
                if (expectedReferences.Except(foundReferences).Any())
                {
                    Console.WriteLine();
                    Console.WriteLine("Reference mismatch for " + entity + ":");
                    var n = expectedReferences.Except(foundReferences).First();
                    Console.WriteLine("Did not find expected reference " + n + " (" + n.GetRegion() + ")");
                }
            }

            if (entityCount.ContainsKey(entity.SymbolKind))
            {
                entityCount[entity.SymbolKind]++;
            }
            else
            {
                entityCount[entity.SymbolKind] = 1;
            }
        }
        void FindReferencesInFile(SymbolSearchArgs args, IFindReferenceSearchScope searchScope, FileName fileName, Action <SearchedFile> callback, CancellationToken cancellationToken)
        {
            ITextSource textSource = args.ParseableFileContentFinder.Create(fileName);

            if (textSource == null)
            {
                return;
            }
            if (searchScope.SearchTerm != null)
            {
                if (textSource.IndexOf(searchScope.SearchTerm, 0, textSource.TextLength, StringComparison.Ordinal) < 0)
                {
                    return;
                }
            }

            var parseInfo = SD.ParserService.Parse(fileName, textSource) as CSharpFullParseInformation;

            if (parseInfo == null)
            {
                return;
            }
            ReadOnlyDocument         document    = null;
            IHighlighter             highlighter = null;
            List <SearchResultMatch> results     = new List <SearchResultMatch>();

            // Grab the unresolved file matching the compilation version
            // (this may differ from the version created by re-parsing the project)
            CSharpUnresolvedFile unresolvedFile = null;
            IProjectContent      pc             = compilation.MainAssembly.UnresolvedAssembly as IProjectContent;

            if (pc != null)
            {
                unresolvedFile = pc.GetFile(fileName) as CSharpUnresolvedFile;
            }

            fr.FindReferencesInFile(
                searchScope, unresolvedFile, parseInfo.SyntaxTree, compilation,
                delegate(AstNode node, ResolveResult result) {
                if (document == null)
                {
                    document    = new ReadOnlyDocument(textSource, fileName);
                    highlighter = SD.EditorControlService.CreateHighlighter(document);
                    highlighter.BeginHighlighting();
                }
                Identifier identifier = node.GetChildByRole(Roles.Identifier);
                if (!identifier.IsNull)
                {
                    node = identifier;
                }
                var region           = new DomRegion(fileName, node.StartLocation, node.EndLocation);
                int offset           = document.GetOffset(node.StartLocation);
                int length           = document.GetOffset(node.EndLocation) - offset;
                var builder          = SearchResultsPad.CreateInlineBuilder(node.StartLocation, node.EndLocation, document, highlighter);
                var defaultTextColor = highlighter != null ? highlighter.DefaultTextColor : null;
                results.Add(new SearchResultMatch(fileName, node.StartLocation, node.EndLocation, offset, length, builder, defaultTextColor));
            }, cancellationToken);
            if (highlighter != null)
            {
                highlighter.Dispose();
            }
            if (results.Count > 0)
            {
                callback(new SearchedFile(fileName, results));
            }
        }
Пример #5
0
        public IEnumerable <AstNode> FindUsageNodes(Request request)
        {
            var res = _parser.ParsedContent(request.Buffer, request.FileName);
            var loc = new TextLocation(request.Line, request.Column);

            _result = new ConcurrentBag <AstNode>();
            var findReferences = new FindReferences
            {
                FindCallsThroughInterface         = true,
                FindCallsThroughVirtualBaseMethod = true,
                FindTypeReferencesEvenIfAliased   = true,
            };

            ResolveResult resolveResult = ResolveAtLocation.Resolve(res.Compilation, res.UnresolvedFile, res.SyntaxTree, loc);

            if (resolveResult is LocalResolveResult)
            {
                var variable = (resolveResult as LocalResolveResult).Variable;
                findReferences.FindLocalReferences(variable, res.UnresolvedFile, res.SyntaxTree, res.Compilation,
                                                   (node, rr) => _result.Add(node.GetDefinition()), CancellationToken.None);
            }
            else
            {
                IEntity entity = null;
                IEnumerable <IList <IFindReferenceSearchScope> > searchScopes = null;
                if (resolveResult is TypeResolveResult)
                {
                    var type = (resolveResult as TypeResolveResult).Type;
                    entity = type.GetDefinition();
                    ProcessTypeResults(type);
                    searchScopes = new[] { findReferences.GetSearchScopes(entity) };
                }

                if (resolveResult is MemberResolveResult)
                {
                    entity = (resolveResult as MemberResolveResult).Member;
                    if (entity.EntityType == EntityType.Constructor)
                    {
                        // process type instead
                        var type = entity.DeclaringType;
                        entity = entity.DeclaringTypeDefinition;
                        ProcessTypeResults(type);
                        searchScopes = new[] { findReferences.GetSearchScopes(entity) };
                    }
                    else
                    {
                        ProcessMemberResults(resolveResult);
                        var members = MemberCollector.CollectMembers(_solution,
                                                                     (resolveResult as MemberResolveResult).Member);
                        searchScopes = members.Select(findReferences.GetSearchScopes);
                    }
                }

                if (entity == null)
                {
                    return(_result);
                }

                var interesting = new List <CSharpUnresolvedFile>();

                foreach (var project in _solution.Projects)
                {
                    var pctx = project.ProjectContent.CreateCompilation();
                    interesting = (from file in project.Files
                                   select(file.ParsedFile as CSharpUnresolvedFile)).ToList();

                    foreach (var file in interesting)
                    {
                        string text = _solution.GetFile(file.FileName).Content.Text;
                        var    unit = new CSharpParser().Parse(text, file.FileName);
                        foreach (var scope in searchScopes)
                        {
                            findReferences.FindReferencesInFile(scope, file, unit,
                                                                pctx,
                                                                (node, rr) => _result.Add(node.GetIdentifier()),
                                                                CancellationToken.None);
                        }
                    }
                }
            }
            return(_result);
        }