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

Documentation comments for both Python and Rust #11993

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

Conversation

probablySophie
Copy link

Someone asked in the Matrix chat yesterday about documentation comments in Python🐍. And I accidentally learned how tree-sitter syntax highlighting works to get doc comments working.
While I was there I figured I may as well do it for Rust🦀 too!


Rust documentation comments are

/// this is a documentation comment

and have the tree-sitter scopes:
["source_file", "impl_item", "declaration_list", "line_comment", "doc_comment"]

So adding a search for line_comments that contain doc_comments lets us isolate them and then themes can target @comment.documentation


Python documentation comments are

"""
   This is a documentation comment (and also a valid string)
"""

Tree-Sitter scopes:
["module", "expression_statement", "string", "string_content"]

Because the doc comments are also valid strings the suggested code looks for expression_statements that contain strings and that start with """.

Regular """ strings aren't affected because they're assignment > string instead of expression_statement > string


Thank you! 😃

@@ -111,6 +111,9 @@

(integer) @constant.numeric.integer
(float) @constant.numeric.float
(expression_statement (string) @content
(#match? @content "^\"\"\"")
Copy link
Member

Choose a reason for hiding this comment

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

@the-mikedavis wow, I guess we don't need grammar changes to detect this?

Copy link
Member

Choose a reason for hiding this comment

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

It looks like python exposes a (string_start) node under (string) which contains these markers anyways, but it is handy that we wouldn't necessarily need the python grammar to do that in order to match this. (@probablySophie you can use :tree-sitter-subtree when selecting a syntax node to see how the nodes are laid out - this is usually more helpful than :tree-sitter-scopes which only shows the node names.)

I think we should follow nvim-treesitter's queries for this instead though since they're more specific and don't need #match? or #eq? at all: https://github.com/nvim-treesitter/nvim-treesitter/blob/dfd3207ed43a0c572c13acc02bf9d7dc270f4030/queries/python/highlights.scm#L201-L222

@the-mikedavis the-mikedavis added the A-language-support Area: Support for programming/text languages label Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-language-support Area: Support for programming/text languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants