Skip to content

Commit 7b6a021

Browse files
Stop collecting floating comments in LSP hover (#4259)
1 parent f927608 commit 7b6a021

File tree

3 files changed

+66
-34
lines changed

3 files changed

+66
-34
lines changed

private/buf/buflsp/hover_test.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ func TestHover(t *testing.T) {
3434
clientJSONConn, testURI := setupLSPServer(t, testProtoPath)
3535

3636
tests := []struct {
37-
name string
38-
line uint32
39-
character uint32
40-
expectedContains string
41-
expectNoHover bool
37+
name string
38+
line uint32
39+
character uint32
40+
expectedContains string
41+
expectedNotContains string
42+
expectNoHover bool
4243
}{
4344
{
4445
name: "hover_on_user_message",
45-
line: 7, // Line with "message User {"
46-
character: 8, // On the word "User"
47-
expectedContains: "User represents a user in the system",
46+
line: 7, // Line with "message User {"
47+
character: 8, // On the word "User"
48+
expectedContains: "system.\nThis", // Ensure newline between comment lines
4849
},
4950
{
5051
name: "hover_on_id_field",
@@ -76,6 +77,16 @@ func TestHover(t *testing.T) {
7677
character: 6, // On "GetUser"
7778
expectedContains: "GetUser retrieves a user by their ID",
7879
},
80+
{
81+
name: "hover_on_deprecated_option",
82+
line: 37, // Line with "option deprecated = true;"
83+
character: 11, // On "deprecated"
84+
// We don't want the hover info to include the floating comment that is separated by newlines
85+
// from the comment above the option.
86+
// Ref: https://buf.build/protocolbuffers/wellknowntypes/file/main:google/protobuf/descriptor.proto#L946
87+
expectedNotContains: "Buffers.", // From last line of the previous floating comment.
88+
expectedContains: "Is this method deprecated?",
89+
},
7990
{
8091
name: "hover_on_status_type_reference",
8192
line: 15, // Line with "Status status = 3;"
@@ -84,7 +95,7 @@ func TestHover(t *testing.T) {
8495
},
8596
{
8697
name: "hover_on_user_type_reference",
87-
line: 45, // Line with "User user = 1;"
98+
line: 50, // Line with "User user = 1;"
8899
character: 2, // On "User" type
89100
expectedContains: "User represents a user in the system",
90101
},
@@ -145,10 +156,17 @@ func TestHover(t *testing.T) {
145156

146157
if tt.expectNoHover {
147158
assert.Nil(t, hover, "expected no hover information")
148-
} else if tt.expectedContains != "" {
149-
require.NotNil(t, hover, "expected hover to be non-nil")
150-
assert.Equal(t, protocol.Markdown, hover.Contents.Kind)
151-
assert.Contains(t, hover.Contents.Value, tt.expectedContains)
159+
} else {
160+
if tt.expectedContains != "" {
161+
require.NotNil(t, hover, "expected hover to be non-nil")
162+
assert.Equal(t, protocol.Markdown, hover.Contents.Kind)
163+
assert.Contains(t, hover.Contents.Value, tt.expectedContains)
164+
}
165+
if tt.expectedNotContains != "" {
166+
require.NotNil(t, hover, "expected hover to be non-nil")
167+
assert.Equal(t, protocol.Markdown, hover.Contents.Kind)
168+
assert.NotContains(t, hover.Contents.Value, tt.expectedNotContains)
169+
}
152170
}
153171
})
154172
}

private/buf/buflsp/symbol.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -453,32 +453,29 @@ func (s *symbol) getDocsFromComments() string {
453453

454454
var comments []string
455455
// We drop the other side of "Around" because we only care about the beginning -- we're
456-
// traversing backwards for leading comemnts only.
457-
_, start := def.Context().Stream().Around(def.Span().Start)
458-
cursor := token.NewCursorAt(start)
459-
t := cursor.PrevSkippable()
460-
var addNewline bool
461-
for !t.IsZero() {
462-
switch t.Kind() {
463-
case token.Comment:
464-
text := t.Text()
465-
if addNewline {
456+
// traversing backwards for leading comments only.
457+
tok, _ := def.Context().Stream().Around(def.Span().Start)
458+
cursor := token.NewCursorAt(tok)
459+
var seenNewline bool
460+
for {
461+
t := cursor.PrevSkippable()
462+
if t.Kind() == token.Comment {
463+
text := commentToMarkdown(t.Text())
464+
if seenNewline {
466465
text += "\n"
467-
addNewline = false
468466
}
469-
comments = append(comments, commentToMarkdown(text))
470-
case token.Space:
471-
// If the space token only contains spaces (e.g. code indentation), then we drop it.
472-
// If the space token ends in a new-line, we append it to the comment above for formatting.
473-
if strings.HasSuffix(t.Text(), "\n") {
474-
addNewline = true
467+
seenNewline = false
468+
comments = append(comments, text)
469+
} else if t.Kind() == token.Space {
470+
if strings.Contains(t.Text(), "\n") {
471+
if seenNewline {
472+
break
473+
}
474+
seenNewline = true
475475
}
476-
}
477-
prev := cursor.PeekPrevSkippable()
478-
if !prev.Kind().IsSkippable() {
476+
} else {
479477
break
480478
}
481-
t = cursor.PrevSkippable()
482479
}
483480
comments = lineUpComments(comments)
484481
// Reverse the list and return joined.

private/buf/buflsp/testdata/hover/test.proto

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ enum Status {
3232
service UserService {
3333
// GetUser retrieves a user by their ID.
3434
rpc GetUser(GetUserRequest) returns (GetUserResponse);
35+
36+
// DeleteUser deletes a user by their ID.
37+
rpc DeleteUser(DeleteUserRequest) returns (DeleteUserResponse) {
38+
option deprecated = true;
39+
}
3540
}
3641

3742
// GetUserRequest is the request message for GetUser.
@@ -45,3 +50,15 @@ message GetUserResponse {
4550
// The retrieved user.
4651
User user = 1;
4752
}
53+
54+
// DeleteUserRequest is the request message for DeleteUser.
55+
message DeleteUserRequest {
56+
// The ID of the user to delete.
57+
string user_id = 1;
58+
}
59+
60+
// DeleteUserResponse is the response message for DeleteUser.
61+
message DeleteUserResponse {
62+
// Whether the deletion was successful.
63+
bool success = 1;
64+
}

0 commit comments

Comments
 (0)