From 45c0de96a08812aff07974b859bc2327e7fe25ed Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 30 May 2025 14:51:59 -0700 Subject: [PATCH 1/2] Get declaration diagnostics in LSP --- internal/ls/diagnostics.go | 45 ++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/internal/ls/diagnostics.go b/internal/ls/diagnostics.go index 3635d8f043..eb3c1bec43 100644 --- a/internal/ls/diagnostics.go +++ b/internal/ls/diagnostics.go @@ -10,36 +10,43 @@ import ( func (l *LanguageService) GetDocumentDiagnostics(ctx context.Context, documentURI lsproto.DocumentUri) (*lsproto.DocumentDiagnosticReport, error) { program, file := l.getProgramAndFile(documentURI) - syntaxDiagnostics := program.GetSyntacticDiagnostics(ctx, file) - var lspDiagnostics []*lsproto.Diagnostic - if len(syntaxDiagnostics) != 0 { - lspDiagnostics = make([]*lsproto.Diagnostic, 0, len(syntaxDiagnostics)) - for _, diag := range syntaxDiagnostics { - lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(diag, l.converters)) - } - } else { - diagnostics := program.GetSemanticDiagnostics(ctx, file) - suggestionDiagnostics := program.GetSuggestionDiagnostics(ctx, file) - lspDiagnostics = make([]*lsproto.Diagnostic, 0, len(diagnostics)+len(suggestionDiagnostics)) - for _, diag := range diagnostics { - lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(diag, l.converters)) - } - for _, diag := range suggestionDiagnostics { - // !!! user preference for suggestion diagnostics; keep only unnecessary/dprecated? - lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(diag, l.converters)) + diagnostics := make([][]*ast.Diagnostic, 0, 3) + if syntaxDiagnostics := program.GetSyntacticDiagnostics(ctx, file); len(syntaxDiagnostics) != 0 { + diagnostics = append(diagnostics, syntaxDiagnostics) + } else { + diagnostics = append(diagnostics, program.GetSemanticDiagnostics(ctx, file)) + // !!! user preference for suggestion diagnostics; keep only unnecessary/deprecated? + // See: https://github.com/microsoft/vscode/blob/3dbc74129aaae102e5cb485b958fa5360e8d3e7a/extensions/typescript-language-features/src/languageFeatures/diagnostics.ts#L114 + diagnostics = append(diagnostics, program.GetSuggestionDiagnostics(ctx, file)) + if program.Options().GetEmitDeclarations() { + diagnostics = append(diagnostics, program.GetDeclarationDiagnostics(ctx, file)) } } + return &lsproto.DocumentDiagnosticReport{ RelatedFullDocumentDiagnosticReport: &lsproto.RelatedFullDocumentDiagnosticReport{ FullDocumentDiagnosticReport: lsproto.FullDocumentDiagnosticReport{ - Kind: lsproto.StringLiteralFull{}, - Items: lspDiagnostics, + Items: toLSPDiagnostics(diagnostics...), }, }, }, nil } +func toLSPDiagnostics(diagnostics ...[]*ast.Diagnostic) []*lsproto.Diagnostic { + size := 0 + for _, diagSlice := range diagnostics { + size += len(diagSlice) + } + lspDiagnostics := make([]*lsproto.Diagnostic, 0, size) + for _, diagSlice := range diagnostics { + for _, diag := range diagSlice { + lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(diag, nil)) // converters can be nil if not needed + } + } + return lspDiagnostics +} + func toLSPDiagnostic(diagnostic *ast.Diagnostic, converters *Converters) *lsproto.Diagnostic { var severity lsproto.DiagnosticSeverity switch diagnostic.Category() { From 9e170a897a3bc56c1e061bc0ea26040059898c80 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 4 Jun 2025 16:24:34 -0700 Subject: [PATCH 2/2] Converters --- internal/ls/diagnostics.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/ls/diagnostics.go b/internal/ls/diagnostics.go index eb3c1bec43..f24b954629 100644 --- a/internal/ls/diagnostics.go +++ b/internal/ls/diagnostics.go @@ -27,13 +27,13 @@ func (l *LanguageService) GetDocumentDiagnostics(ctx context.Context, documentUR return &lsproto.DocumentDiagnosticReport{ RelatedFullDocumentDiagnosticReport: &lsproto.RelatedFullDocumentDiagnosticReport{ FullDocumentDiagnosticReport: lsproto.FullDocumentDiagnosticReport{ - Items: toLSPDiagnostics(diagnostics...), + Items: toLSPDiagnostics(l.converters, diagnostics...), }, }, }, nil } -func toLSPDiagnostics(diagnostics ...[]*ast.Diagnostic) []*lsproto.Diagnostic { +func toLSPDiagnostics(converters *Converters, diagnostics ...[]*ast.Diagnostic) []*lsproto.Diagnostic { size := 0 for _, diagSlice := range diagnostics { size += len(diagSlice) @@ -41,13 +41,13 @@ func toLSPDiagnostics(diagnostics ...[]*ast.Diagnostic) []*lsproto.Diagnostic { lspDiagnostics := make([]*lsproto.Diagnostic, 0, size) for _, diagSlice := range diagnostics { for _, diag := range diagSlice { - lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(diag, nil)) // converters can be nil if not needed + lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(converters, diag)) } } return lspDiagnostics } -func toLSPDiagnostic(diagnostic *ast.Diagnostic, converters *Converters) *lsproto.Diagnostic { +func toLSPDiagnostic(converters *Converters, diagnostic *ast.Diagnostic) *lsproto.Diagnostic { var severity lsproto.DiagnosticSeverity switch diagnostic.Category() { case diagnostics.CategorySuggestion: