Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve lsp hover definition comment formatting #3411

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

hirasawayuki
Copy link

@hirasawayuki hirasawayuki commented Oct 20, 2024

This PR introduces a new formatComment function to enhance the formatting of comments displayed in hover definitions. The implementation handles both single-line and multi-line comments, ensuring proper formatting while maintaining Markdown compatibility.

Before

screenshot2

As shown above, hover definitions currently display unnecessary asterisks from the proto file's comment syntax, making the documentation harder to read.

After

screenshot1

The new implementation removes the redundant asterisks while preserving the comment's structure, resulting in cleaner and more readable documentation in hover definitions.

@hirasawayuki hirasawayuki changed the title feat: Improve comment formatting for hover definitions feat(lsp): Improve comment formatting for hover definitions Oct 20, 2024
@hirasawayuki hirasawayuki changed the title feat(lsp): Improve comment formatting for hover definitions Improve lsp hover definition comment formatting Oct 25, 2024
Copy link
Member

@mcy mcy left a comment

Choose a reason for hiding this comment

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

Sorry for the delay. This hasn't been very high on my priority list and I've only just had a chance to look at this.

// formatComment takes a raw comment string and formats it by removing comment
// delimiters and unnecessary whitespace. It handles both single-line (//) and
// multi-line (/* */) comments.
func formatComment(comment string) string {
Copy link
Member

Choose a reason for hiding this comment

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

I think a better name for this is to rename it to something like commentToMarkdown, since the important thing is that this is emitting a Markdown string.

Comment on lines 535 to 541
lines := strings.Split(strings.TrimSpace(comment), "\n")
for i, line := range lines {
line = strings.TrimSpace(line)
line = strings.TrimLeft(line, "*")
line = strings.TrimPrefix(line, " ")
lines[i] = line
}
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer if this only triggered if the comment starts with /** Otherwise, the following will be mangled:

/*

my things:
* foo
* bar
* baz

*/

Copy link
Member

Choose a reason for hiding this comment

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

I would also like to see a comment that explains that what this is processing is a Doxygen-style comment, with an inline example of what we expect the comment to look like.


if strings.HasPrefix(comment, "/*") && strings.HasSuffix(comment, "*/") {
comment = strings.Trim(comment, "/*")
// single-line
Copy link
Member

Choose a reason for hiding this comment

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

Comments should be complete sentences.

"github.com/stretchr/testify/assert"
)

func Test_formatComment(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Don't use underscores in test names. Just name it TestFormatComment or equivalent.

@@ -0,0 +1,58 @@
package buflsp
Copy link
Member

Choose a reason for hiding this comment

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

Missing license notice.

},
{
name: "Multi-line comment with mixed indentation",
input: "/*\n * First line\n * - Second line\n * - Third line\n */",
Copy link
Member

Choose a reason for hiding this comment

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

This test case is incorrect. You should not trim asterisks if the comment does not begin with /**. Alternatively, you could instead only trim asterisks if every line starts with one.

},
{
name: "Empty comment",
input: "/**/",
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test case for there being a space in this?

expected string
}{
{
name: "Single-line comment",
Copy link
Member

Choose a reason for hiding this comment

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

can you make the test names be all-lowercase, separated by dashes?

},
}

for _, tt := range tests {
Copy link
Member

Choose a reason for hiding this comment

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

tt -> test

Comment on lines 52 to 55
result := formatComment(tt.input)
if result != tt.expected {
assert.Equal(t, tt.input, result)
}
Copy link
Member

Choose a reason for hiding this comment

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

Rewrite this to

assert.Equal(t, text.expected, formatComment(test.input))

@hirasawayuki
Copy link
Author

@mcy
Thank you for your review! I've addressed your feedback and made the suggested changes.
Could you please review the updated code?

@hirasawayuki hirasawayuki requested a review from mcy October 30, 2024 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants