Skip to content

Highlight module and function docstrings as comments#17

Merged
robertoaloi merged 2 commits intoerlang-ls:mainfrom
KornelH:otp_27_docstring_comment
May 6, 2025
Merged

Highlight module and function docstrings as comments#17
robertoaloi merged 2 commits intoerlang-ls:mainfrom
KornelH:otp_27_docstring_comment

Conversation

@KornelH
Copy link

@KornelH KornelH commented Apr 23, 2025

Set the syntax highlight to module and function documentation, more precisely to the docstring content of -moduledoc and -doc attributes to comment.

This change distinguish documentation comments and source code, just as the old EDoc comments did, and as other languages do as well, for example Elixir.

For more info, please check PR #9.

An additional change also included in the branch of this PR, highlight io:fread and io:fwrite control sequences in verbatim strings for convenience.

KornelH added 2 commits April 25, 2025 09:19
Highlight `io:fread` and `io:fwrite` control sequences in verbatim
strings for convenience.
Set the syntax highlight to module and function documentation, more
precisely to the docstring content of `-moduledoc` and `-doc` attributes
to comment.

This change distinguish documentation comments and source code, just
as the old EDoc comments did, and as other languages do as well, for
example Elixir.
@KornelH KornelH force-pushed the otp_27_docstring_comment branch from 13d5421 to 6115175 Compare April 25, 2025 07:27
@robertoaloi
Copy link
Member

Thanks for this @KornelH , let me test this a bit and come back to you!

@robertoaloi
Copy link
Member

Hi @KornelH, I finally got a chance to try this locally. First, let me say that the change is very welcome, since big docstrings can indeed be confusing.

One thing I noticed is that the change is quite sensitive to formatting. For example, it doesn't work if there's a newline after a fun. Compare the two following snippets:

Screenshot 2025-05-06 at 08 20 13 Screenshot 2025-05-06 at 08 20 26

Also, are there any plans about the -doc metadata? Right now they look like generic terms. This last point does not block the PR, since things can be improved incrementally, I'm just curious about your thoughts.

Screenshot 2025-05-06 at 08 20 53

@KornelH
Copy link
Author

KornelH commented May 6, 2025

Hi @robertoaloi,

I start with the second note, I didn't plan to do anything with -doc metadata attributes. Usually, it's just one line if present at all, so I don't mind to have the regular syntax for that.

Regarding the first note, I know about this issue but it is not related to the current or earlier docstring changes. It's an old standing problem in function expression syntax highlighting that is sensitive for formatting and breaks the following attributes. Before OTP 27 it was usually noticeable on -define attributes:

Screenshot 2025-05-06 101726 Screenshot 2025-05-06 102214

@robertoaloi
Copy link
Member

Regarding the first note, I know about this issue but it is not related to the current or earlier docstring changes. It's an old standing problem in function expression syntax highlighting that is sensitive for formatting and breaks the following attributes. Before OTP 27 it was usually noticeable on -define attributes:

Ah, fair enough. I didn't notice that before. Thanks for pointing that out, let's create an issue for that.

I start with the second note, I didn't plan to do anything with -doc metadata attributes. Usually, it's just one line if present at all, so I don't mind to have the regular syntax for that.

That's all right, we can tweak this as a follow-up. Just for reference, in ELP we are converting the old @param Edoc tags into doc metadata, so they can be used for IDE features such as signature help. That can cause the -doc metadata to be non trivial. For example:

-doc #{params => #{"Foo" => "This is foo", "Bar" => "This is bar"}}.

@robertoaloi robertoaloi merged commit b6a7360 into erlang-ls:main May 6, 2025
1 check passed
@robertoaloi
Copy link
Member

Thanks again for contributing this! 🎸

@robertoaloi
Copy link
Member

Let's create an issue for that.

And there's already one: #1

@KornelH
Copy link
Author

KornelH commented May 7, 2025

Yesterday, I looked into what VSC thinks about tokens when the syntax highlight is broken and it is the implicit function expressions (fun m:f/a) that are actually broken. The line break after fun is the culprit that activates implicit function expression until the next / sign that could be the arity divider. Since in the example there is no / sign, the syntax highlight is broken in the rest of the file.

Difference: 42 vs 42/1
Screenshot 2025-05-07 105616 Screenshot 2025-05-07 105759

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