Skip to content

Commit c830924

Browse files
authored
Get declaration diagnostics in LSP (#1000)
1 parent f2e0063 commit c830924

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

internal/ls/diagnostics.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,44 @@ import (
1010

1111
func (l *LanguageService) GetDocumentDiagnostics(ctx context.Context, documentURI lsproto.DocumentUri) (*lsproto.DocumentDiagnosticReport, error) {
1212
program, file := l.getProgramAndFile(documentURI)
13-
syntaxDiagnostics := program.GetSyntacticDiagnostics(ctx, file)
14-
var lspDiagnostics []*lsproto.Diagnostic
15-
if len(syntaxDiagnostics) != 0 {
16-
lspDiagnostics = make([]*lsproto.Diagnostic, 0, len(syntaxDiagnostics))
17-
for _, diag := range syntaxDiagnostics {
18-
lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(diag, l.converters))
19-
}
20-
} else {
21-
diagnostics := program.GetSemanticDiagnostics(ctx, file)
22-
suggestionDiagnostics := program.GetSuggestionDiagnostics(ctx, file)
2313

24-
lspDiagnostics = make([]*lsproto.Diagnostic, 0, len(diagnostics)+len(suggestionDiagnostics))
25-
for _, diag := range diagnostics {
26-
lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(diag, l.converters))
27-
}
28-
for _, diag := range suggestionDiagnostics {
29-
// !!! user preference for suggestion diagnostics; keep only unnecessary/dprecated?
30-
lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(diag, l.converters))
14+
diagnostics := make([][]*ast.Diagnostic, 0, 3)
15+
if syntaxDiagnostics := program.GetSyntacticDiagnostics(ctx, file); len(syntaxDiagnostics) != 0 {
16+
diagnostics = append(diagnostics, syntaxDiagnostics)
17+
} else {
18+
diagnostics = append(diagnostics, program.GetSemanticDiagnostics(ctx, file))
19+
// !!! user preference for suggestion diagnostics; keep only unnecessary/deprecated?
20+
// See: https://github.com/microsoft/vscode/blob/3dbc74129aaae102e5cb485b958fa5360e8d3e7a/extensions/typescript-language-features/src/languageFeatures/diagnostics.ts#L114
21+
diagnostics = append(diagnostics, program.GetSuggestionDiagnostics(ctx, file))
22+
if program.Options().GetEmitDeclarations() {
23+
diagnostics = append(diagnostics, program.GetDeclarationDiagnostics(ctx, file))
3124
}
3225
}
26+
3327
return &lsproto.DocumentDiagnosticReport{
3428
RelatedFullDocumentDiagnosticReport: &lsproto.RelatedFullDocumentDiagnosticReport{
3529
FullDocumentDiagnosticReport: lsproto.FullDocumentDiagnosticReport{
36-
Kind: lsproto.StringLiteralFull{},
37-
Items: lspDiagnostics,
30+
Items: toLSPDiagnostics(l.converters, diagnostics...),
3831
},
3932
},
4033
}, nil
4134
}
4235

43-
func toLSPDiagnostic(diagnostic *ast.Diagnostic, converters *Converters) *lsproto.Diagnostic {
36+
func toLSPDiagnostics(converters *Converters, diagnostics ...[]*ast.Diagnostic) []*lsproto.Diagnostic {
37+
size := 0
38+
for _, diagSlice := range diagnostics {
39+
size += len(diagSlice)
40+
}
41+
lspDiagnostics := make([]*lsproto.Diagnostic, 0, size)
42+
for _, diagSlice := range diagnostics {
43+
for _, diag := range diagSlice {
44+
lspDiagnostics = append(lspDiagnostics, toLSPDiagnostic(converters, diag))
45+
}
46+
}
47+
return lspDiagnostics
48+
}
49+
50+
func toLSPDiagnostic(converters *Converters, diagnostic *ast.Diagnostic) *lsproto.Diagnostic {
4451
var severity lsproto.DiagnosticSeverity
4552
switch diagnostic.Category() {
4653
case diagnostics.CategorySuggestion:

0 commit comments

Comments
 (0)