Skip to content

Commit 71011ec

Browse files
authored
Upgrade protocompile and use span to index referenceable symbols (#4149)
1 parent dae11ca commit 71011ec

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
buf.build/go/standard v0.1.0
1717
connectrpc.com/connect v1.19.1
1818
connectrpc.com/otelconnect v0.8.0
19-
github.com/bufbuild/protocompile v0.14.2-0.20251027134829-3998dcfe9ba2
19+
github.com/bufbuild/protocompile v0.14.2-0.20251105181640-1a16185708f1
2020
github.com/bufbuild/protoplugin v0.0.0-20250218205857-750e09ce93e1
2121
github.com/docker/docker v28.5.1+incompatible
2222
github.com/go-chi/chi/v5 v5.2.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYW
4040
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
4141
github.com/bmatcuk/doublestar/v4 v4.9.1 h1:X8jg9rRZmJd4yRy7ZeNDRnM+T3ZfHv15JiBJ/avrEXE=
4242
github.com/bmatcuk/doublestar/v4 v4.9.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
43-
github.com/bufbuild/protocompile v0.14.2-0.20251027134829-3998dcfe9ba2 h1:ZX/PzJPQycvHBhtgCuO5XHp/PFos6Yyr2rNgSxAYic4=
44-
github.com/bufbuild/protocompile v0.14.2-0.20251027134829-3998dcfe9ba2/go.mod h1:HKN246DRQwavs64sr2xYmSL+RFOFxmLti+WGCZ2jh9U=
43+
github.com/bufbuild/protocompile v0.14.2-0.20251105181640-1a16185708f1 h1:ZHKztgOoelyVqVeOnh6FUHeMlk5L9KoJcLSHfZXV22k=
44+
github.com/bufbuild/protocompile v0.14.2-0.20251105181640-1a16185708f1/go.mod h1:HKN246DRQwavs64sr2xYmSL+RFOFxmLti+WGCZ2jh9U=
4545
github.com/bufbuild/protoplugin v0.0.0-20250218205857-750e09ce93e1 h1:V1xulAoqLqVg44rY97xOR+mQpD2N+GzhMHVwJ030WEU=
4646
github.com/bufbuild/protoplugin v0.0.0-20250218205857-750e09ce93e1/go.mod h1:c5D8gWRIZ2HLWO3gXYTtUfw/hbJyD8xikv2ooPxnklQ=
4747
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=

private/buf/buflsp/completion.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/bufbuild/buf/private/pkg/normalpath"
2929
"github.com/bufbuild/protocompile/experimental/ast"
3030
"github.com/bufbuild/protocompile/experimental/ast/syntax"
31+
"github.com/bufbuild/protocompile/experimental/id"
3132
"github.com/bufbuild/protocompile/experimental/ir"
3233
"github.com/bufbuild/protocompile/experimental/report"
3334
"github.com/bufbuild/protocompile/experimental/seq"
@@ -44,7 +45,7 @@ func getCompletionItems(
4445
file *file,
4546
position protocol.Position,
4647
) []protocol.CompletionItem {
47-
if file.ir.AST().IsZero() {
48+
if file.ir.AST() == nil {
4849
file.lsp.logger.DebugContext(
4950
ctx,
5051
"no AST found for completion",
@@ -53,7 +54,9 @@ func getCompletionItems(
5354
return nil
5455
}
5556

56-
declPath := getDeclForPosition(file.ir.AST().DeclBody, position)
57+
// This grabs the contents of the file as the top-level [ast.DeclBody], see [ast.File].Decls()
58+
// for reference.
59+
declPath := getDeclForPosition(id.Wrap(file.ir.AST(), id.ID[ast.DeclBody](1)), position)
5760
if len(declPath) > 0 {
5861
decl := declPath[len(declPath)-1]
5962
file.lsp.logger.DebugContext(
@@ -727,7 +730,7 @@ func typeReferencesToCompletionItems(
727730
if _, ok := symbol.ir.Deprecated().AsBool(); ok {
728731
isDeprecated = true
729732
}
730-
symbolFile := symbol.ir.File().Path()
733+
symbolFile := symbol.ir.Context().Path()
731734
_, hasImport := currentImportPaths[symbolFile]
732735
var additionalTextEdits []protocol.TextEdit
733736
if !hasImport && symbolFile != current.ir.Path() {
@@ -832,10 +835,10 @@ func isTokenNewline(tok token.Token) bool {
832835

833836
// extractAroundOffset extracts the value around the offset by querying the token stream.
834837
func extractAroundOffset(file *file, offset int, isTokenBefore, isTokenAfter func(token.Token) bool) report.Span {
835-
if file.ir.AST().IsZero() {
838+
if file.ir.AST() == nil {
836839
return report.Span{}
837840
}
838-
stream := file.ir.AST().Context().Stream()
841+
stream := file.ir.AST().Stream()
839842
if stream == nil {
840843
return report.Span{}
841844
}

private/buf/buflsp/file.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ type file struct {
6060
workspace *workspace // May be nil.
6161
objectInfo storage.ObjectInfo // Info in the context of the workspace.
6262

63-
ir ir.File
64-
referenceableSymbols map[string]*symbol
63+
ir *ir.File
64+
referenceableSymbols map[report.Span]*symbol
6565
referenceSymbols []*symbol
6666
symbols []*symbol
6767
diagnostics []protocol.Diagnostic
@@ -215,7 +215,7 @@ func (f *file) RefreshIR(ctx context.Context) {
215215
slog.Int("version", int(f.version)),
216216
)
217217
f.diagnostics = nil
218-
f.ir = ir.File{}
218+
f.ir = nil
219219

220220
// Opener creates a cached view of all files in the workspace.
221221
pathToFiles := f.workspace.PathToFile()
@@ -228,7 +228,7 @@ func (f *file) RefreshIR(ctx context.Context) {
228228
opener := source.NewMap(openerMap)
229229

230230
session := new(ir.Session)
231-
queries := xslices.Map(files, func(file *file) incremental.Query[ir.File] {
231+
queries := xslices.Map(files, func(file *file) incremental.Query[*ir.File] {
232232
return queries.IR{
233233
Opener: opener,
234234
Path: file.objectInfo.Path(),
@@ -285,15 +285,15 @@ func (f *file) RefreshIR(ctx context.Context) {
285285
func (f *file) IndexSymbols(ctx context.Context) {
286286
defer xslog.DebugProfile(f.lsp.logger, slog.String("uri", string(f.uri)))()
287287
// We cannot index symbols without the IR, so we keep the symbols as-is.
288-
if f.ir.IsZero() {
288+
if f.ir == nil {
289289
return
290290
}
291291

292292
// Throw away all the old symbols and rebuild symbols unconditionally. This is because if
293293
// this file depends on a file that has since been modified, we may need to update references.
294294
f.symbols = nil
295295
f.referenceSymbols = nil
296-
f.referenceableSymbols = make(map[string]*symbol)
296+
f.referenceableSymbols = make(map[report.Span]*symbol)
297297

298298
// Process all imports as symbols
299299
for imp := range seq.Values(f.ir.Imports()) {
@@ -311,7 +311,7 @@ func (f *file) IndexSymbols(ctx context.Context) {
311311
if !ok {
312312
continue
313313
}
314-
f.referenceableSymbols[def.ast.Name().Canonicalized()] = sym
314+
f.referenceableSymbols[def.ast.Name().Span()] = sym
315315
}
316316

317317
// TODO: this could use a refactor, probably.
@@ -338,7 +338,7 @@ func (f *file) IndexSymbols(ctx context.Context) {
338338
}
339339
file = f
340340
}
341-
def, ok := file.referenceableSymbols[ref.def.Name().Canonicalized()]
341+
def, ok := file.referenceableSymbols[ref.def.Name().Span()]
342342
if !ok {
343343
// This could happen in the case where we are in the cache for example, and we do not
344344
// have access to a buildable workspace.
@@ -377,7 +377,7 @@ func (f *file) IndexSymbols(ctx context.Context) {
377377
if ref.def.Span().Path() != f.objectInfo.Path() {
378378
continue
379379
}
380-
def, ok := f.referenceableSymbols[ref.def.Name().Canonicalized()]
380+
def, ok := f.referenceableSymbols[ref.def.Name().Span()]
381381
if !ok {
382382
// This shouldn't happen, if a symbol is pointing at this file, all definitions
383383
// should be resolved, logging a warning
@@ -425,7 +425,7 @@ func (f *file) indexSymbols() ([]*symbol, []*symbol) {
425425
for i := range f.ir.Symbols().Len() {
426426
// We only index the symbols for this file.
427427
symbol := f.ir.Symbols().At(i)
428-
if symbol.File().Path() != f.objectInfo.Path() {
428+
if symbol.Context().Path() != f.objectInfo.Path() {
429429
continue
430430
}
431431
resolvedSyms, unresolvedSyms := f.irToSymbols(symbol)
@@ -831,7 +831,7 @@ type wktObjectInfo struct {
831831
// This operation requires [IndexSymbols].
832832
func (f *file) GetSymbols(query string) iter.Seq[protocol.SymbolInformation] {
833833
return func(yield func(protocol.SymbolInformation) bool) {
834-
if f.ir.IsZero() {
834+
if f.ir == nil {
835835
return
836836
}
837837
// Search through all symbols in this file.

0 commit comments

Comments
 (0)