Skip to content

Commit

Permalink
Make LSP always publish diagnostics (#3397)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcy authored Oct 11, 2024
1 parent 71e170e commit 1a7712f
Showing 1 changed file with 12 additions and 7 deletions.
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

0 comments on commit 1a7712f

Please sign in to comment.