-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
215 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
7b4144b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#!/usr/bin/env python3
import re
import sys
from pathlib import Path
Regex to find repeated comment blocks.
This pattern is intentionally flexible to capture repeated doc lines.
Adjust as needed for your doc style, e.g., capturing "/*" or " * " lines, etc.
DOC_COMMENT_PATTERN = re.compile(
r'(?P'
r'(?:/*[\s\S]?*/|//[^\n]\n+)' # A doc block (/* ... / or // ...)
r')\s'
r'(?P=block)' # The same block repeated immediately after
, re.MULTILINE
)
def merge_repeated_comments(code_text: str) -> str:
"""
Merge repeated doc-comment blocks into a single instance.
Returns the transformed code text.
"""
# We repeatedly apply the regex until no more duplicates are found
# (handle scenarios with multiple repeated blocks).
merged = code_text
while True:
new_text, count = DOC_COMMENT_PATTERN.subn(r'\1', merged)
if count == 0:
break
merged = new_text
return merged
def unify_doc_spacing(code_text: str) -> str:
"""
Example function to enforce consistent spacing after doc comments.
Insert a blank line after each doc block if missing.
"""
# Insert a newline after a doc comment block if not already followed by one.
# This pattern looks for a closing '/' or last '//' line not already followed by blank line.
pattern = re.compile(r'(*/|//[^\n])(?P\n(?!\n))')
return pattern.sub(r'\1\n\n', code_text)
def process_file(path: Path):
original_text = path.read_text(encoding='utf-8')
merged = merge_repeated_comments(original_text)
spaced = unify_doc_spacing(merged)
def main():
if len(sys.argv) < 2:
print("Usage: python merge_doc_comments.py [ ...]")
sys.exit(1)
if name == "main":
main()