Skip to content

Commit

Permalink
Fix #159
Browse files Browse the repository at this point in the history
  • Loading branch information
bricoletc committed Dec 12, 2022
1 parent f37c097 commit 1423e6b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
11 changes: 7 additions & 4 deletions snakefmt/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
r"(.*)^(if|elif|else|with|for|while)([^:]*)(:.*)", re.S | re.M
)
after_if_keywords = ("elif", "else")
is_all_comments = lambda string: all(map(comment_start, string.splitlines()))


class Formatter(Parser):
Expand Down Expand Up @@ -105,8 +106,7 @@ def flush_buffer(
if comment_start(self.buffer.rstrip().splitlines()[-1]):
formatted += "\n"
# Only stick together separated single-parm keywords when separated by comments
buffer_is_all_comments = all(map(comment_start, self.buffer.splitlines()))
if not buffer_is_all_comments:
if not is_all_comments(self.buffer):
self.last_recognised_keyword = ""
self.add_newlines(self.block_indent, formatted, final_flush, in_global_context)
self.buffer = ""
Expand Down Expand Up @@ -135,8 +135,11 @@ def process_keyword_param(
def run_black_format_str(
self, string: str, target_indent: int, extra_spacing: int = 0
) -> str:
needs_artifical_nest = self.from_python and not string.startswith("if")
needs_artifical_nest = False
needs_artifical_nest = (
self.from_python
and not string.startswith("if")
and not is_all_comments(string)
)
if needs_artifical_nest:
string = f"if x:\n{textwrap.indent(string, TAB)}"

Expand Down
3 changes: 1 addition & 2 deletions snakefmt/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ def __init__(self, snakefile: TokenIterator):
in_if_else = self.buffer.startswith(("if", "else", "elif"))
if self.syntax.from_python or status.pythonable or in_if_else:
self.from_python = True
else: # Over-indented context gets reset
self.syntax.cur_indent = max(self.keyword_indent - 1, 0)
elif self.from_python:
# We are exiting python context, so force spacing out keywords
self.last_recognised_keyword = ""
Expand Down Expand Up @@ -117,6 +115,7 @@ def __init__(self, snakefile: TokenIterator):
self.from_python
and status.cur_indent == 0
and not self.last_block_was_snakecode
and self.block_indent > 0
):
# This flushes any nested python code following a
# nested snakemake keyword
Expand Down
2 changes: 1 addition & 1 deletion tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ def test_snakecode_after_indented_comment_does_not_get_unindented(self):
snakecode = (
'if config.get("s3_dst"):\n\n'
f'{TAB * 1}include: "workflow/rule1.smk"\n'
f'{TAB * 1}include: "workflow/rule2.smk"\n\n'
f'{TAB * 1}include: "workflow/rule2.smk"\n'
f"{TAB * 1}# a comment\n"
f"{TAB * 1}# further comment\n"
f'{TAB * 1}include: "workflow/rule3.smk"\n'
Expand Down

0 comments on commit 1423e6b

Please sign in to comment.