diff --git a/CHANGELOG.md b/CHANGELOG.md index a73cbe8075..45c2221550 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [Unreleased] +- Fix LSP go-to-definition, references, hover, and rename for file options. - Fix an LSP crash when organizing imports in a workspace with a file that fails to build. - Fix the `PROTOVALIDATE` lint rule to check constant arguments to `duration`, `timestamp`, and `matches` in CEL expressions, such as `duration('1d')` or an invalid regex, which otherwise only diff --git a/private/buf/buflsp/definition_test.go b/private/buf/buflsp/definition_test.go index 0421d7b8c5..c95564c398 100644 --- a/private/buf/buflsp/definition_test.go +++ b/private/buf/buflsp/definition_test.go @@ -208,6 +208,31 @@ func TestDefinition(t *testing.T) { } } +func TestDefinitionFileOption(t *testing.T) { + t.Parallel() + + ctx := t.Context() + + testProtoPath, err := filepath.Abs("testdata/definition/file_options.proto") + require.NoError(t, err) + optionsProtoPath, err := filepath.Abs("testdata/definition/options.proto") + require.NoError(t, err) + + clientJSONConn, testURI := setupLSPServer(t, testProtoPath) + + var locations []protocol.Location + _, err = clientJSONConn.Call(ctx, protocol.MethodTextDocumentDefinition, protocol.DefinitionParams{ + TextDocumentPositionParams: protocol.TextDocumentPositionParams{ + TextDocument: protocol.TextDocumentIdentifier{URI: testURI}, + Position: protocol.Position{Line: 6, Character: 22}, + }, + }, &locations) + require.NoError(t, err) + require.Len(t, locations, 1) + assert.Equal(t, buflsp.FilePathToURI(optionsProtoPath), locations[0].URI) + assert.Equal(t, protocol.Position{Line: 7, Character: 9}, locations[0].Range.Start) +} + // TestDefinitionURLEncoding verifies that file paths with special characters // like '@' are properly URL-encoded in the URI responses. func TestDefinitionURLEncoding(t *testing.T) { diff --git a/private/buf/buflsp/file.go b/private/buf/buflsp/file.go index 3b6eb7b7ef..0fd58afad8 100644 --- a/private/buf/buflsp/file.go +++ b/private/buf/buflsp/file.go @@ -469,6 +469,7 @@ func (f *file) indexSymbols() ([]*symbol, []*symbol) { resolved = append(resolved, resolvedSyms...) unresolved = append(unresolved, unresolvedSyms...) } + unresolved = append(unresolved, f.messageToSymbols(f.ir.Options())...) return resolved, unresolved } diff --git a/private/buf/buflsp/references_test.go b/private/buf/buflsp/references_test.go index 7a1e3ec9a6..e76f26bcd0 100644 --- a/private/buf/buflsp/references_test.go +++ b/private/buf/buflsp/references_test.go @@ -276,6 +276,35 @@ func TestReferencesStableAcrossReindex(t *testing.T) { assert.Len(t, slices.Compact(slices.Clone(after)), len(after), "re-indexing introduced duplicate references") } +func TestReferencesFileOption(t *testing.T) { + t.Parallel() + + ctx := t.Context() + + fileOptionsProtoPath, err := filepath.Abs("testdata/definition/file_options.proto") + require.NoError(t, err) + optionsProtoPath, err := filepath.Abs("testdata/definition/options.proto") + require.NoError(t, err) + + clientJSONConn, fileOptionsURI := setupLSPServer(t, fileOptionsProtoPath) + optionsURI := buflsp.FilePathToURI(optionsProtoPath) + + locations := testRequestReferences(ctx, t, clientJSONConn, optionsURI, 7, 10, true) + + type refLocation struct { + uri uri.URI + line uint32 + } + var actual []refLocation + for _, location := range locations { + actual = append(actual, refLocation{uri: location.URI, line: location.Range.Start.Line}) + } + assert.ElementsMatch(t, []refLocation{ + {uri: optionsURI, line: 7}, + {uri: fileOptionsURI, line: 6}, + }, actual) +} + // testRequestReferences sends a textDocument/references request and returns the locations. func testRequestReferences( ctx context.Context, diff --git a/private/buf/buflsp/testdata/definition/file_options.proto b/private/buf/buflsp/testdata/definition/file_options.proto new file mode 100644 index 0000000000..a81718f1f7 --- /dev/null +++ b/private/buf/buflsp/testdata/definition/file_options.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +package definition.v1; + +import "options.proto"; + +option (definition.v1.file_tag) = "f"; diff --git a/private/buf/buflsp/testdata/definition/options.proto b/private/buf/buflsp/testdata/definition/options.proto new file mode 100644 index 0000000000..89539cad4e --- /dev/null +++ b/private/buf/buflsp/testdata/definition/options.proto @@ -0,0 +1,9 @@ +syntax = "proto3"; + +package definition.v1; + +import "google/protobuf/descriptor.proto"; + +extend google.protobuf.FileOptions { + string file_tag = 50001; +}