Ejemplo n.º 1
0
 private async Task SendDiagnosticsNotificationAsync(Uri uri, ImmutableArray <LanguageServer.Protocol.Diagnostic> diagnostics)
 {
     var publishDiagnosticsParams = new PublishDiagnosticParams {
         Diagnostics = diagnostics.ToArray(), Uri = uri
     };
     await _jsonRpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, publishDiagnosticsParams).ConfigureAwait(false);
 }
Ejemplo n.º 2
0
        private async void DiagnosticService_DiagnosticsUpdated(object sender, DiagnosticsUpdatedArgs e)
#pragma warning restore VSTHRD100 // Avoid async void methods
        {
            // Since this is an async void method, exceptions here will crash the host VS. We catch exceptions here to make sure that we don't crash the host since
            // the worst outcome here is that guests may not see all diagnostics.
            try
            {
                // LSP doesnt support diagnostics without a document. So if we get project level diagnostics without a document, ignore them.
                if (e.DocumentId != null && e.Solution != null)
                {
                    var document = e.Solution.GetDocument(e.DocumentId);
                    if (document == null || document.FilePath == null)
                    {
                        return;
                    }

                    // Only publish document diagnostics for the languages this provider supports.
                    if (document.Project.Language != LanguageNames.CSharp && document.Project.Language != LanguageNames.VisualBasic)
                    {
                        return;
                    }

                    // LSP does not currently support publishing diagnostics incrememntally, so we re-publish all diagnostics.
                    var diagnostics = await GetDiagnosticsAsync(e.Solution, document, CancellationToken.None).ConfigureAwait(false);

                    var publishDiagnosticsParams = new PublishDiagnosticParams {
                        Diagnostics = diagnostics, Uri = document.GetURI()
                    };
                    await _jsonRpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, publishDiagnosticsParams).ConfigureAwait(false);
                }
            }
            catch (Exception ex) when(FatalError.ReportWithoutCrash(ex))
            {
            }
        }
            static InterceptionResult CreateResponse(PublishDiagnosticParams diagnosticParams)
            {
                var newToken           = JToken.FromObject(diagnosticParams);
                var interceptionResult = new InterceptionResult(newToken, changedDocumentUri: true);

                return(interceptionResult);
            }
        public async Task ApplyChangesAsync_VirtualHtmlDocumentNotFound_ReturnsEmptyDiagnosticResponse()
        {
            // Arrange
            var diagnosticsProvider = Mock.Of <LSPDiagnosticsTranslator>(MockBehavior.Strict);

            var testVirtualDocument          = new TestVirtualDocumentSnapshot(s_razorUri, hostDocumentVersion: 0);
            LSPDocumentSnapshot testDocument = new TestLSPDocumentSnapshot(s_razorUri, version: 0, testVirtualDocument);
            var documentManager = new Mock <TrackingLSPDocumentManager>(MockBehavior.Strict);

            documentManager.Setup(manager => manager.TryGetDocument(It.IsAny <Uri>(), out testDocument))
            .Returns(true);

            var htmlDiagnosticsInterceptor = new RazorHtmlPublishDiagnosticsInterceptor(documentManager.Object, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new PublishDiagnosticParams()
            {
                Diagnostics = s_diagnostics,
                Uri         = s_razorVirtualHtmlUri
            };

            // Act
            var result = await htmlDiagnosticsInterceptor.ApplyChangesAsync(JToken.FromObject(diagnosticRequest), string.Empty, cancellationToken : default).ConfigureAwait(false);

            // Assert
            var updatedParams = result.UpdatedToken.ToObject <PublishDiagnosticParams>();

            Assert.Empty(updatedParams.Diagnostics);
            Assert.Equal(s_razorUri, updatedParams.Uri);
            Assert.True(result.ChangedDocumentUri);
        }
Ejemplo n.º 5
0
        private async Task PublishDiagnosticsAsync(Document document)
        {
            var diagnostics = await GetDiagnosticsAsync(document, CancellationToken.None).ConfigureAwait(false);

            var publishDiagnosticsParams = new PublishDiagnosticParams {
                Diagnostics = diagnostics, Uri = document.GetURI()
            };
            await _jsonRpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, publishDiagnosticsParams).ConfigureAwait(false);
        }
Ejemplo n.º 6
0
        public void SendDiagnostics()
        {
            if (this.textDocument == null || this.diagnostics == null)
            {
                return;
            }

            string[] lines = this.textDocument.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            List <Diagnostic> diagnostics = new List <Diagnostic>();

            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i];

                int j = 0;
                while (j < line.Length)
                {
                    Diagnostic diagnostic = null;
                    foreach (var tag in this.diagnostics)
                    {
                        diagnostic = GetDiagnostic(line, i, ref j, tag.Key, tag.Value);

                        if (diagnostic != null)
                        {
                            break;
                        }
                    }

                    if (diagnostic == null)
                    {
                        ++j;
                    }
                    else
                    {
                        diagnostics.Add(diagnostic);
                    }
                }
            }

            PublishDiagnosticParams parameter = new PublishDiagnosticParams();

            parameter.Uri         = textDocument.Uri;
            parameter.Diagnostics = diagnostics.ToArray();

            if (this.maxProblems > -1)
            {
                parameter.Diagnostics = parameter.Diagnostics.Take(this.maxProblems).ToArray();
            }

            Log(Methods.TextDocumentPublishDiagnostics, parameter);

            this.rpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnostics, parameter);
            this.rpc.NotifyWithParameterObjectAsync("test/customNotification", parameter);
        }
Ejemplo n.º 7
0
        public void SendDiagnostics()
        {
            if (textDocument == null || this.diagnostics == null)
            {
                return;
            }

            string[] lines = textDocument.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            List <Diagnostic> diagnostics = new List <Diagnostic>();

            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];

                int j = 0;
                while (j < line.Length)
                {
                    Diagnostic diagnostic = null;
                    foreach (KeyValuePair <string, DiagnosticSeverity> tag in this.diagnostics)
                    {
                        diagnostic = GetDiagnostic(line, i, ref j, tag.Key, tag.Value);

                        if (diagnostic != null)
                        {
                            break;
                        }
                    }

                    if (diagnostic == null)
                    {
                        ++j;
                    }
                    else
                    {
                        diagnostics.Add(diagnostic);
                    }
                }
            }

            PublishDiagnosticParams parameter = new PublishDiagnosticParams
            {
                Uri         = textDocument.Uri,
                Diagnostics = diagnostics.ToArray()
            };

            if (maxProblems > -1)
            {
                parameter.Diagnostics = parameter.Diagnostics.Take(maxProblems).ToArray();
            }

            rpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, parameter);
        }
Ejemplo n.º 8
0
        public void SendDiagnostics(List <Diagnostic> diagnostics, string path)
        {
            PublishDiagnosticParams parameter = new PublishDiagnosticParams
            {
                Uri         = new Uri(path),
                Diagnostics = this.maxProblems > -1
                    ? diagnostics.Take(this.maxProblems).ToArray()
                    : diagnostics.ToArray()
            };

            this.rpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, parameter);
        }
        public async Task ApplyChangesAsync_RazorUriNotSupported_ReturnsDefaultResponse()
        {
            // Arrange
            var documentManager     = new TestDocumentManager();
            var diagnosticsProvider = Mock.Of <LSPDiagnosticsTranslator>(MockBehavior.Strict);

            var htmlDiagnosticsInterceptor = new RazorHtmlPublishDiagnosticsInterceptor(documentManager, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new PublishDiagnosticParams()
            {
                Diagnostics = s_diagnostics,
                Uri         = s_razorUri
            };
            var token = JToken.FromObject(diagnosticRequest);

            // Act
            var result = await htmlDiagnosticsInterceptor.ApplyChangesAsync(token, containedLanguageName : string.Empty, cancellationToken : default).ConfigureAwait(false);

            // Assert
            Assert.Same(token, result.UpdatedToken);
            Assert.False(result.ChangedDocumentUri);
        }
        public async Task ApplyChangesAsync_ProcessesDiagnostics_ReturnsDiagnosticResponse()
        {
            // Arrange
            var documentManager     = CreateDocumentManager();
            var diagnosticsProvider = GetDiagnosticsProvider();

            var htmlDiagnosticsInterceptor = new RazorHtmlPublishDiagnosticsInterceptor(documentManager, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new PublishDiagnosticParams()
            {
                Diagnostics = s_diagnostics,
                Uri         = s_razorVirtualHtmlUri
            };

            // Act
            var result = await htmlDiagnosticsInterceptor.ApplyChangesAsync(JToken.FromObject(diagnosticRequest), string.Empty, cancellationToken : default).ConfigureAwait(false);

            // Assert
            var updatedParams = result.UpdatedToken.ToObject <PublishDiagnosticParams>();

            Assert.Equal(s_diagnostics, updatedParams.Diagnostics);
            Assert.Equal(s_razorUri, updatedParams.Uri);
            Assert.True(result.ChangedDocumentUri);
        }
Ejemplo n.º 11
0
 internal Task PublishDiagnosticsAsync(PublishDiagnosticParams diagnostics) =>
 this.NotifyClientAsync(Methods.TextDocumentPublishDiagnosticsName, diagnostics);
 public void OnPublishDiagnostics(PublishDiagnosticParams diagnostic)
 {
     DiagnosticsPublished?.Invoke(this, new DiagnosticsEventArgs(diagnostic));
 }
 static InterceptionResult CreateEmptyDiagnosticsResponse(PublishDiagnosticParams diagnosticParams)
 {
     diagnosticParams.Diagnostics = Array.Empty <Diagnostic>();
     return(CreateResponse(diagnosticParams));
 }
 public DiagnosticsEventArgs(PublishDiagnosticParams diagnostic)
 {
     this.diagnostic = diagnostic;
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Sends the textDocument/publishDiagnostics notification to the LSP client.
 /// </summary>
 /// <returns>A task to send the textDocument/publishDiagnostics message.</returns>
 public static Task PublishDiagnosticsAsync(this ProviderContext providerContext, PublishDiagnosticParams @params)
 {
     return(Dispatch(providerContext, "textDocument/publishDiagnostics", @params));
 }
Ejemplo n.º 16
0
        public void SendDiagnostics(List <DiagnosticInfo> list)
        {
            var files = list.Select(l => l.Document).OrderBy(q => q).Distinct().ToList();

            // If the computed set is empty it has to push the empty array to clear former diagnostics.
            foreach (var file in files)
            {
                PublishDiagnosticParams parameter = new PublishDiagnosticParams
                {
                    Uri         = file,
                    Diagnostics = Array.Empty <Diagnostic>()
                };
                _ = rpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, parameter);
            }
            foreach (var file in files)
            {
                List <Diagnostic> diagnostics = new List <Diagnostic>();
                foreach (var info in list)
                {
                    DiagnosticSeverity severity = default;
                    switch (info.Severify)
                    {
                    case DiagnosticInfo.Severity.Info:
                        severity = DiagnosticSeverity.Information;
                        break;

                    case DiagnosticInfo.Severity.Warning:
                        severity = DiagnosticSeverity.Warning;
                        break;

                    case DiagnosticInfo.Severity.Error:
                        severity = DiagnosticSeverity.Error;
                        break;
                    }
                    var document = target.CheckDoc(info.Document);
                    (int, int)bs = new LanguageServer.Module().GetLineColumn(info.Start, document);
                    (int, int)be = new LanguageServer.Module().GetLineColumn(info.End, document);
                    Diagnostic diagnostic = new Diagnostic
                    {
                        Message  = info.Message,
                        Severity = severity,
                        Range    = new LspTypes.Range
                        {
                            Start = new Position(bs.Item1, bs.Item2),
                            End   = new Position(be.Item1, be.Item2)
                        },
                        Code = "Test" + Enum.GetName(typeof(DiagnosticSeverity), severity)
                    };
                    diagnostics.Add(diagnostic);
                }

                PublishDiagnosticParams parameter = new PublishDiagnosticParams
                {
                    Uri         = file,
                    Diagnostics = diagnostics.ToArray()
                };
                if (maxProblems > -1)
                {
                    parameter.Diagnostics = parameter.Diagnostics.Take(maxProblems).ToArray();
                }
                _ = rpc.NotifyWithParameterObjectAsync(Methods.TextDocumentPublishDiagnosticsName, parameter);
            }
        }