public async Task <Reference[]> FindAllReferencesAsync(Uri uri, SourceLocation location, ReferenceSearchOptions options, CancellationToken cancellationToken = default)
        {
            if (uri != null)
            {
                var analysis = await Document.GetAnalysisAsync(uri, _services, FindReferencesAnalysisTimeout, cancellationToken);

                var definitionSource = new DefinitionSource(_services);
                var definition       = definitionSource.FindDefinition(analysis, location, out var definingMember);
                if (definition == null)
                {
                    return(Array.Empty <Reference>());
                }

                var rootDefinition = definingMember.GetRootDefinition();
                var name           = definingMember.GetName();

                // If it is an implicitly declared variable, such as function or a class
                // then the location is invalid and the module is null. Use current module.
                var declaringModule = rootDefinition.DeclaringModule ?? analysis.Document;
                if (!string.IsNullOrEmpty(name) && (declaringModule.ModuleType == ModuleType.User || options == ReferenceSearchOptions.All))
                {
                    return(await FindAllReferencesAsync(name, declaringModule, rootDefinition, location, definitionSource, cancellationToken));
                }
            }
            return(Array.Empty <Reference>());
        }
        private async Task <Reference[]> FindAllReferencesAsync(string name, IPythonModule declaringModule, ILocatedMember rootDefinition, SourceLocation location, DefinitionSource definitionSource,
                                                                CancellationToken cancellationToken)
        {
            var candidateFiles       = ScanClosedFiles(name, cancellationToken);
            var reloadRootDefinition = false;

            if (candidateFiles.Count > 0)
            {
                reloadRootDefinition = await AnalyzeFiles(declaringModule.Interpreter.ModuleResolution, candidateFiles, cancellationToken);
            }

            if (reloadRootDefinition)
            {
                var analysis = await Document.GetAnalysisAsync(declaringModule.Uri, _services, FindReferencesAnalysisTimeout, cancellationToken);

                var definition = definitionSource.FindDefinition(analysis, location, out var definingMember);
                if (definition == null)
                {
                    return(Array.Empty <Reference>());
                }

                rootDefinition = definingMember.GetRootDefinition();
            }

            return(rootDefinition.References
                   .Select(r => new Reference {
                uri = r.DocumentUri, range = r.Span
            })
                   .ToArray());
        }
예제 #3
0
        public async Task <DocumentHighlight[]> DocumentHighlightAsync(Uri uri, SourceLocation location, CancellationToken cancellationToken = default)
        {
            if (uri == null)
            {
                return(Array.Empty <DocumentHighlight>());
            }

            var analysis = await Document.GetAnalysisAsync(uri, _services, DocumentHighlightAnalysisTimeout, cancellationToken);

            var definitionSource = new DefinitionSource(_services);

            var definition = definitionSource.FindDefinition(analysis, location, out var definingMember);

            if (definition == null || definingMember == null)
            {
                return(Array.Empty <DocumentHighlight>());
            }

            var rootDefinition = definingMember.GetRootDefinition();

            var result = rootDefinition.References
                         .Where(r => r.DocumentUri.Equals(uri))
                         .Select((r, i) => new DocumentHighlight {
                kind = (i == 0) ? DocumentHighlightKind.Write : DocumentHighlightKind.Read, range = r.Span
            })
                         .ToArray();

            return(result);
        }