aboutsummaryrefslogtreecommitdiffstats
path: root/tests/query/highlights/python
diff options
context:
space:
mode:
authorJakob Zahn <jakob.zahn@mailbox.org>2025-04-03 09:35:41 +0200
committerGitHub <noreply@github.com>2025-04-03 09:35:41 +0200
commit997288c55253e27f782d991099490f80205d65bf (patch)
treebe439bc924aa0413de5caa1a72ffad05a9daaf37 /tests/query/highlights/python
parentci(tests): run tests on v0.10.4 (diff)
downloadnvim-treesitter-997288c55253e27f782d991099490f80205d65bf.tar
nvim-treesitter-997288c55253e27f782d991099490f80205d65bf.tar.gz
nvim-treesitter-997288c55253e27f782d991099490f80205d65bf.tar.bz2
nvim-treesitter-997288c55253e27f782d991099490f80205d65bf.tar.lz
nvim-treesitter-997288c55253e27f782d991099490f80205d65bf.tar.xz
nvim-treesitter-997288c55253e27f782d991099490f80205d65bf.tar.zst
nvim-treesitter-997288c55253e27f782d991099490f80205d65bf.zip
fix(python): highlight all types of docstrings (#7788)
Diffstat (limited to 'tests/query/highlights/python')
-rw-r--r--tests/query/highlights/python/docstrings.py121
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/query/highlights/python/docstrings.py b/tests/query/highlights/python/docstrings.py
new file mode 100644
index 000000000..02c0a2cc6
--- /dev/null
+++ b/tests/query/highlights/python/docstrings.py
@@ -0,0 +1,121 @@
+# Docstrings according to PEP 257 (https://peps.python.org/pep-0257/)
+# <- @comment
+
+"""Module docstring assigned to `__doc__`..."""
+# <- @string.documentation
+"""
+... with an addtional docstring, not part of `__doc__`.
+"""
+# <- @string.documentation
+
+"""
+Some random docstring in the middle if nowhere...
+"""
+# <- @string.documentation
+"""
+... also with not one ...
+"""
+# <- @string.documentation
+"""
+... but two addtional docstrings.
+"""
+# <- @string.documentation
+
+oneline_string_assignment = "not detected as docstring"
+# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ @string
+# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ !@string.documentation
+"""Module attribute docstring."""
+# <- @string.documentation
+
+multiline_string_assignment = """
+ also not detected as docstring
+ """
+# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @string
+# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ !@string.documentation
+
+looks_like_implicit_string_concatenation = "abc"
+# ^^^^^ @string
+"def"
+# <- @string.documentation
+
+single_line_implicit_string_concatenation = "abc" "def"
+# ^^^^^ @string
+# ^^^^^ @string
+# ^^^^^ !@string.documentation
+
+multiline_implicit_string_concatenation = (
+ "not "
+ # <- @string
+ # <- !@string.documentation
+ "detected "
+ # <- @string
+ # <- !@string.documentation
+ "as docstring, "
+ # <- @string
+ # <- !@string.documentation
+ "either."
+ # <- @string
+ # <- !@string.documentation
+)
+
+
+class A:
+ """
+ Class docstring, assigned to `__doc__`.
+ """
+ # <- @string.documentation
+
+ """
+ Some random docstring again, ...
+ """
+ # <- @string.documentation
+ """
+ ... with an "additional" docstring. Again.
+ """
+ # <- @string.documentation
+
+ foo = "class attribute"
+ # ^^^^^^^^^^^^^^^^^ @string
+ # ^^^^^^^^^^^^^^^^^ !@string.documentation
+ """
+ Class attribute docstring, but an attribute
+ does not have a `__doc__` attribute itself.
+ """
+ # <- @string.documentation
+
+ bar: int
+ """Class attribute docstring, type annotation only."""
+ # <- @string.documentation
+
+ baz: str = "type annotated class attribute"
+ # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @string
+ # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ !@string.documentation
+ """Class attribute docstring, type annotation and assignment."""
+ # <- @string.documentation
+
+ def __init__(self):
+ """Method docstring."""
+ # <- @string.documentation
+
+ self.quux = "instance attribute"
+ # ^^^^^^^^^^^^^^^^^^^^ @string
+ # ^^^^^^^^^^^^^^^^^^^^ !@string.documentation
+ """Instance attribute docstring."""
+ # <- @string.documentation
+
+
+def f(x):
+ """Function docstring."""
+ # <- @string.documentation
+ """Addtional function docstring."""
+ # <- @string.documentation
+ return x**2
+
+
+f.a = 1
+"""Function attribute docstring."""
+# <- @string.documentation
+
+
+"Random docstring with single quotes - legal, but far off standard and confusing."
+# <- @string.documentation