diff --git a/format_ipy_cells/helpers.py b/format_ipy_cells/helpers.py index 4735677..f28c64c 100644 --- a/format_ipy_cells/helpers.py +++ b/format_ipy_cells/helpers.py @@ -96,3 +96,24 @@ def delete_last_cell_if_empty(text: str) -> str: """ # \Z matches only at end of string return re.sub(r"(?m)\s+^# %%\s*\Z", r"\n", text) + + +def single_blank_line_after_doc_string(text: str) -> str: + """Ensure single blank line between cell delimiter and preceding module doc string. + + Added to avoid clash with ruff format. Started enforcing single blank line in 0.3.0. + + --- before --- + '''module doc string.''' + + + # %% + some_code = 'here' + + --- after --- + '''module doc string.''' + + # %% + some_code = 'here' + """ + return re.sub(r'(?m)(["\']{3})\n+(# %%)', r"\1\n\n\2", text) diff --git a/format_ipy_cells/main.py b/format_ipy_cells/main.py index f1385be..2212ac1 100644 --- a/format_ipy_cells/main.py +++ b/format_ipy_cells/main.py @@ -9,6 +9,7 @@ format_comments_after_cell_delimiters, remove_empty_cells, remove_empty_lines_starting_cell, + single_blank_line_after_doc_string, ) @@ -39,6 +40,8 @@ def fix_file(filename: str) -> int: text = delete_last_cell_if_empty(text) + text = single_blank_line_after_doc_string(text) + if orig_text != text: print(f"Rewriting {filename}")