public Task FindReferencesAsync(SymbolSearchArgs args, Action <SearchedFile> callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }
            var cancellationToken = args.ProgressMonitor.CancellationToken;

            return(Task.Run(
                       () => {
                for (int i = 0; i < searchScopes.Count; i++)
                {
                    IFindReferenceSearchScope searchScope = searchScopes[i];
                    object progressLock = new object();
                    Parallel.ForEach(
                        interestingFileNames[i],
                        new ParallelOptions {
                        MaxDegreeOfParallelism = Environment.ProcessorCount,
                        CancellationToken = cancellationToken
                    },
                        delegate(string fileName) {
                        try {
                            FindReferencesInFile(args, searchScope, FileName.Create(fileName), callback, cancellationToken);
                        } catch (OperationCanceledException) {
                            throw;
                        } catch (Exception ex) {
                            throw new ApplicationException("Error searching in file '" + fileName + "'", ex);
                        }
                        lock (progressLock)
                            args.ProgressMonitor.Progress += workAmountInverse;
                    });
                }
            }, cancellationToken
                       ));
        }
        public Task FindReferencesAsync(SymbolSearchArgs searchArguments, Action <SearchedFile> callback)
        {
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }
            var cancellationToken = searchArguments.ProgressMonitor.CancellationToken;

            return(Task.Run(
                       () => {
                object progressLock = new object();
                Parallel.ForEach(
                    interestingFileNames,
                    new ParallelOptions {
                    MaxDegreeOfParallelism = Environment.ProcessorCount,
                    CancellationToken = cancellationToken
                },
                    delegate(FileName fileName) {
                    FindReferencesInFile(searchArguments, entity, fileName, callback, cancellationToken);
                    lock (progressLock)
                        searchArguments.ProgressMonitor.Progress += workAmountInverse;
                });
            }, cancellationToken
                       ));
        }
        void FindReferencesInFile(SymbolSearchArgs searchArguments, ISymbol entity, FileName fileName, Action <SearchedFile> callback, CancellationToken cancellationToken)
        {
            ITextSource textSource = searchArguments.ParseableFileContentFinder.Create(fileName);

            if (textSource == null)
            {
                return;
            }
            int offset = textSource.IndexOf(entity.Name, 0, textSource.TextLength, StringComparison.Ordinal);

            if (offset < 0)
            {
                return;
            }

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

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

            do
            {
                if (document == null)
                {
                    document    = new ReadOnlyDocument(textSource, fileName);
                    highlighter = SD.EditorControlService.CreateHighlighter(document);
                    highlighter.BeginHighlighting();
                }
                var result = resolver.ResolveAtLocation(document.GetLocation(offset + entity.Name.Length / 2 + 1), cancellationToken);
                int length = entity.Name.Length;
                if ((result is TypeResolveResult && ((TypeResolveResult)result).Type.Equals(entity)) || (result is MemberResolveResult && ((MemberResolveResult)result).Member.Equals(entity)))
                {
                    var region  = new DomRegion(fileName, document.GetLocation(offset), document.GetLocation(offset + length));
                    var builder = SearchResultsPad.CreateInlineBuilder(region.Begin, region.End, document, highlighter);
                    results.Add(new SearchResultMatch(fileName, document.GetLocation(offset), document.GetLocation(offset + length), offset, length, builder, highlighter.DefaultTextColor));
                }
                offset = textSource.IndexOf(entity.Name, offset + length, textSource.TextLength - offset - length, StringComparison.OrdinalIgnoreCase);
            } while (offset > 0);
            if (highlighter != null)
            {
                highlighter.Dispose();
            }
            if (results.Count > 0)
            {
                callback(new SearchedFile(fileName, results));
            }
        }
        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));
            }
        }