Skip to content

Commit 06bea71

Browse files
✨ Enhance docstring highlighting: multi-word, complex types, NumPy params, Sphinx rtype
1 parent 22ea153 commit 06bea71

File tree

9 files changed

+75
-38
lines changed

9 files changed

+75
-38
lines changed

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"type": "extensionHost",
1212
"request": "launch",
1313
"args": [
14+
"--disable-extension=rodolphebarbanneau.python-docstring-highlighter",
1415
"--extensionDevelopmentPath=${workspaceFolder}",
1516
"--folder-uri=${workspaceFolder}/tests",
1617
"${workspaceFolder}/tests/google.py",

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@ This extension is designed to **highlight docstrings** in **Python code**, makin
1111

1212
![Demo](https://raw.githubusercontent.com/rodolphebarbanneau/python-docstring-highlighter/main/assets/docstring.gif)
1313

14+
> **Note**: The highlighting appearance in your editor may differ from this demo. Your personal color theme and the latest extension updates can affect the visual result. This demo is intended to showcase the general functionality rather than an exact representation.
15+
1416
> **Technical Note**: The highlighting uses the VScode TextMate grammar injection feature, which means that it is compatible with any color theme and should not lead to performance issues (i.e. no custom scripts is executed). The extension uses a custom grammar to match the docstring patterns and inject the appropriate scopes into the code editor.
1517
16-
> **Technical Note**: TextMate grammar injection can only match one-ligne regular expressions consistently. This means that the extension will not be able to match multi-line patterns in the docstring. This is a limitation of the TextMate grammar injection feature and cannot be worked around.
18+
> **Technical Note**: TextMate grammar injection can only match one-line regular expressions consistently. This means that the extension will not be able to match multi-line patterns in the docstring. This is a limitation of the TextMate grammar injection feature and cannot be worked around.
1719
1820
## Requirements
1921

2022
This extension is **only compatible** with the standard [VSCode Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python). It is not compatible with other language server like _Python for VSCode (deprecated)_.
2123

2224
## Extension Settings
2325

24-
This extensions does not provide specific settings. It is designed to work out of the box with the default color theme. However, you can customize the color theme by overriding the default color theme in your `settings.json` file (see the [Customization](#customization) section).
26+
This extension does not provide specific settings. It is designed to work out of the box with the default color theme. However, you can customize the color theme by overriding the default color theme in your `settings.json` file (see the [Customization](#customization) section).
2527

2628
### Available Scopes
2729

@@ -31,7 +33,7 @@ The following list shows the available scopes with their available sub-scopes sp
3133

3234
#### `docstring.heading`
3335

34-
Section headings in the docstring (e.g. `Args`, `Returns`, or `Raises`)
36+
Section headings in the docstring (e.g. `Args`, `Returns`, or `Raises`).
3537

3638
- `.python`
3739
- `.begin.python`
@@ -58,7 +60,7 @@ Identifiers in the docstring (e.g. `:meth:`, or `:attr:`).
5860

5961
#### `docstring.literal`
6062

61-
Inline literals in the docstring (e.g. ``` ``"Sucssefully executed"`` ```).
63+
Inline literals in the docstring (e.g. ``` ``"Successful Error"`` ```). 😭
6264

6365
- `.python`
6466
- `.begin.python`
@@ -103,9 +105,9 @@ You can customize the color theme by overriding the default color theme in your
103105
"scope": "docstring.heading.placeholder",
104106
"settings": {
105107
"fontStyle": "bold underline",
106-
"foreground": "#569cd6",
108+
"foreground": "#569cd6"
107109
}
108-
},
110+
}
109111
]
110112
}
111113
```
@@ -114,6 +116,13 @@ You can customize the color theme by overriding the default color theme in your
114116
115117
## Release Notes
116118

119+
### 0.2.4
120+
121+
- Added colorization for multi-word capitalized sections (e.g., `See Also`)
122+
- Fixed highlighting for complex type annotations (e.g., `Sequence[str]`)
123+
- Improved support for NumPy-style multiple parameters (e.g., `param1, param2 : int`)
124+
- Fixed Sphinx-style return type highlighting (e.g., `:rtype: int`)
125+
117126
### 0.2.3
118127

119128
- Updated base heading pattern end token to match end of line

syntaxes/base.tmLanguage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"name": "punctuation.definition.tag.end.docstring.python, docstring.heading.end.python"
1818
}
1919
},
20-
"match": "(?:^\\s*)()(_*[A-Z]\\w*)(:?$)",
20+
"match": "(?:^\\s*)()(_*(?:[A-Z]\\w*(?:\\s+(?!$))?)+)(:?$)",
2121
"name": "docstring.heading.python"
2222
},
2323
{

syntaxes/google.tmLanguage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"name": "punctuation.definition.tag.end.docstring.python, docstring.variable.end.google.python"
1818
}
1919
},
20-
"match": "(?:^\\s*)((?:-\\s*)?)((?:\\*\\*|\\*)?[a-zA-Z_]\\w*)((?:\\s\\(.*\\))?:)(?=\\s)",
20+
"match": "(?:^\\s*)((?:-\\s*)?)((?:\\*\\*|\\*)?[a-zA-Z_]\\w*(?:\\[.*\\])?)((?:\\s\\(.*\\))?:)(?=\\s)",
2121
"name": "docstring.variable.google.python"
2222
},
2323
{

syntaxes/numpy.tmLanguage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"name": "punctuation.definition.tag.end.docstring.python, docstring.variable.end.numpy.python"
1818
}
1919
},
20-
"match": "(?:^\\s*)((?:-\\s*)?)((?:\\*\\*|\\*)?[a-zA-Z_]\\w*)((?:\\s:\\s.*)?)(?:$)",
20+
"match": "(?:^\\s*)((?:-\\s*)?)((?:(?:\\*\\*|\\*)?[a-zA-Z_]\\w*(?:\\[.*\\])?(?:\\s*,\\s*(?=[a-zA-Z_]))?)+)((?:\\s+:\\s+.*)?)(?:$)",
2121
"name": "docstring.variable.numpy.python"
2222
},
2323
{

syntaxes/sphinx.tmLanguage.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"name": "punctuation.definition.tag.end.docstring.python, docstring.variable.end.sphinx.python"
2424
}
2525
},
26-
"match": "(?:^\\s*)(:)(\\w+)(?:(\\s|(?:.*\\s))?((?:\\*\\*|\\*|_)?\\w*)?)((?:=[^:]+)?:)(?=\\s)",
26+
"match": "(?:^\\s*)(:)(\\w+)(?:(\\s|(?:.*\\s))?((?:\\*\\*|\\*|_)?\\w*(?:\\[.*\\])?)?)((?:=[^:]+)?:(?:(?:(?<=:rtype:)\\s.*)|(?=\\s)))",
2727
"name": "docstring.variable.sphinx.python"
2828
},
2929
{

tests/google.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@
2424
one convention to document module level variables and be consistent
2525
with it.
2626
27+
See Also:
28+
`Extension Documentation`_ for more details on the extension.
29+
2730
.. todo::
2831
* For TODOs directive module
2932
* You have to also use ``sphinx.ext.todo`` extension;
3033
3134
.. _Google Python Style Guide:
3235
https://google.github.io/styleguide/pyguide.html
3336
https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
37+
38+
.. _Extension Documentation:
39+
https://marketplace.visualstudio.com/items?itemName=rodolphebarbanneau.python-docstring-highlighter
3440
"""
3541

3642
module_level_variable1 = 12345
@@ -43,7 +49,7 @@
4349
"""
4450

4551

46-
def function_with_types_in_docstring(param1, param2):
52+
def function_with_types_in_docstring(param1, param2, param3):
4753
"""Example function with types documented in the docstring.
4854
4955
`PEP 484`_ type annotations are supported. If attribute, parameter, and
@@ -52,7 +58,8 @@ def function_with_types_in_docstring(param1, param2):
5258
5359
Args:
5460
param1 (int): The first parameter.
55-
param2 (str): The second parameter.
61+
param2 (int): The second parameter.
62+
param3 (str): The third parameter.
5663
5764
Returns:
5865
bool: The return value. True for success, False otherwise.
@@ -63,12 +70,15 @@ def function_with_types_in_docstring(param1, param2):
6370
pass
6471

6572

66-
def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
73+
def function_with_pep484_type_annotations(
74+
param1: int, param2: int, param3: str
75+
) -> bool:
6776
"""Example function with PEP 484 type annotations.
6877
6978
Args:
7079
param1: The first parameter.
7180
param2: The second parameter.
81+
param3: The third parameter.
7282
7383
Returns:
7484
The return value. True for success, False otherwise.
@@ -248,7 +258,7 @@ def example_method(self, param1, param2):
248258
param2: The second parameter.
249259
250260
Returns:
251-
True if successful, False otherwise.
261+
Sequence[str]: A sequence of strings.
252262
"""
253263
return True
254264

tests/numpy.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
Section breaks are created with two blank lines. Section breaks are also
1717
implicitly created anytime a new section starts. Section bodies *may* be
18-
indented:
18+
indented.
1919
2020
Notes
2121
-----
@@ -35,6 +35,9 @@
3535
Either form is acceptable, but the two should not be mixed. Choose one
3636
convention to document module level variables and be consistent with it.
3737
38+
See Also
39+
--------
40+
`Extension Documentation`_ for more details on the extension.
3841
3942
.. todo::
4043
* For TODOs directive module
@@ -43,6 +46,9 @@
4346
.. _NumPy Python Style Guide:
4447
https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
4548
https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_numpy.html
49+
50+
.. _Extension Documentation:
51+
https://marketplace.visualstudio.com/items?itemName=rodolphebarbanneau.python-docstring-highlighter
4652
"""
4753

4854
module_level_variable1 = 12345
@@ -55,7 +61,7 @@
5561
"""
5662

5763

58-
def function_with_types_in_docstring(param1, param2):
64+
def function_with_types_in_docstring(param1, param2, param3):
5965
"""Example function with types documented in the docstring.
6066
6167
`PEP 484`_ type annotations are supported. If attribute, parameter, and
@@ -64,10 +70,10 @@ def function_with_types_in_docstring(param1, param2):
6470
6571
Parameters
6672
----------
67-
param1 : int
68-
The first parameter.
69-
param2 : str
70-
The second parameter.
73+
param1, param2 : int
74+
The first and second parameters.
75+
param3 : str
76+
The third parameter.
7177
7278
Returns
7379
-------
@@ -80,15 +86,17 @@ def function_with_types_in_docstring(param1, param2):
8086
pass
8187

8288

83-
def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
89+
def function_with_pep484_type_annotations(
90+
param1: int, param2: int, param3: str
91+
) -> bool:
8492
"""Example function with PEP 484 type annotations.
8593
8694
Parameters
8795
----------
88-
param1
89-
The first parameter.
90-
param2
91-
The second parameter.
96+
param1, param2
97+
The first and second parameters.
98+
param3
99+
The third parameter.
92100
93101
Returns
94102
-------
@@ -310,8 +318,8 @@ def example_method(self, param1, param2):
310318
311319
Returns
312320
-------
313-
bool
314-
True if successful, False otherwise.
321+
Sequence[str]
322+
A sequence of strings.
315323
"""
316324
return True
317325

tests/sphinx.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
The :attr:`module_level_variable1` module level variables must be documented
1919
in an inline docstring immediately following the variable.
2020
21+
See Also:
22+
`Extension Documentation`_ for more details on the extension.
23+
2124
.. todo::
2225
* For TODOs directive module
2326
* You have to also use ``sphinx.ext.todo`` extension
2427
2528
.. _Sphinx Python Style Guide:
2629
https://sphinx-rtd-tutorial.readthedocs.io/en/latest/docstrings.html
30+
31+
.. _Extension Documentation:
32+
https://marketplace.visualstudio.com/items?itemName=rodolphebarbanneau.python-docstring-highlighter
2733
"""
2834

2935
module_level_variable1 = 12345
@@ -36,31 +42,34 @@
3642
"""
3743

3844

39-
def function_with_types_in_docstring(param1, param2):
45+
def function_with_types_in_docstring(param1, param2, param3):
4046
"""Example function with types documented in the docstring.
4147
4248
`PEP 484`_ type annotations are supported. If attribute, parameter, and
4349
return types are annotated according to `PEP 484`_, they do not need to be
4450
included in the docstring:
4551
4652
:param int param1: The first parameter.
47-
:param str param2: The second parameter.
48-
53+
:param int param2: The second parameter.
54+
:param str param3: The third parameter.
55+
:return: The return value. True for success, False otherwise.
4956
:rtype: bool
50-
:returns: The return value. True for success, False otherwise.
5157
5258
.. _PEP 484:
5359
https://www.python.org/dev/peps/pep-0484/
5460
"""
5561
pass
5662

5763

58-
def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
64+
def function_with_pep484_type_annotations(
65+
param1: int, param2: int, param3: str
66+
) -> bool:
5967
"""Example function with PEP 484 type annotations.
6068
6169
:param param1: The first parameter.
6270
:param param2: The second parameter.
63-
:returns: The return value. True for success, False otherwise.
71+
:param param3: The third parameter.
72+
:return: The return value. True for success, False otherwise.
6473
"""
6574
return True
6675

@@ -92,15 +101,15 @@ def module_level_function(param1, param2=None, *args, **kwargs):
92101
:param kwargs: Miscellaneous.
93102
94103
:rtype: bool
95-
:returns: True if successful, False otherwise.
104+
:return: True if successful, False otherwise.
96105
97106
The return type is optional and may be specified using the ``:rtype:``
98107
directive.
99108
100-
The ``:returns:`` description may span multiple lines and paragraphs.
109+
The ``:return:`` description may span multiple lines and paragraphs.
101110
Following lines should be indented to match the first line.
102111
103-
The ``:returns:`` description supports any reStructuredText formatting,
112+
The ``:return:`` description supports any reStructuredText formatting,
104113
including literal blocks::
105114
106115
{
@@ -218,8 +227,8 @@ def example_method(self, param1, param2):
218227
219228
:param str param1: The first parameter.
220229
:param int param2: The second parameter.
221-
:returns: True if successful, False otherwise.
222-
:rtype: bool
230+
:return: A sequence of strings.
231+
:rtype: Sequence[str]
223232
224233
.. note::
225234
Do not include the `self` parameter in the ``:param:``directives.

0 commit comments

Comments
 (0)