Skip to content

Commit 8ac3092

Browse files
authored
Respect client capabilities for diagnostics (#1980)
1 parent d891e4f commit 8ac3092

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

internal/fourslash/fourslash.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,17 @@ func getCapabilitiesWithDefaults(capabilities *lsproto.ClientCapabilities) *lspr
261261
if capabilitiesWithDefaults.TextDocument.Completion == nil {
262262
capabilitiesWithDefaults.TextDocument.Completion = defaultCompletionCapabilities
263263
}
264+
if capabilitiesWithDefaults.TextDocument.Diagnostic == nil {
265+
capabilitiesWithDefaults.TextDocument.Diagnostic = &lsproto.DiagnosticClientCapabilities{
266+
RelatedInformation: ptrTrue,
267+
TagSupport: &lsproto.ClientDiagnosticsTagOptions{
268+
ValueSet: []lsproto.DiagnosticTag{
269+
lsproto.DiagnosticTagUnnecessary,
270+
lsproto.DiagnosticTagDeprecated,
271+
},
272+
},
273+
}
274+
}
264275
if capabilitiesWithDefaults.Workspace == nil {
265276
capabilitiesWithDefaults.Workspace = &lsproto.WorkspaceClientCapabilities{}
266277
}

internal/ls/diagnostics.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ls
22

33
import (
44
"context"
5+
"slices"
56
"strings"
67

78
"github.com/microsoft/typescript-go/internal/ast"
@@ -11,7 +12,7 @@ import (
1112
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
1213
)
1314

14-
func (l *LanguageService) ProvideDiagnostics(ctx context.Context, uri lsproto.DocumentUri) (lsproto.DocumentDiagnosticResponse, error) {
15+
func (l *LanguageService) ProvideDiagnostics(ctx context.Context, uri lsproto.DocumentUri, clientOptions *lsproto.DiagnosticClientCapabilities) (lsproto.DocumentDiagnosticResponse, error) {
1516
program, file := l.getProgramAndFile(uri)
1617

1718
diagnostics := make([][]*ast.Diagnostic, 0, 4)
@@ -26,26 +27,26 @@ func (l *LanguageService) ProvideDiagnostics(ctx context.Context, uri lsproto.Do
2627

2728
return lsproto.RelatedFullDocumentDiagnosticReportOrUnchangedDocumentDiagnosticReport{
2829
FullDocumentDiagnosticReport: &lsproto.RelatedFullDocumentDiagnosticReport{
29-
Items: toLSPDiagnostics(l.converters, diagnostics...),
30+
Items: l.toLSPDiagnostics(clientOptions, diagnostics...),
3031
},
3132
}, nil
3233
}
3334

34-
func toLSPDiagnostics(converters *lsconv.Converters, diagnostics ...[]*ast.Diagnostic) []*lsproto.Diagnostic {
35+
func (l *LanguageService) toLSPDiagnostics(clientOptions *lsproto.DiagnosticClientCapabilities, diagnostics ...[]*ast.Diagnostic) []*lsproto.Diagnostic {
3536
size := 0
3637
for _, diagSlice := range diagnostics {
3738
size += len(diagSlice)
3839
}
3940
lspDiagnostics := make([]*lsproto.Diagnostic, 0, size)
4041
for _, diagSlice := range diagnostics {
4142
for _, diag := range diagSlice {
42-
lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(converters, diag))
43+
lspDiagnostics = append(lspDiagnostics, l.toLSPDiagnostic(clientOptions, diag))
4344
}
4445
}
4546
return lspDiagnostics
4647
}
4748

48-
func toLSPDiagnostic(converters *lsconv.Converters, diagnostic *ast.Diagnostic) *lsproto.Diagnostic {
49+
func (l *LanguageService) toLSPDiagnostic(clientOptions *lsproto.DiagnosticClientCapabilities, diagnostic *ast.Diagnostic) *lsproto.Diagnostic {
4950
var severity lsproto.DiagnosticSeverity
5051
switch diagnostic.Category() {
5152
case diagnostics.CategorySuggestion:
@@ -58,30 +59,33 @@ func toLSPDiagnostic(converters *lsconv.Converters, diagnostic *ast.Diagnostic)
5859
severity = lsproto.DiagnosticSeverityError
5960
}
6061

61-
relatedInformation := make([]*lsproto.DiagnosticRelatedInformation, 0, len(diagnostic.RelatedInformation()))
62-
for _, related := range diagnostic.RelatedInformation() {
63-
relatedInformation = append(relatedInformation, &lsproto.DiagnosticRelatedInformation{
64-
Location: lsproto.Location{
65-
Uri: lsconv.FileNameToDocumentURI(related.File().FileName()),
66-
Range: converters.ToLSPRange(related.File(), related.Loc()),
67-
},
68-
Message: related.Message(),
69-
})
62+
var relatedInformation []*lsproto.DiagnosticRelatedInformation
63+
if clientOptions != nil && ptrIsTrue(clientOptions.RelatedInformation) {
64+
relatedInformation = make([]*lsproto.DiagnosticRelatedInformation, 0, len(diagnostic.RelatedInformation()))
65+
for _, related := range diagnostic.RelatedInformation() {
66+
relatedInformation = append(relatedInformation, &lsproto.DiagnosticRelatedInformation{
67+
Location: lsproto.Location{
68+
Uri: lsconv.FileNameToDocumentURI(related.File().FileName()),
69+
Range: l.converters.ToLSPRange(related.File(), related.Loc()),
70+
},
71+
Message: related.Message(),
72+
})
73+
}
7074
}
7175

7276
var tags []lsproto.DiagnosticTag
73-
if diagnostic.ReportsUnnecessary() || diagnostic.ReportsDeprecated() {
77+
if clientOptions != nil && clientOptions.TagSupport != nil && (diagnostic.ReportsUnnecessary() || diagnostic.ReportsDeprecated()) {
7478
tags = make([]lsproto.DiagnosticTag, 0, 2)
75-
if diagnostic.ReportsUnnecessary() {
79+
if diagnostic.ReportsUnnecessary() && slices.Contains(clientOptions.TagSupport.ValueSet, lsproto.DiagnosticTagUnnecessary) {
7680
tags = append(tags, lsproto.DiagnosticTagUnnecessary)
7781
}
78-
if diagnostic.ReportsDeprecated() {
82+
if diagnostic.ReportsDeprecated() && slices.Contains(clientOptions.TagSupport.ValueSet, lsproto.DiagnosticTagDeprecated) {
7983
tags = append(tags, lsproto.DiagnosticTagDeprecated)
8084
}
8185
}
8286

8387
return &lsproto.Diagnostic{
84-
Range: converters.ToLSPRange(diagnostic.File(), diagnostic.Loc()),
88+
Range: l.converters.ToLSPRange(diagnostic.File(), diagnostic.Loc()),
8589
Code: &lsproto.IntegerOrString{
8690
Integer: ptrTo(diagnostic.Code()),
8791
},

internal/lsp/server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,11 @@ func (s *Server) handleSetTrace(ctx context.Context, params *lsproto.SetTracePar
800800
}
801801

802802
func (s *Server) handleDocumentDiagnostic(ctx context.Context, ls *ls.LanguageService, params *lsproto.DocumentDiagnosticParams) (lsproto.DocumentDiagnosticResponse, error) {
803-
return ls.ProvideDiagnostics(ctx, params.TextDocument.Uri)
803+
var diagnosticClientCapabilities *lsproto.DiagnosticClientCapabilities
804+
if s.initializeParams != nil && s.initializeParams.Capabilities != nil && s.initializeParams.Capabilities.TextDocument != nil {
805+
diagnosticClientCapabilities = s.initializeParams.Capabilities.TextDocument.Diagnostic
806+
}
807+
return ls.ProvideDiagnostics(ctx, params.TextDocument.Uri, diagnosticClientCapabilities)
804808
}
805809

806810
func (s *Server) handleHover(ctx context.Context, ls *ls.LanguageService, params *lsproto.HoverParams) (lsproto.HoverResponse, error) {

0 commit comments

Comments
 (0)