diff --git a/private/buf/buflsp/symbol.go b/private/buf/buflsp/symbol.go index 7474ed99a7..12b8af3d26 100644 --- a/private/buf/buflsp/symbol.go +++ b/private/buf/buflsp/symbol.go @@ -408,22 +408,27 @@ func (s *symbol) getDocsFromComments() string { _, start := def.Context().Stream().Around(def.Span().Start) cursor := token.NewCursorAt(start) t := cursor.PrevSkippable() + var addNewline bool for !t.IsZero() { switch t.Kind() { case token.Comment: - comments = append(comments, commentToMarkdown(t.Text())) + text := t.Text() + if addNewline { + text += "\n" + addNewline = false + } + comments = append(comments, commentToMarkdown(text)) + case token.Space: + // If the space token only contains spaces (e.g. code indentation), then we drop it. + // If the space token ends in a new-line, we append it to the comment above for formatting. + if strings.HasSuffix(t.Text(), "\n") { + addNewline = true + } } prev := cursor.PeekPrevSkippable() if !prev.Kind().IsSkippable() { break } - if prev.Kind() == token.Space { - // Check if the whitespace contains a newline. If so, then we break. This is to prevent - // picking up comments that are not contiguous to the declaration. - if strings.Contains(prev.Text(), "\n") { - break - } - } t = cursor.PrevSkippable() } comments = lineUpComments(comments)