Exemple #1
0
        public async Task <MetadataResponse> Handle(MetadataRequest request)
        {
            var externalSourceService = _externalSourceServiceFactory.Create(_omniSharpOptions);

            var response = new MetadataResponse();

            foreach (var project in _workspace.CurrentSolution.Projects)
            {
                var compilation = await project.GetCompilationAsync();

                var symbol = compilation.GetTypeByMetadataName(request.TypeName);
                if (symbol != null && symbol.ContainingAssembly.Name == request.AssemblyName)
                {
                    var cancellationToken = _externalSourceServiceFactory.CreateCancellationToken(_omniSharpOptions, request.Timeout);
                    var(metadataDocument, documentPath) = await externalSourceService.GetAndAddExternalSymbolDocument(project, symbol, cancellationToken);

                    if (metadataDocument != null)
                    {
                        var source = await metadataDocument.GetTextAsync();

                        response.Source     = source.ToString();
                        response.SourceName = documentPath;

                        return(response);
                    }
                }
            }
            return(response);
        }
        public async Task <GotoDefinitionResponse> Handle(GotoDefinitionRequest request)
        {
            var cancellationToken     = _externalSourceServiceFactory.CreateCancellationToken(_omnisharpOptions, request.Timeout);
            var externalSourceService = _externalSourceServiceFactory.Create(_omnisharpOptions);
            var document = externalSourceService.FindDocumentInCache(request.FileName) ??
                           _workspace.GetDocument(request.FileName);

            if (document == null)
            {
                return(new GotoDefinitionResponse());
            }

            var symbol = await GoToDefinitionHelpers.GetDefinitionSymbol(document, request.Line, request.Column, cancellationToken);

            if (symbol?.Locations.IsDefaultOrEmpty != false)
            {
                return(new GotoDefinitionResponse());
            }

            if (symbol.Locations[0].IsInSource)
            {
                return(new GotoDefinitionResponse()
                {
                    Definitions = symbol.Locations
                                  .Select(location => new Definition
                    {
                        Location = location.GetMappedLineSpan().GetLocationFromFileLinePositionSpan(),
                        SourceGeneratedFileInfo = GoToDefinitionHelpers.GetSourceGeneratedFileInfo(_workspace, location)
                    })
                                  .ToList()
                });
            }
            else
            {
                var maybeSpan = await GoToDefinitionHelpers.GetMetadataMappedSpan(document, symbol, externalSourceService, cancellationToken);

                if (maybeSpan is FileLinePositionSpan lineSpan)
                {
                    return(new GotoDefinitionResponse
                    {
                        Definitions = new()
                        {
                            new Definition
                            {
                                Location = lineSpan.GetLocationFromFileLinePositionSpan(),
                                MetadataSource = new OmniSharp.Models.Metadata.MetadataSource()
                                {
                                    AssemblyName = symbol.ContainingAssembly.Name,
                                    ProjectName = document.Project.Name,
                                    TypeName = symbol.GetSymbolName()
                                }
                            }
                        }
                    });
Exemple #3
0
        public async Task <GotoDefinitionResponse> Handle(GotoDefinitionRequest request)
        {
            var externalSourceService = _externalSourceServiceFactory.Create(_omnisharpOptions);
            var cancellationToken     = _externalSourceServiceFactory.CreateCancellationToken(_omnisharpOptions, request.Timeout);
            var document = externalSourceService.FindDocumentInCache(request.FileName) ??
                           _workspace.GetDocument(request.FileName);

            var symbol = await GoToDefinitionHelpers.GetDefinitionSymbol(document, request.Line, request.Column, cancellationToken);

            if (symbol == null)
            {
                return(new GotoDefinitionResponse());
            }

            var location = symbol.Locations.First();

            GotoDefinitionResponse?response = null;

            if (location.IsInSource)
            {
                var lineSpan = symbol.Locations.First().GetMappedLineSpan();
                response = new GotoDefinitionResponse
                {
                    FileName            = lineSpan.Path,
                    Line                = lineSpan.StartLinePosition.Line,
                    Column              = lineSpan.StartLinePosition.Character,
                    SourceGeneratedInfo = GoToDefinitionHelpers.GetSourceGeneratedFileInfo(_workspace, location)
                };
            }
            else if (location.IsInMetadata && request.WantMetadata)
            {
                var maybeSpan = await GoToDefinitionHelpers.GetMetadataMappedSpan(document, symbol, externalSourceService, cancellationToken);

                if (maybeSpan is FileLinePositionSpan lineSpan)
                {
                    response = new GotoDefinitionResponse
                    {
                        Line           = lineSpan.StartLinePosition.Line,
                        Column         = lineSpan.StartLinePosition.Character,
                        MetadataSource = new MetadataSource()
                        {
                            AssemblyName = symbol.ContainingAssembly.Name,
                            ProjectName  = document.Project.Name,
                            TypeName     = symbol.GetSymbolName()
                        },
                    };
                }
            }

            return(response ?? new GotoDefinitionResponse());
        }
Exemple #4
0
        // Based on https://github.com/OmniSharp/omnisharp-roslyn/blob/cbfca2cccaf814f3c906a49c9321f0bc898fa0e6/src/OmniSharp.Roslyn.CSharp/Services/Navigation/GotoDefinitionService.cs
        public async Task <GotoDefinitionResponse> Handle(GotoDefinitionRequest request)
        {
            if (_codeSearchServiceProvider?.CodeSearchService == null)
            {
                return(null);
            }

            var externalSourceService = _externalSourceServiceFactory.Create(_omnisharpOptions);
            var document = externalSourceService.FindDocumentInCache(request.FileName) ??
                           _workspace.GetDocument(request.FileName);

            var response = new GotoDefinitionResponse();

            if (document == null)
            {
                _logger.LogDebug($"FastCodeNav: Couldn't get document for {request.FileName}");
                return(await GetDefinitionFromCodeSearchAsync(request));
            }

            if (document != null)
            {
                var semanticModel = await document.GetSemanticModelAsync();

                var sourceText = await document.GetTextAsync();

                var position = sourceText.Lines.GetPosition(new LinePosition(request.Line, request.Column));
                var symbol   = await SymbolFinder.FindSymbolAtPositionAsync(semanticModel, position, _workspace);

                if (symbol == null)
                {
                    return(await GetDefinitionFromCodeSearchAsync(request));
                }

                // go to definition for namespaces is not supported
                if (symbol != null && !(symbol is INamespaceSymbol))
                {
                    // for partial methods, pick the one with body
                    if (symbol is IMethodSymbol method)
                    {
                        // Return an empty response for property accessor symbols like get and set
                        if (method.AssociatedSymbol is IPropertySymbol)
                        {
                            return(response);
                        }

                        symbol = method.PartialImplementationPart ?? symbol;
                    }

                    var location = symbol.Locations.First();

                    if (location.IsInSource)
                    {
                        var lineSpan = symbol.Locations.First().GetMappedLineSpan();
                        response = new GotoDefinitionResponse
                        {
                            FileName = lineSpan.Path,
                            Line     = lineSpan.StartLinePosition.Line,
                            Column   = lineSpan.StartLinePosition.Character
                        };
                    }
                    else if (location.IsInMetadata && request.WantMetadata)
                    {
                        var cancellationToken = _externalSourceServiceFactory.CreateCancellationToken(_omnisharpOptions, request.Timeout);
                        var(metadataDocument, _) = await externalSourceService.GetAndAddExternalSymbolDocument(document.Project, symbol, cancellationToken);

                        if (metadataDocument != null)
                        {
                            cancellationToken = _externalSourceServiceFactory.CreateCancellationToken(_omnisharpOptions, request.Timeout);
                            var metadataLocation = await externalSourceService.GetExternalSymbolLocation(symbol, metadataDocument, cancellationToken);

                            var lineSpan = metadataLocation.GetMappedLineSpan();

                            response = new GotoDefinitionResponse
                            {
                                Line           = lineSpan.StartLinePosition.Line,
                                Column         = lineSpan.StartLinePosition.Character,
                                MetadataSource = new MetadataSource()
                                {
                                    AssemblyName = symbol.ContainingAssembly.Name,
                                    ProjectName  = document.Project.Name,
                                    TypeName     = symbol.GetSymbolName()
                                },
                            };
                        }
                    }
                }
            }
            return(response);
        }