Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions private/buf/buflsp/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ func TestHover(t *testing.T) {
character: 3, // On "map" keyword
expectedContains: "language-spec#maps",
},
{
name: "hover_on_message_after_import_with_trailing_comment",
protoFile: "testdata/hover/unused_import.proto",
line: 4, // Line with "message TestTopLevel {"
character: 8, // On "TestTopLevel"
expectNoHover: true,
},
{
name: "hover_on_message_after_import_with_trailing_comment_no_blank_line",
protoFile: "testdata/hover/unused_import_no_blank_line.proto",
line: 3, // Line with "message TestTopLevel {}"
character: 8, // On "TestTopLevel"
expectNoHover: true,
},
}

for _, tt := range tests {
Expand Down
42 changes: 32 additions & 10 deletions private/buf/buflsp/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,22 +555,28 @@ func (s *symbol) getDocsFromComments() string {
// traversing backwards for leading comments only.
tok, _ := def.Context().Stream().Around(def.Span().Start)
cursor := token.NewCursorAt(tok)
var seenNewline bool
// Count consecutive newlines. If we accumulate 2+ without encountering a comment,
// there's a blank line separating the comments from the symbol.
newlinesSeen := 0
if tok.Kind() == token.Space {
newlinesSeen = strings.Count(tok.Text(), "\n")
if newlinesSeen >= 2 {
return ""
}
}
for {
t := cursor.PrevSkippable()
if t.Kind() == token.Comment {
text := commentToMarkdown(t.Text())
if seenNewline {
text += "\n"
if isTrailingComment(t) {
break
}
seenNewline = false
text := commentToMarkdown(t.Text()) + "\n"
newlinesSeen = 0
comments = append(comments, text)
} else if t.Kind() == token.Space {
if strings.Contains(t.Text(), "\n") {
if seenNewline {
break
}
seenNewline = true
newlinesSeen += strings.Count(t.Text(), "\n")
if newlinesSeen >= 2 {
break
}
} else {
break
Expand Down Expand Up @@ -795,3 +801,19 @@ func plural(i int) string {
}
return "s"
}

// isTrailingComment returns true if the comment has code on the same line before it.
func isTrailingComment(t token.Token) bool {
if t.Kind() != token.Comment {
return false
}
for c := token.NewCursorAt(t); ; {
p := c.PrevSkippable()
if p.IsZero() || strings.Contains(p.Text(), "\n") {
return false
}
if p.Kind() != token.Space {
return true
}
}
}
6 changes: 6 additions & 0 deletions private/buf/buflsp/testdata/hover/unused_import.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

import "google/protobuf/cpp_features.proto"; // unused import

message TestTopLevel {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also handle:

import "google/protobuf/cpp_features.proto"; // unused import
message TestTopLevel {}

I think we need the comment handling to be aware that it is on it's own newline.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call. let me make sure we handle that.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
syntax = "proto3";

import "google/protobuf/cpp_features.proto"; // unused import
message TestTopLevel {}