Skip to content

Commit 4899175

Browse files
Fix import LSP document link to not include import keyword (#4308)
1 parent 15d712b commit 4899175

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Add `buf registry policy {commit,create,delete,info,label,settings}` commands to manage BSR policies.
66
- Add LSP deprecate code action to add the deprecated option on types and symbols.
7+
- Fix import LSP document link to not include `import` keyword
78

89
## [v1.64.0] - 2026-01-19
910

private/buf/buflsp/document_link_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,33 @@ func TestDocumentLink(t *testing.T) {
3939
protoFile: "testdata/document_link/main.proto",
4040
expectedLinks: []expectedLink{
4141
{
42-
line: 4, // import "types.proto" on line 5 (0-indexed line 4)
42+
line: 4, // import "types.proto" on line 5 (0-indexed line 4)
43+
startChar: 7, // Start of "types.proto" (including opening quote)
44+
endChar: 20, // End of "types.proto" (after closing quote)
4345
description: "local import to types.proto",
4446
targetType: linkTargetTypeLocal,
4547
localPath: "testdata/document_link/types.proto",
4648
},
4749
{
4850
line: 7, // https://example.com/docs on line 8
51+
startChar: 7,
52+
endChar: 31,
4953
description: "comment URL 1",
5054
targetType: linkTargetTypeURL,
5155
targetURL: "https://example.com/docs",
5256
},
5357
{
5458
line: 8, // https://github.com/example/repo on line 9
59+
startChar: 14,
60+
endChar: 45,
5561
description: "comment URL 2",
5662
targetType: linkTargetTypeURL,
5763
targetURL: "https://github.com/example/repo",
5864
},
5965
{
6066
line: 11, // https://example.com/status on line 12
67+
startChar: 43,
68+
endChar: 69,
6169
description: "comment URL 3",
6270
targetType: linkTargetTypeURL,
6371
targetURL: "https://example.com/status",
@@ -69,13 +77,17 @@ func TestDocumentLink(t *testing.T) {
6977
protoFile: "testdata/document_link/wkt.proto",
7078
expectedLinks: []expectedLink{
7179
{
72-
line: 4, // import "google/protobuf/timestamp.proto" on line 5
80+
line: 4, // import "google/protobuf/timestamp.proto" on line 5
81+
startChar: 7, // Start of "google/protobuf/timestamp.proto" (including opening quote)
82+
endChar: 40, // End of "google/protobuf/timestamp.proto" (after closing quote)
7383
description: "WKT Timestamp import",
7484
targetType: linkTargetTypeURL,
7585
targetURL: "https://buf.build/protocolbuffers/wellknowntypes/file/main:google/protobuf/timestamp.proto",
7686
},
7787
{
78-
line: 5, // import "google/protobuf/duration.proto" on line 6
88+
line: 5, // import "google/protobuf/duration.proto" on line 6
89+
startChar: 7, // Start of "google/protobuf/duration.proto" (including opening quote)
90+
endChar: 39, // End of "google/protobuf/duration.proto" (after closing quote)
7991
description: "WKT Duration import",
8092
targetType: linkTargetTypeURL,
8193
targetURL: "https://buf.build/protocolbuffers/wellknowntypes/file/main:google/protobuf/duration.proto",
@@ -105,7 +117,10 @@ func TestDocumentLink(t *testing.T) {
105117

106118
for i, expected := range tt.expectedLinks {
107119
link := links[i]
108-
assert.Equal(t, expected.line, link.Range.Start.Line, "link %d (%s): wrong line", i, expected.description)
120+
assert.Equal(t, expected.line, link.Range.Start.Line, "link %d (%s): wrong start line", i, expected.description)
121+
assert.Equal(t, expected.startChar, link.Range.Start.Character, "link %d (%s): wrong start character", i, expected.description)
122+
assert.Equal(t, expected.line, link.Range.End.Line, "link %d (%s): wrong end line", i, expected.description)
123+
assert.Equal(t, expected.endChar, link.Range.End.Character, "link %d (%s): wrong end character", i, expected.description)
109124

110125
switch expected.targetType {
111126
case linkTargetTypeLocal:
@@ -133,6 +148,8 @@ const (
133148

134149
type expectedLink struct {
135150
line uint32
151+
startChar uint32 // expected starting character position
152+
endChar uint32 // expected ending character position
136153
description string
137154
targetType linkTargetType
138155
localPath string // used when targetType is linkTargetTypeLocal

private/buf/buflsp/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ func getKindForMapType(typeAST ast.TypeAny, mapField ir.Member, isKey bool) (kin
851851
func (f *file) importToSymbol(imp ir.Import) *symbol {
852852
return &symbol{
853853
file: f,
854-
span: imp.Decl.Span(),
854+
span: imp.Decl.ImportPath().Span(),
855855
kind: &imported{
856856
file: f.workspace.PathToFile()[imp.File.Path()],
857857
},

0 commit comments

Comments
 (0)