Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make LSP always publish diagnostics #3397

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions private/buf/buflsp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (f *file) Refresh(ctx context.Context) {
progress.Begin(ctx, "Indexing")

progress.Report(ctx, "Parsing AST", 1.0/6)
hasReport := f.RefreshAST(ctx)
f.RefreshAST(ctx)

progress.Report(ctx, "Indexing Imports", 2.0/6)
f.IndexImports(ctx)
Expand All @@ -179,15 +179,17 @@ func (f *file) Refresh(ctx context.Context) {

progress.Report(ctx, "Linking Descriptors", 4.0/6)
f.BuildImage(ctx)
hasReport = f.RunLints(ctx) || hasReport // Avoid short-circuit here.
f.RunLints(ctx)

progress.Report(ctx, "Indexing Symbols", 5.0/6)
f.IndexSymbols(ctx)

progress.Done(ctx)
if hasReport {
f.PublishDiagnostics(ctx)
}

// NOTE: Diagnostics are published unconditionally. This is necessary even
// if we have zero diagnostics, so that the client correctly ticks over from
// n > 0 diagnostics to 0 diagnostics.
f.PublishDiagnostics(ctx)
}

// RefreshAST reparses the file and generates diagnostics if necessary.
Expand Down Expand Up @@ -232,8 +234,11 @@ func (f *file) RefreshAST(ctx context.Context) bool {
func (f *file) PublishDiagnostics(ctx context.Context) {
defer slogext.DebugProfile(f.lsp.logger, slog.String("uri", string(f.uri)))()

// NOTE: We need to avoid sending a JSON null here, so we replace it with
// a non-nil empty slice when the diagnostics slice is nil.
diagnostics := f.diagnostics
if f.diagnostics == nil {
return
diagnostics = []protocol.Diagnostic{}
}

// Publish the diagnostics. This error is automatically logged by the LSP framework.
Expand All @@ -242,7 +247,7 @@ func (f *file) PublishDiagnostics(ctx context.Context) {
// NOTE: For some reason, Version is int32 in the document struct, but uint32 here.
// This seems like a bug in the LSP protocol package.
Version: uint32(f.version),
Diagnostics: f.diagnostics,
Diagnostics: diagnostics,
})
}

Expand Down