public override async Task <RazorDocumentRangeFormattingResponse> RazorRangeFormattingAsync(RazorDocumentRangeFormattingParams request, CancellationToken cancellationToken)
        {
            var response = new RazorDocumentRangeFormattingResponse()
            {
                Edits = Array.Empty <TextEdit>()
            };

            if (request.Kind == RazorLanguageKind.Razor)
            {
                return(response);
            }

            await _joinableTaskFactory.SwitchToMainThreadAsync();

            var hostDocumentUri = new Uri(request.HostDocumentFilePath);

            if (!_documentManager.TryGetDocument(hostDocumentUri, out var documentSnapshot))
            {
                return(response);
            }

            var serverKind   = default(LanguageServerKind);
            var projectedUri = default(Uri);

            if (request.Kind == RazorLanguageKind.CSharp &&
                documentSnapshot.TryGetVirtualDocument <CSharpVirtualDocumentSnapshot>(out var csharpDocument))
            {
                serverKind   = LanguageServerKind.CSharp;
                projectedUri = csharpDocument.Uri;
            }
            else if (request.Kind == RazorLanguageKind.Html &&
                     documentSnapshot.TryGetVirtualDocument <HtmlVirtualDocumentSnapshot>(out var htmlDocument))
            {
                serverKind   = LanguageServerKind.Html;
                projectedUri = htmlDocument.Uri;
            }
            else
            {
                Debug.Fail("Unexpected RazorLanguageKind. This can't really happen in a real scenario.");
                return(response);
            }

            var formattingParams = new DocumentRangeFormattingParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = projectedUri
                },
                Range   = request.ProjectedRange,
                Options = request.Options
            };

            response.Edits = await _requestInvoker.RequestServerAsync <DocumentRangeFormattingParams, TextEdit[]>(
                Methods.TextDocumentRangeFormattingName,
                serverKind,
                formattingParams,
                cancellationToken).ConfigureAwait(false);

            return(response);
        }
コード例 #2
0
        public override async Task <RazorDocumentRangeFormattingResponse> HtmlOnTypeFormattingAsync(RazorDocumentOnTypeFormattingParams request, CancellationToken cancellationToken)
        {
            var response = new RazorDocumentRangeFormattingResponse()
            {
                Edits = Array.Empty <TextEdit>()
            };

            var hostDocumentUri = request.TextDocument.Uri;

            if (!_documentManager.TryGetDocument(hostDocumentUri, out var documentSnapshot))
            {
                return(response);
            }

            string languageServerName;
            Uri    projectedUri;

            if (documentSnapshot.TryGetVirtualDocument <HtmlVirtualDocumentSnapshot>(out var htmlDocument))
            {
                languageServerName = RazorLSPConstants.HtmlLanguageServerName;
                projectedUri       = htmlDocument.Uri;
            }
            else
            {
                Debug.Fail("Unexpected RazorLanguageKind. This can't really happen in a real scenario.");
                return(response);
            }

            var synchronized = await _documentSynchronizer.TrySynchronizeVirtualDocumentAsync(
                request.HostDocumentVersion, htmlDocument, cancellationToken);

            if (!synchronized)
            {
                return(response);
            }

            var formattingParams = new DocumentOnTypeFormattingParams()
            {
                Character    = request.Character,
                Position     = request.Position,
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = projectedUri
                },
                Options = request.Options
            };

            var textBuffer = htmlDocument.Snapshot.TextBuffer;
            var edits      = await _requestInvoker.ReinvokeRequestOnServerAsync <DocumentOnTypeFormattingParams, TextEdit[]>(
                textBuffer,
                Methods.TextDocumentOnTypeFormattingName,
                languageServerName,
                formattingParams,
                cancellationToken).ConfigureAwait(false);

            response.Edits = edits?.Response ?? Array.Empty <TextEdit>();

            return(response);
        }
コード例 #3
0
        public override async Task <RazorDocumentRangeFormattingResponse> RazorRangeFormattingAsync(RazorDocumentRangeFormattingParams request, CancellationToken cancellationToken)
        {
            var response = new RazorDocumentRangeFormattingResponse()
            {
                Edits = Array.Empty <TextEdit>()
            };

            if (request.Kind == RazorLanguageKind.Razor)
            {
                return(response);
            }

            await _joinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            var hostDocumentUri = new Uri(request.HostDocumentFilePath);

            if (!_documentManager.TryGetDocument(hostDocumentUri, out var documentSnapshot))
            {
                return(response);
            }

            string languageServerName;
            Uri    projectedUri;

            if (request.Kind == RazorLanguageKind.CSharp &&
                documentSnapshot.TryGetVirtualDocument <CSharpVirtualDocumentSnapshot>(out var csharpDocument))
            {
                languageServerName = RazorLSPConstants.RazorCSharpLanguageServerName;
                projectedUri       = csharpDocument.Uri;
            }
            else
            {
                Debug.Fail("Unexpected RazorLanguageKind. This can't really happen in a real scenario.");
                return(response);
            }

            var formattingParams = new DocumentRangeFormattingParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = projectedUri
                },
                Range   = request.ProjectedRange,
                Options = request.Options
            };

            var textBuffer = csharpDocument.Snapshot.TextBuffer;
            var edits      = await _requestInvoker.ReinvokeRequestOnServerAsync <DocumentRangeFormattingParams, TextEdit[]>(
                textBuffer,
                Methods.TextDocumentRangeFormattingName,
                languageServerName,
                formattingParams,
                cancellationToken).ConfigureAwait(false);

            response.Edits = edits?.Response ?? Array.Empty <TextEdit>();

            return(response);
        }