Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

refactor(ingestion): read docstrings through per-language tables (#4076)

Raghav Chamadiya26757dbe9ea1
diff --git a/packages/core/src/repowise/core/ingestion/extractors/docstrings.py b/packages/core/src/repowise/core/ingestion/extractors/docstrings.py
index 842fbd95b..197d96e97 100644
--- a/packages/core/src/repowise/core/ingestion/extractors/docstrings.py
+++ b/packages/core/src/repowise/core/ingestion/extractors/docstrings.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import re
+from collections.abc import Callable
from tree_sitter import Node
@@ -66,6 +67,8 @@ def _elixir_attribute_doc(node: Node, src: str, names: tuple[str, ...]) -> str |
if child.type == "string":
return clean_string_literal(node_text(child, src)) or None
return None
+
+
def _fsharp_doc_text(xml_doc_nodes: list[Node], src: str) -> str | None:
"""Join a run of ``///`` lines and strip the XML the way C# does.
@@ -104,394 +107,399 @@ def _fsharp_preceding_doc(node: Node, src: str) -> list[Node]:
def extract_module_docstring(root: Node, src: str, lang: str) -> str | None:
"""Extract a module/file-level docstring or leading comment."""
- if lang == "python":
- for child in root.children:
- if child.type == "expression_statement":
- for sub in child.children:
- if sub.type == "string":
- return clean_string_literal(node_text(sub, src))
- break
- elif child.type not in (
- "comment",
- "newline",
- "import_statement",
- "import_from_statement",
- "future_import_statement",
- ):
- break
- elif lang in ("typescript", "javascript", "svelte", "vue", "astro"):
- # Look for leading /** ... */ comment
- for child in root.children:
- if child.type == "comment":
- text = node_text(child, src).strip()
- if text.startswith("/**"):
- return clean_jsdoc(text)
- elif child.type not in ("comment",):
- break
- elif lang == "go":
- # Package comment is a series of // lines before package_clause
- lines: list[str] = []
- for child in root.children:
- if child.type == "comment":
- lines.append(node_text(child, src).lstrip("/ ").strip())
- elif child.type == "package_clause":
- break
- return "\n".join(lines) if lines else None
- elif lang == "fsharp":
- # The file's own doc sits above (or just inside) the `module` /
- # `namespace` header, which is itself the first node of the file.
- head = root
- while head.named_child_count and head.named_children[0].type in (
- "named_module",
- "namespace",
+ reader = _MODULE_DOC.get(lang)
+ return reader(root, src) if reader is not None else None
+
+
+def extract_symbol_docstring(def_node: Node, src: str, lang: str) -> str | None:
+ """Extract the docstring from a symbol's body node."""
+ reader = _SYMBOL_DOC.get(lang)
+ return reader(def_node, src) if reader is not None else None
+
+
+def _leading_block_doc(
+ root: Node, src: str, comment_kinds: tuple[str, ...], skip_kinds: tuple[str, ...]
+) -> str | None:
+ """The first ``/** ... */`` among the file's leading comments.
+
+ Children of *skip_kinds* (imports, package headers) are stepped over; any
+ other node ends the search.
+ """
+ for child in root.children:
+ if child.type in comment_kinds:
+ text = node_text(child, src).strip()
+ if text.startswith("/**"):
+ return clean_jsdoc(text)
+ elif child.type not in skip_kinds:
+ break
+ return None
+
+
+def _python_module_doc(root: Node, src: str) -> str | None:
+ for child in root.children:
+ if child.type == "expression_statement":
+ for sub in child.children:
+ if sub.type == "string":
+ return clean_string_literal(node_text(sub, src))
+ break
+ elif child.type not in (
+ "comment",
+ "newline",
+ "import_statement",
+ "import_from_statement",
+ "future_import_statement",
):
- head = head.named_children[0]
- docs: list[Node] = []
- for child in head.children:
- if child.type == "xml_doc":
- docs.append(child)
- elif docs or child.type not in ("module", "namespace", "long_identifier"):
- break
- return _fsharp_doc_text(docs, src) if docs else None
-
- elif lang == "rust":
- # //! inner doc comments or /*! block inner doc comments at top
- inner_lines: list[str] = []
- for child in root.children:
- if child.type == "line_comment":
- text = node_text(child, src).strip()
- if text.startswith("//!"):
- inner_lines.append(text[3:].strip())
- continue
- elif child.type == "block_comment":
- text = node_text(child, src).strip()
- if text.startswith("/*!"):
- inner = text[3:]
- if inner.endswith("*/"):
- inner = inner[:-2]
- return inner.strip()
- else:
- break
- if inner_lines:
- return "\n".join(inner_lines)
- elif lang in ("cpp", "c", "objectivec"):
- # Doxygen: first /** ... */ block comment before any declaration
- for child in root.children:
- if child.type in ("comment", "block_comment"):
- text = node_text(child, src).strip()
- if text.startswith("/**"):
- return clean_jsdoc(text)
- elif child.type not in (
- "comment",
- "preproc_include",
- "preproc_ifdef",
- "preproc_ifndef",
- ):
- break
- elif lang == "kotlin":
- for child in root.children:
- if child.type in ("comment", "multiline_comment"):
- text = node_text(child, src).strip()
- if text.startswith("/**"):
- return clean_jsdoc(text)
- elif child.type not in ("comment", "package_header", "import"):
- break
- elif lang == "elixir":
- # @moduledoc sits inside the defmodule's do_block, not at file top.
- for child in root.children:
- if child.type != "call":
- continue
- block = next((c for c in child.named_children if c.type == "do_block"), None)
- if block is None:
- continue
- for statement in block.named_children:
- doc = _elixir_attribute_doc(statement, src, ("moduledoc",))
- if doc:
- return doc
break
+ return None
- elif lang == "ruby":
- lines: list[str] = []
- for child in root.children:
- if child.type == "comment":
- lines.append(node_text(child, src).lstrip("# ").strip())
- else:
- break
- return "\n".join(lines) if lines else None
-
- elif lang == "csharp":
- # Module-level XML doc: a run of `///` lines, optionally preceded by
- # a /** block. Either form may appear before any using directives or
- # the (file-scoped) namespace declaration.
- triple_slash_lines: list[str] = []
- for child in root.children:
- if child.type == "comment":
- text = node_text(child, src).strip()
- if text.startswith("/**"):
- return clean_jsdoc(text)
- if text.startswith("///"):
- triple_slash_lines.append(text.lstrip("/ ").strip())
- continue
- # Plain // comment — ignore but keep scanning for /// runs.
- continue
- elif child.type not in (
- "comment",
- "using_directive",
- "global_using_directive",
- "extern_alias_directive",
- ):
- break
- if triple_slash_lines:
- return _csharp_clean_xml_doc("\n".join(triple_slash_lines))
-
- elif lang == "swift":
- for child in root.children:
- if child.type == "comment":
- text = node_text(child, src).strip()
- if text.startswith("/**"):
- return clean_jsdoc(text)
- elif text.startswith("///"):
- return text.lstrip("/ ").strip()
- elif child.type not in ("comment", "import_declaration"):
- break
-
- elif lang == "scala":
- for child in root.children:
- if child.type == "comment":
- text = node_text(child, src).strip()
- if text.startswith("/**"):
- return clean_jsdoc(text)
- elif child.type not in ("comment", "import_declaration"):
- break
-
- elif lang == "php":
- for child in root.children:
- if child.type == "comment":
- text = node_text(child, src).strip()
- if text.startswith("/**"):
- return clean_jsdoc(text)
- elif child.type not in (
- "comment",
- "php_tag",
- "namespace_definition",
- "namespace_use_declaration",
- ):
- break
-
- elif lang == "java":
- for child in root.children:
- if child.type in ("comment", "block_comment", "line_comment"):
- text = node_text(child, src).strip()
- if text.startswith("/**"):
- return clean_jsdoc(text)
- elif child.type not in ("comment", "package_declaration", "import_declaration"):
- break
-
- elif lang == "luau":
- # --[[ block comment ]] or run of leading --- comments
- triple_dash_lines: list[str] = []
- for child in root.children:
- if child.type == "comment":
- text = node_text(child, src).strip()
- if text.startswith("--[["):
- inner = text[4:]
- if inner.endswith("]]"):
- inner = inner[:-2]
- return inner.strip()
- if text.startswith("---"):
- triple_dash_lines.append(text.lstrip("- ").strip())
- continue
+
+def _go_module_doc(root: Node, src: str) -> str | None:
+ """The package comment: the ``//`` lines before ``package_clause``."""
+ lines: list[str] = []
+ for child in root.children:
+ if child.type == "comment":
+ lines.append(node_text(child, src).lstrip("/ ").strip())
+ elif child.type == "package_clause":
+ break
+ return "\n".join(lines) if lines else None
+
+
+def _fsharp_module_doc(root: Node, src: str) -> str | None:
+ # The file's own doc sits above (or just inside) the `module` /
+ # `namespace` header, which is itself the first node of the file.
+ head = root
+ while head.named_child_count and head.named_children[0].type in (
+ "named_module",
+ "namespace",
+ ):
+ head = head.named_children[0]
+ docs: list[Node] = []
+ for child in head.children:
+ if child.type == "xml_doc":
+ docs.append(child)
+ elif docs or child.type not in ("module", "namespace", "long_identifier"):
+ break
+ return _fsharp_doc_text(docs, src) if docs else None
+
+
+def _rust_module_doc(root: Node, src: str) -> str | None:
+ """``//!`` inner doc lines, or a ``/*!`` inner block, at the top."""
+ inner_lines: list[str] = []
+ for child in root.children:
+ if child.type == "line_comment":
+ text = node_text(child, src).strip()
+ if text.startswith("//!"):
+ inner_lines.append(text[3:].strip())
continue
- else:
- break
- if triple_dash_lines:
- return "\n".join(triple_dash_lines)
+ elif child.type == "block_comment":
+ text = node_text(child, src).strip()
+ if text.startswith("/*!"):
+ inner = text[3:]
+ if inner.endswith("*/"):
+ inner = inner[:-2]
+ return inner.strip()
+ else:
+ break
+ return "\n".join(inner_lines) if inner_lines else None
+
+
+def _elixir_module_doc(root: Node, src: str) -> str | None:
+ # @moduledoc sits inside the defmodule's do_block, not at file top.
+ for child in root.children:
+ if child.type != "call":
+ continue
+ block = next((c for c in child.named_children if c.type == "do_block"), None)
+ if block is None:
+ continue
+ for statement in block.named_children:
+ doc = _elixir_attribute_doc(statement, src, ("moduledoc",))
+ if doc:
+ return doc
+ break
+ return None
+
+
+def _ruby_module_doc(root: Node, src: str) -> str | None:
+ lines: list[str] = []
+ for child in root.children:
+ if child.type == "comment":
+ lines.append(node_text(child, src).lstrip("# ").strip())
+ else:
+ break
+ return "\n".join(lines) if lines else None
+
+def _csharp_module_doc(root: Node, src: str) -> str | None:
+ """A ``/**`` block, else a run of ``///`` lines, before the usings or namespace."""
+ triple_slash_lines: list[str] = []
+ for child in root.children:
+ if child.type == "comment":
+ text = node_text(child, src).strip()
+ if text.startswith("/**"):
+ return clean_jsdoc(text)
+ # A plain // comment is skipped; the scan goes on for /// runs.
+ if text.startswith("///"):
+ triple_slash_lines.append(text.lstrip("/ ").strip())
+ elif child.type not in (
+ "comment",
+ "using_directive",
+ "global_using_directive",
+ "extern_alias_directive",
+ ):
+ break
+ if triple_slash_lines:
+ return _csharp_clean_xml_doc("\n".join(triple_slash_lines))
return None
-def extract_symbol_docstring(def_node: Node, src: str, lang: str) -> str | None:
- """Extract the docstring from a symbol's body node."""
- if lang == "python":
- body = def_node.child_by_field_name("body")
- if body is None:
- return None
- for child in body.children:
- if child.type == "expression_statement":
- for sub in child.children:
- if sub.type == "string":
- return clean_string_literal(node_text(sub, src))
- return None
- elif child.type not in ("comment", "newline"):
- return None
- return None
+def _swift_module_doc(root: Node, src: str) -> str | None:
+ for child in root.children:
+ if child.type == "comment":
+ text = node_text(child, src).strip()
+ if text.startswith("/**"):
+ return clean_jsdoc(text)
+ elif text.startswith("///"):
+ return text.lstrip("/ ").strip()
+ elif child.type not in ("comment", "import_declaration"):
+ break
+ return None
- elif lang in ("typescript", "javascript", "svelte", "vue", "astro"):
- return find_preceding_jsdoc(def_node, src)
- elif lang == "go":
- # Leading // comment lines before the function
- parent = def_node.parent
- if parent is None:
+def _luau_module_doc(root: Node, src: str) -> str | None:
+ """A ``--[[ ]]`` block, else a run of leading ``---`` lines."""
+ triple_dash_lines: list[str] = []
+ for child in root.children:
+ if child.type != "comment":
+ break
+ text = node_text(child, src).strip()
+ if text.startswith("--[["):
+ inner = text[4:]
+ if inner.endswith("]]"):
+ inner = inner[:-2]
+ return inner.strip()
+ if text.startswith("---"):
+ triple_dash_lines.append(text.lstrip("- ").strip())
+ return "\n".join(triple_dash_lines) if triple_dash_lines else None
+
+
+def _block_doc_reader(
+ comment_kinds: tuple[str, ...], skip_kinds: tuple[str, ...]
+) -> Callable[[Node, str], str | None]:
+ return lambda root, src: _leading_block_doc(root, src, comment_kinds, skip_kinds)
+
+
+_JS_LIKE = ("typescript", "javascript", "svelte", "vue", "astro")
+_C_LIKE = ("cpp", "c", "objectivec")
+
+_MODULE_DOC: dict[str, Callable[[Node, str], str | None]] = {
+ "python": _python_module_doc,
+ **dict.fromkeys(_JS_LIKE, _block_doc_reader(("comment",), ("comment",))),
+ "go": _go_module_doc,
+ "fsharp": _fsharp_module_doc,
+ "rust": _rust_module_doc,
+ # Doxygen: the first /** block before any declaration.
+ **dict.fromkeys(
+ _C_LIKE,
+ _block_doc_reader(
+ ("comment", "block_comment"),
+ ("comment", "preproc_include", "preproc_ifdef", "preproc_ifndef"),
+ ),
+ ),
+ "kotlin": _block_doc_reader(
+ ("comment", "multiline_comment"), ("comment", "package_header", "import")
+ ),
+ "elixir": _elixir_module_doc,
+ "ruby": _ruby_module_doc,
+ "csharp": _csharp_module_doc,
+ "swift": _swift_module_doc,
+ "scala": _block_doc_reader(("comment",), ("comment", "import_declaration")),
+ "php": _block_doc_reader(
+ ("comment",),
+ ("comment", "php_tag", "namespace_definition", "namespace_use_declaration"),
+ ),
+ "java": _block_doc_reader(
+ ("comment", "block_comment", "line_comment"),
+ ("comment", "package_declaration", "import_declaration"),
+ ),
+ "luau": _luau_module_doc,
+}
+
+
+def _python_symbol_doc(def_node: Node, src: str) -> str | None:
+ body = def_node.child_by_field_name("body")
+ if body is None:
+ return None
+ for child in body.children:
+ if child.type == "expression_statement":
+ for sub in child.children:
+ if sub.type == "string":
+ return clean_string_literal(node_text(sub, src))
return None
- siblings = list(parent.children)
- idx = next((i for i, s in enumerate(siblings) if s.id == def_node.id), -1)
- if idx <= 0:
+ elif child.type not in ("comment", "newline"):
return None
- lines: list[str] = []
- i = idx - 1
- while i >= 0 and siblings[i].type == "comment":
- lines.insert(0, node_text(siblings[i], src).lstrip("/ ").strip())
- i -= 1
- return "\n".join(lines) if lines else None
-
- elif lang == "fsharp":
- # `xml_doc` is a preceding SIBLING of the statement, and the captured
- # node is usually nested inside that statement (a binding's left-hand
- # side inside its `declaration_expression`, a type body inside its
- # `type_definition`). Climb only while the node is still the first
- # thing in its parent: the second clause of a `let rec f ... and g`
- # is not, and must not inherit the first clause's doc.
- node: Node | None = def_node
- for _ in range(4):
- if node is None:
- break
- docs = _fsharp_preceding_doc(node, src)
- if docs:
- return _fsharp_doc_text(docs, src)
- parent = node.parent
- if parent is None:
- break
- leading = [
- sib
- for sib in parent.named_children
- if sib.id != node.id and sib.start_byte < node.start_byte
- ]
- if any(sib.type not in ("attributes", "xml_doc") for sib in leading):
- break
- node = parent
+ return None
+
+
+def _preceding_siblings(node: Node) -> tuple[list[Node], int]:
+ """*node*'s parent's children and *node*'s index there (``-1`` when unparented)."""
+ parent = node.parent
+ if parent is None:
+ return [], -1
+ siblings = list(parent.children)
+ return siblings, next((i for i, s in enumerate(siblings) if s.id == node.id), -1)
+
+
+def _go_symbol_doc(def_node: Node, src: str) -> str | None:
+ """The ``//`` lines right before the declaration."""
+ siblings, idx = _preceding_siblings(def_node)
+ if idx <= 0:
return None
+ lines: list[str] = []
+ i = idx - 1
+ while i >= 0 and siblings[i].type == "comment":
+ lines.insert(0, node_text(siblings[i], src).lstrip("/ ").strip())
+ i -= 1
+ return "\n".join(lines) if lines else None
- elif lang == "rust":
- # /// doc comments or /** block doc comments before the item.
- # Attributes (#[...]) may appear between the doc comment and the
- # item, so we skip over attribute_item nodes when walking backward.
- parent = def_node.parent
- if parent is None:
- return None
- siblings = list(parent.children)
- idx = next((i for i, s in enumerate(siblings) if s.id == def_node.id), -1)
- if idx <= 0:
- return None
- # Walk backward, skipping attribute_item nodes
- i = idx - 1
- while i >= 0 and siblings[i].type == "attribute_item":
- i -= 1
- # Check for /** block doc comment first
- if i >= 0 and siblings[i].type == "block_comment":
- text = node_text(siblings[i], src).strip()
- if text.startswith("/**"):
- return clean_jsdoc(text)
- # Collect consecutive /// line doc comments
- lines: list[str] = []
- while i >= 0 and siblings[i].type == "line_comment":
- text = node_text(siblings[i], src).strip()
- if text.startswith("///"):
- lines.insert(0, text[3:].strip())
- i -= 1
- else:
- break
- return "\n".join(lines) if lines else None
-
- elif lang == "java":
- # /** Javadoc */ comment before the method/class
- return find_preceding_block_comment(def_node, src, "/**")
-
- elif lang in ("cpp", "c", "objectivec"):
- # Doxygen: /** ... */ block comment or /// line comments
- result = find_preceding_block_comment(def_node, src, "/**")
- if result:
- return result
- return _find_preceding_line_doc_comments(def_node, src, "///")
-
- elif lang == "kotlin":
- # KDoc: /** ... */ block comment before declaration
- return find_preceding_block_comment(def_node, src, "/**")
-
- elif lang == "elixir":
- # @doc is a preceding sibling, with no AST link to the def it
- # documents. Other attributes (@impl, @spec) may sit between the two,
- # so walk back over attributes and stop at the first real statement.
- parent = def_node.parent
+
+def _fsharp_symbol_doc(def_node: Node, src: str) -> str | None:
+ """The ``xml_doc`` run before the statement holding *def_node*.
+
+ The captured node is usually nested inside that statement (a binding's
+ left-hand side inside its ``declaration_expression``, a type body inside
+ its ``type_definition``). Climb only while the node is still the first
+ thing in its parent: the second clause of a ``let rec f ... and g`` is not,
+ and must not inherit the first clause's doc.
+ """
+ node: Node | None = def_node
+ for _ in range(4):
+ if node is None:
+ break
+ docs = _fsharp_preceding_doc(node, src)
+ if docs:
+ return _fsharp_doc_text(docs, src)
+ parent = node.parent
if parent is None:
- return None
- siblings = list(parent.named_children)
- index = next((i for i, s in enumerate(siblings) if s.id == def_node.id), -1)
- for previous in reversed(siblings[:index]):
- if previous.type != "unary_operator":
- return None
- doc = _elixir_attribute_doc(previous, src, ("doc",))
- if doc:
- return doc
- return None
+ break
+ leading = [
+ sib
+ for sib in parent.named_children
+ if sib.id != node.id and sib.start_byte < node.start_byte
+ ]
+ if any(sib.type not in ("attributes", "xml_doc") for sib in leading):
+ break
+ node = parent
+ return None
+
- elif lang == "ruby":
- # RDoc/YARD: # comment lines before method/class
- return _find_preceding_line_doc_comments(def_node, src, "#")
-
- elif lang == "csharp":
- # XML doc comments: /// lines or /** block. After collecting the raw
- # text, strip XML tags so callers see the human-readable summary.
- result = find_preceding_block_comment(def_node, src, "/**")
- if not result:
- result = _find_preceding_line_doc_comments(def_node, src, "///")
- if result:
- return _csharp_clean_xml_doc(result)
+def _rust_symbol_doc(def_node: Node, src: str) -> str | None:
+ """A ``/**`` block, else ``///`` lines, before the item, over ``#[...]`` attributes."""
+ siblings, idx = _preceding_siblings(def_node)
+ if idx <= 0:
return None
+ i = idx - 1
+ while i >= 0 and siblings[i].type == "attribute_item":
+ i -= 1
+ if i >= 0 and siblings[i].type == "block_comment":
+ text = node_text(siblings[i], src).strip()
+ if text.startswith("/**"):
+ return clean_jsdoc(text)
+ lines: list[str] = []
+ while i >= 0 and siblings[i].type == "line_comment":
+ text = node_text(siblings[i], src).strip()
+ if not text.startswith("///"):
+ break
+ lines.insert(0, text[3:].strip())
+ i -= 1
+ return "\n".join(lines) if lines else None
- elif lang == "swift":
- # Swift doc: /** block or /// lines
- result = find_preceding_block_comment(def_node, src, "/**")
- if result:
- return result
- return _find_preceding_line_doc_comments(def_node, src, "///")
- elif lang == "scala":
- # ScalaDoc: /** ... */
- return find_preceding_block_comment(def_node, src, "/**")
+def _block_or_line_doc(def_node: Node, src: str) -> str | None:
+ """A ``/**`` block, else ``///`` lines (Doxygen, Swift)."""
+ result = find_preceding_block_comment(def_node, src, "/**")
+ if result:
+ return result
+ return _find_preceding_line_doc_comments(def_node, src, "///")
- elif lang == "php":
- # PHPDoc: /** ... */
- return find_preceding_block_comment(def_node, src, "/**")
- elif lang == "luau":
- # Luau: --[[ block ]] or --- triple-dash lines preceding the symbol
- parent = def_node.parent
- if parent is None:
- return None
- siblings = list(parent.children)
- idx = next((i for i, s in enumerate(siblings) if s.id == def_node.id), -1)
- if idx <= 0:
+def _javadoc(def_node: Node, src: str) -> str | None:
+ """A ``/** ... */`` block before the declaration (Javadoc, KDoc, ScalaDoc, PHPDoc)."""
+ return find_preceding_block_comment(def_node, src, "/**")
+
+
+def _elixir_symbol_doc(def_node: Node, src: str) -> str | None:
+ # @doc is a preceding sibling, with no AST link to the def it
+ # documents. Other attributes (@impl, @spec) may sit between the two,
+ # so walk back over attributes and stop at the first real statement.
+ parent = def_node.parent
+ if parent is None:
+ return None
+ siblings = list(parent.named_children)
+ index = next((i for i, s in enumerate(siblings) if s.id == def_node.id), -1)
+ for previous in reversed(siblings[:index]):
+ if previous.type != "unary_operator":
return None
- lines: list[str] = []
- i = idx - 1
- while i >= 0 and siblings[i].type == "comment":
- text = node_text(siblings[i], src).strip()
- if text.startswith("--[["):
- inner = text[4:]
- if inner.endswith("]]"):
- inner = inner[:-2]
- return inner.strip()
- if text.startswith("---"):
- lines.insert(0, text.lstrip("- ").strip())
- i -= 1
- else:
- break
- return "\n".join(lines) if lines else None
+ doc = _elixir_attribute_doc(previous, src, ("doc",))
+ if doc:
+ return doc
+ return None
+
+def _ruby_symbol_doc(def_node: Node, src: str) -> str | None:
+ """RDoc / YARD: the ``#`` lines before the method or class."""
+ return _find_preceding_line_doc_comments(def_node, src, "#")
+
+
+def _csharp_symbol_doc(def_node: Node, src: str) -> str | None:
+ """A ``/**`` block or ``///`` lines, with the XML stripped to the summary."""
+ result = find_preceding_block_comment(def_node, src, "/**")
+ if not result:
+ result = _find_preceding_line_doc_comments(def_node, src, "///")
+ if result:
+ return _csharp_clean_xml_doc(result)
return None
+def _luau_symbol_doc(def_node: Node, src: str) -> str | None:
+ """A ``--[[ ]]`` block, else ``---`` lines, before the symbol."""
+ siblings, idx = _preceding_siblings(def_node)
+ if idx <= 0:
+ return None
+ lines: list[str] = []
+ i = idx - 1
+ while i >= 0 and siblings[i].type == "comment":
+ text = node_text(siblings[i], src).strip()
+ if text.startswith("--[["):
+ inner = text[4:]
+ if inner.endswith("]]"):
+ inner = inner[:-2]
+ return inner.strip()
+ if not text.startswith("---"):
+ break
+ lines.insert(0, text.lstrip("- ").strip())
+ i -= 1
+ return "\n".join(lines) if lines else None
+
+
+_SYMBOL_DOC: dict[str, Callable[[Node, str], str | None]] = {
+ "python": _python_symbol_doc,
+ **dict.fromkeys(_JS_LIKE, find_preceding_jsdoc),
+ "go": _go_symbol_doc,
+ "fsharp": _fsharp_symbol_doc,
+ "rust": _rust_symbol_doc,
+ "java": _javadoc,
+ **dict.fromkeys(_C_LIKE, _block_or_line_doc),
+ "kotlin": _javadoc,
+ "elixir": _elixir_symbol_doc,
+ "ruby": _ruby_symbol_doc,
+ "csharp": _csharp_symbol_doc,
+ "swift": _block_or_line_doc,
+ "scala": _javadoc,
+ "php": _javadoc,
+ "luau": _luau_symbol_doc,
+}
+
+
def _find_preceding_line_doc_comments(node: Node, src: str, prefix: str) -> str | None:
"""Collect consecutive line comments with *prefix* (e.g. ``///``) before *node*."""
parent = node.parent