|
1 | 1 | import re |
2 | 2 | from typing import List, Generator |
3 | 3 |
|
4 | | -from griffe.dataclasses import Docstring |
| 4 | +from griffe.dataclasses import Docstring, Function, Parameters, Parameter |
5 | 5 | from griffe.docstrings import Parser |
6 | 6 |
|
7 | 7 | from mkdocstrings_handlers.vba.regex import re_signature, re_arg |
@@ -52,19 +52,23 @@ def find_file_docstring(code: str) -> Docstring: |
52 | 52 | It's the first block of comment lines before the first signature, if any. |
53 | 53 | """ |
54 | 54 | docstring_lines = [] |
| 55 | + lineno = None |
55 | 56 |
|
56 | | - for line in code.splitlines(): |
| 57 | + for i, line in enumerate(code.splitlines()): |
57 | 58 | if is_signature(line): |
58 | 59 | break |
59 | 60 | if is_comment(line): |
| 61 | + if lineno is None: |
| 62 | + # This is the first docstring line |
| 63 | + lineno = i |
| 64 | + |
60 | 65 | docstring_lines.append(line) |
61 | 66 |
|
62 | 67 | docstring_value = "\n".join(uncomment_lines(docstring_lines)) |
63 | 68 |
|
64 | 69 | return Docstring( |
65 | 70 | value=docstring_value, |
66 | | - parser=Parser.google, |
67 | | - parser_options={}, |
| 71 | + lineno=lineno, |
68 | 72 | ) |
69 | 73 |
|
70 | 74 |
|
@@ -159,14 +163,31 @@ def find_procedures(code: str) -> Generator[VbaProcedureInfo, None, None]: |
159 | 163 |
|
160 | 164 | docstring_value = "\n".join(uncomment_lines(docstring_lines)) |
161 | 165 |
|
| 166 | + # See https://mkdocstrings.github.io/griffe/usage/#using-griffe-as-a-docstring-parsing-library |
| 167 | + docstring = Docstring( |
| 168 | + value=docstring_value, |
| 169 | + parser=Parser.google, |
| 170 | + parser_options={}, |
| 171 | + lineno=procedure["first_line"] + 1, |
| 172 | + parent=Function( |
| 173 | + name=procedure["signature"].name, |
| 174 | + parameters=Parameters( |
| 175 | + *( |
| 176 | + Parameter( |
| 177 | + name=arg.name, |
| 178 | + annotation=arg.arg_type, |
| 179 | + default=arg.default, |
| 180 | + ) |
| 181 | + for arg in procedure["signature"].args |
| 182 | + ) |
| 183 | + ), |
| 184 | + ), |
| 185 | + ) |
| 186 | + |
162 | 187 | # Yield it and start over. |
163 | 188 | yield VbaProcedureInfo( |
164 | 189 | signature=procedure["signature"], |
165 | | - docstring=Docstring( |
166 | | - value=docstring_value, |
167 | | - parser=Parser.google, |
168 | | - parser_options={}, |
169 | | - ), |
| 190 | + docstring=docstring, |
170 | 191 | first_line=procedure["first_line"], |
171 | 192 | last_line=procedure["last_line"], |
172 | 193 | source=procedure_source, |
|
0 commit comments