|
1 | 1 | """ |
2 | 2 | --- AUTO-GENERATED DOCSTRING --- |
3 | | - Table of content is automatically generated by Agent Docstrings v1.3.2 |
| 3 | + Table of content is automatically generated by Agent Docstrings v1.3.3 |
4 | 4 | |
5 | 5 | Classes/Functions: |
6 | | - - SignatureInfo (line 17): |
7 | | - - ClassInfo (line 21): |
8 | | - - CommentStyle (line 27): |
9 | | - - remove_agent_docstring(text: str, language: str) -> str (line 46) |
| 6 | + - SignatureInfo (line 19): |
| 7 | + - ClassInfo (line 24): |
| 8 | + - CommentStyle (line 31): |
| 9 | + - remove_agent_docstring(text: str, language: str) -> str (line 52) |
10 | 10 | --- END AUTO-GENERATED DOCSTRING --- |
11 | 11 | """ |
12 | 12 | from __future__ import annotations |
@@ -57,22 +57,47 @@ def remove_agent_docstring(text: str, language: str) -> str: |
57 | 57 | if language == "python": |
58 | 58 | def replacer(match): |
59 | 59 | docstring_content = match.group(0) |
| 60 | + |
| 61 | + # If the docstring doesn't contain the agent marker, it's a manual docstring. |
| 62 | + # Leave it untouched. |
| 63 | + if DOCSTRING_START_MARKER not in docstring_content: |
| 64 | + return docstring_content |
| 65 | + |
| 66 | + # * Match the auto-generated block inside the docstring, including any leading/ |
| 67 | + # * trailing whitespace and the trailing newline (if present). Use single |
| 68 | + # * backslashes so that ``\s`` is interpreted by the *regex* engine as a |
| 69 | + # * whitespace token instead of a literal backslash followed by ``s``. |
60 | 70 | auto_content_pattern = re.compile( |
61 | | - rf'\s*{start_marker_escaped}[\s\S]*?{end_marker_escaped}\s*?\n?', |
62 | | - re.DOTALL |
| 71 | + rf"\s*{start_marker_escaped}[\s\S]*?{end_marker_escaped}\s*\n?", |
| 72 | + re.DOTALL, |
63 | 73 | ) |
64 | 74 | cleaned_docstring = auto_content_pattern.sub('', docstring_content) |
| 75 | + |
| 76 | + # Check what's left after removing the agent part |
65 | 77 | temp_cleaned = cleaned_docstring.replace('"""', '').replace("'''", '').strip() |
| 78 | + |
66 | 79 | if not temp_cleaned: |
67 | | - return '' # Remove empty docstring |
68 | | - # Ensure single newline padding for non-empty manual comments |
69 | | - return f'"""\n{temp_cleaned}\n"""' |
70 | | - docstring_pattern = re.compile(r'^\s*("""[\s\S]*?"""|'r"'''[\s\S]*?''')") |
| 80 | + return '' # Docstring was purely agent-generated, so remove it. |
| 81 | + |
| 82 | + # There was a manual part. Reformat it cleanly. |
| 83 | + return f'"""\\n{temp_cleaned}\\n"""' |
| 84 | + |
| 85 | + # * Match ANY triple-quoted block (single or double quotes) anywhere in the text. |
| 86 | + # * The former pattern anchored at ``^`` missed auto-generated blocks that were |
| 87 | + # * not located at the very start of the file, leading to duplication issues. |
| 88 | + docstring_pattern = re.compile( |
| 89 | + r'("""[\s\S]*?"""|\'\'\'[\s\S]*?\'\'\')', |
| 90 | + re.DOTALL, |
| 91 | + ) |
71 | 92 | # Iteratively clean the text |
72 | 93 | cleaned_text = docstring_pattern.sub(replacer, text) |
73 | | - cleaned_text = docstring_pattern.sub(replacer, cleaned_text) # Run again to handle adjacent blocks |
74 | | - # Collapse whitespace and return |
75 | | - return cleaned_text.strip() |
| 94 | + # * Run a second pass to handle cases where two docstrings appear back-to-back, |
| 95 | + # * which can happen after removing an intermediary block. |
| 96 | + cleaned_text = docstring_pattern.sub(replacer, cleaned_text) |
| 97 | + # * Remove leading whitespace that may be left after docstring removal |
| 98 | + # * to ensure consistent line numbering between runs |
| 99 | + cleaned_text = cleaned_text.lstrip('\n') |
| 100 | + return cleaned_text |
76 | 101 | else: |
77 | 102 | # For C-style comments, be more flexible with the format |
78 | 103 | # Handle both compact (/**---...---*/) and expanded formats |
|
0 commit comments