コード例 #1
0
        public async Task <Location[]> HandleRequestAsync(TextDocumentPositionParams request, ClientCapabilities clientCapabilities, CancellationToken cancellationToken)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (clientCapabilities is null)
            {
                throw new ArgumentNullException(nameof(clientCapabilities));
            }

            if (!_documentManager.TryGetDocument(request.TextDocument.Uri, out var documentSnapshot))
            {
                return(null);
            }

            var projectionResult = await _projectionProvider.GetProjectionAsync(documentSnapshot, request.Position, cancellationToken).ConfigureAwait(false);

            if (projectionResult == null || projectionResult.LanguageKind != RazorLanguageKind.CSharp)
            {
                return(null);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var serverKind = LanguageServerKind.CSharp;
            var textDocumentPositionParams = new TextDocumentPositionParams()
            {
                Position     = projectionResult.Position,
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = projectionResult.Uri
                }
            };

            var result = await _requestInvoker.ReinvokeRequestOnServerAsync <TextDocumentPositionParams, Location[]>(
                Methods.TextDocumentImplementationName,
                serverKind,
                textDocumentPositionParams,
                cancellationToken).ConfigureAwait(false);

            if (result == null || result.Length == 0)
            {
                return(result);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var remappedLocations = await _documentMappingProvider.RemapLocationsAsync(result, cancellationToken).ConfigureAwait(false);

            return(remappedLocations);
        }
コード例 #2
0
        public async Task <Location[]> HandleRequestAsync(TextDocumentPositionParams request, ClientCapabilities clientCapabilities, CancellationToken cancellationToken)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (clientCapabilities is null)
            {
                throw new ArgumentNullException(nameof(clientCapabilities));
            }

            _logger.LogInformation($"Starting request for {request.TextDocument.Uri}.");

            if (!_documentManager.TryGetDocument(request.TextDocument.Uri, out var documentSnapshot))
            {
                _logger.LogWarning($"Failed to find document {request.TextDocument.Uri}.");
                return(null);
            }

            var projectionResult = await _projectionProvider.GetProjectionAsync(
                documentSnapshot,
                request.Position,
                cancellationToken).ConfigureAwait(false);

            if (projectionResult == null)
            {
                return(null);
            }

            cancellationToken.ThrowIfCancellationRequested();

            var textDocumentPositionParams = new TextDocumentPositionParams()
            {
                Position     = projectionResult.Position,
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = projectionResult.Uri
                }
            };

            var serverKind         = projectionResult.LanguageKind.ToLanguageServerKind();
            var languageServerName = serverKind.ToLanguageServerName();

            _logger.LogInformation($"Requesting {languageServerName} implementation for {projectionResult.Uri}.");

            var textBuffer = serverKind.GetTextBuffer(documentSnapshot);
            var response   = await _requestInvoker.ReinvokeRequestOnServerAsync <TextDocumentPositionParams, Location[]>(
                textBuffer,
                Methods.TextDocumentImplementationName,
                languageServerName,
                textDocumentPositionParams,
                cancellationToken).ConfigureAwait(false);

            if (!ReinvocationResponseHelper.TryExtractResultOrLog(response, _logger, languageServerName, out var locations))
            {
                return(null);
            }

            if (locations.Length == 0)
            {
                _logger.LogInformation("Received no results.");
                return(locations);
            }

            _logger.LogInformation($"Received {locations.Length} results, remapping.");

            cancellationToken.ThrowIfCancellationRequested();

            var remappedLocations = await _documentMappingProvider.RemapLocationsAsync(locations, cancellationToken).ConfigureAwait(false);

            _logger.LogInformation($"Returning {remappedLocations?.Length} locations.");
            return(remappedLocations);
        }
コード例 #3
0
        public async Task <SumType <Location[]?, VSInternalReferenceItem[]?> > HandleRequestAsync(TextDocumentPositionParams request, ClientCapabilities clientCapabilities, CancellationToken cancellationToken)
        {
            if (request is null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (clientCapabilities is null)
            {
                throw new ArgumentNullException(nameof(clientCapabilities));
            }

            _logger.LogInformation($"Starting request for {request.TextDocument.Uri}.");

            if (!_documentManager.TryGetDocument(request.TextDocument.Uri, out var documentSnapshot))
            {
                _logger.LogWarning($"Failed to find document {request.TextDocument.Uri}.");
                return(new());
            }

            var projectionResult = await _projectionProvider.GetProjectionAsync(
                documentSnapshot,
                request.Position,
                cancellationToken).ConfigureAwait(false);

            if (projectionResult is null)
            {
                return(new());
            }

            cancellationToken.ThrowIfCancellationRequested();

            var textDocumentPositionParams = new TextDocumentPositionParams()
            {
                Position     = projectionResult.Position,
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = projectionResult.Uri
                }
            };

            var serverKind         = projectionResult.LanguageKind.ToLanguageServerKind();
            var languageServerName = serverKind.ToLanguageServerName();

            _logger.LogInformation($"Requesting {languageServerName} implementation for {projectionResult.Uri}.");

            var textBuffer = serverKind.GetTextBuffer(documentSnapshot);
            var response   = await _requestInvoker.ReinvokeRequestOnServerAsync <TextDocumentPositionParams, SumType <Location[]?, VSInternalReferenceItem[]?> >(
                textBuffer,
                Methods.TextDocumentImplementationName,
                languageServerName,
                textDocumentPositionParams,
                cancellationToken).ConfigureAwait(false);

            if (!ReinvocationResponseHelper.TryExtractResultOrLog(response, _logger, languageServerName, out var result))
            {
                return(new());
            }

            cancellationToken.ThrowIfCancellationRequested();

            // From some language servers we get VSInternalReferenceItem results, and from some we get Location results.
            // We check for the _vs_id property, which is required in VSInternalReferenceItem, to know which is which.
            if (result.Value is VSInternalReferenceItem[] referenceItems)
            {
                var remappedLocations = await FindAllReferencesHandler.RemapReferenceItemsAsync(referenceItems, _documentMappingProvider, _documentManager, cancellationToken).ConfigureAwait(false);

                _logger.LogInformation($"Returning {remappedLocations?.Length} internal reference items.");
                return(remappedLocations);
            }
            else if (result.Value is Location[] locations)
            {
                var remappedLocations = await _documentMappingProvider.RemapLocationsAsync(locations, cancellationToken).ConfigureAwait(false);

                _logger.LogInformation($"Returning {remappedLocations?.Length} locations.");

                return(remappedLocations);
            }

            _logger.LogInformation("Received no results.");
            return(Array.Empty <VSInternalReferenceItem>());
        }