Skip to content

Commit

Permalink
[Python] Add syntax support for sphinx-style doc comments (#3839)
Browse files Browse the repository at this point in the history
Sphinx (the most common Python documentation tool) uses `#:` comments to
signal documentation comments for its "autodoc" module.

There is no known overlap with other comment punctuation, so this should
be a safe addition.

`TM_COMMENT_START_2` is also added to be able to undo the comment using
`toggle_comment`, but unfortunately it does not work like this
currently because `Default/comment.py` does not prioritize the longest
prefix match of all available comment styles but we don't want this
style to be the default.

https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-autoattribute
  • Loading branch information
FichteFoll authored Sep 15, 2023
1 parent a5cde86 commit 2aeae5d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Python/Comments.tmPreferences
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
<key>value</key>
<string>:</string>
</dict>
<dict>
<!--
Cannot be undone using `toggle_comment` at the moment
because the earlier `TM_COMMENT_START` is tested first
and is a true prefix of this one.
-->
<key>name</key>
<string>TM_COMMENT_START_2</string>
<key>value</key>
<string>#: </string>
</dict>
</array>
</dict>
</dict>
Expand Down
10 changes: 10 additions & 0 deletions Python/Python.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ contexts:
###[ COMMENTS ]###############################################################

comments:
# Special Sphinx comment syntax to denote documentation
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#directive-autoattribute
- match: '#:'
scope: punctuation.definition.comment.python
push: comment-sphinxdoc-body
- match: \#
scope: punctuation.definition.comment.python
push:
Expand All @@ -375,6 +380,11 @@ contexts:
- match: \n
pop: 1

comment-sphinxdoc-body:
- meta_scope: comment.line.documentation.python
- match: \n
pop: 1

shebang:
- meta_include_prototype: false
- match: ^\#!
Expand Down
8 changes: 8 additions & 0 deletions Python/tests/syntax_test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.block.documentation
"""
#: This is a prefixed "doc comment", supported by sphinx
# <- comment.line.documentation.python punctuation.definition.comment.python
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.documentation.python
#^ punctuation.definition.comment.python
attribute = ... #: also supported on the same line
# ^^ comment.line.documentation.python punctuation.definition.comment.python
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.documentation.python
r'''This is a syntax test file.
# <- storage.type.string - comment
#^^^ comment.block.documentation.python punctuation.definition.comment.begin.python
Expand Down

0 comments on commit 2aeae5d

Please sign in to comment.