Files
kjol/tools/tsgo/internal/ls/diagnostics.go
2026-07-09 16:50:43 -04:00

59 lines
2.1 KiB
Go

package ls
import (
"context"
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/ls/lsconv"
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
)
// getAllDiagnostics collects all diagnostics for a file: syntactic, semantic,
// suggestion, and (when declarations are emitted) declaration diagnostics.
func getAllDiagnostics(ctx context.Context, program *compiler.Program, file *ast.SourceFile) []*ast.Diagnostic {
var diags []*ast.Diagnostic
diags = append(diags, program.GetSyntacticDiagnostics(ctx, file)...)
diags = append(diags, program.GetSemanticDiagnostics(ctx, file)...)
diags = append(diags, program.GetSuggestionDiagnostics(ctx, file)...)
if program.Options().GetEmitDeclarations() {
diags = append(diags, program.GetDeclarationDiagnostics(ctx, file)...)
}
return diags
}
func (l *LanguageService) ProvideDiagnostics(ctx context.Context, uri lsproto.DocumentUri) (lsproto.DocumentDiagnosticResponse, error) {
program, file := l.getProgramAndFile(uri)
if l.UserPreferences().EnableValidation.IsFalse() {
diagnostics := []*lsproto.Diagnostic{}
return lsproto.RelatedFullDocumentDiagnosticReportOrUnchangedDocumentDiagnosticReport{
FullDocumentDiagnosticReport: &lsproto.RelatedFullDocumentDiagnosticReport{
Items: diagnostics,
},
}, nil
}
diagnostics := getAllDiagnostics(ctx, program, file)
return lsproto.RelatedFullDocumentDiagnosticReportOrUnchangedDocumentDiagnosticReport{
FullDocumentDiagnosticReport: &lsproto.RelatedFullDocumentDiagnosticReport{
Items: l.toLSPDiagnostics(ctx, diagnostics),
},
}, nil
}
func (l *LanguageService) toLSPDiagnostics(ctx context.Context, 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, lsconv.DiagnosticToLSPPull(ctx, l.converters, diag, l.UserPreferences().ReportStyleChecksAsWarnings.IsTrue()))
}
}
return lspDiagnostics
}