Skip to content

Commit

Permalink
fix: indentation issues from #124
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhall88 committed Dec 1, 2022
1 parent a3846ae commit 399ec55
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
19 changes: 13 additions & 6 deletions snakefmt/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def flush_buffer(
self.buffer = ""
return

code_indent = self.syntax.code_indent
if not from_python:
formatted = self.run_black_format_str(self.buffer, self.target_indent)
if self.target_indent > 0:
Expand All @@ -84,7 +85,7 @@ def flush_buffer(
to_format = (
f"{re_match.group(1)}{test_substitute}" f"{re_match.group(4)}pass"
)
formatted = self.run_black_format_str(to_format, self.target_indent)
formatted = self.run_black_format_str(to_format, code_indent)
re_rematch = contextual_matcher.match(formatted)
if condition != "":
callback_keyword += re_rematch.group(3)
Expand All @@ -94,9 +95,8 @@ def flush_buffer(
formatted_lines = formatted.splitlines(keepends=True)
formatted = "".join(formatted_lines[:-1]) # Remove the 'pass' line
else:
formatted = self.run_black_format_str(self.buffer, self.target_indent)
formatted = self.run_black_format_str(self.buffer, code_indent)

code_indent = self.syntax.code_indent
if code_indent is not None:
formatted = textwrap.indent(formatted, f"{TAB * code_indent}")
if self.syntax.effective_indent == 0:
Expand All @@ -110,7 +110,7 @@ def flush_buffer(
buffer_is_all_comments = all(map(comment_start, self.buffer.splitlines()))
if not buffer_is_all_comments:
self.last_recognised_keyword = ""
self.add_newlines(self.target_indent, formatted, final_flush, in_global_context)
self.add_newlines(code_indent, formatted, final_flush, in_global_context)
self.buffer = ""

def process_keyword_context(self, in_global_context: bool):
Expand Down Expand Up @@ -196,6 +196,10 @@ def align_strings(self, string: str, target_indent: int) -> str:
indented = ""
for match in re.finditer(full_string_matcher, string):
indented += textwrap.indent(string[pos : match.start(1)], used_indent)
lagging_spaces = len(indented) - len(indented.rstrip(" "))
lagging_indent = (
TAB * int(lagging_spaces / 4) if lagging_spaces % 4 == 0 else ""
)
match_slice = string[match.start(1) : match.end(1)].replace("\t", TAB)
all_lines = match_slice.splitlines(keepends=True)
first = textwrap.indent(textwrap.dedent(all_lines[0]), used_indent)
Expand All @@ -209,14 +213,17 @@ def align_strings(self, string: str, target_indent: int) -> str:
middle = "".join(all_lines[1:-1])
else:
middle = textwrap.indent(
textwrap.dedent("".join(all_lines[1:-1])), used_indent
textwrap.dedent("".join(all_lines[1:-1])),
used_indent + lagging_indent,
)
indented += middle
if len(all_lines) > 1:
if is_multiline_string:
last = all_lines[-1]
else:
last = textwrap.indent(textwrap.dedent(all_lines[-1]), used_indent)
last = textwrap.indent(
textwrap.dedent(all_lines[-1]), used_indent + lagging_indent
)
indented += last
pos = match.end()
indented += textwrap.indent(string[pos:], used_indent)
Expand Down
3 changes: 2 additions & 1 deletion snakefmt/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def __init__(self, snakefile: TokenIterator):

if self.vocab.recognises(keyword):
if status.indent > self.target_indent:
if self.syntax.from_python or status.pythonable:
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.target_indent - 1, 0)
Expand Down
20 changes: 19 additions & 1 deletion tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,4 +1223,22 @@ def test_indenting_long_param_lines(self):
)
formatter = setup_formatter(snakecode)

assert formatter.get_formatted() == snakecode
assert formatter.get_formatted() == snakecode

def test_indented_block_with_functions_and_rule(self):
"""https://github.com/snakemake/snakefmt/issues/124#issuecomment-986845398"""
snakecode = (
"if True:\n\n"
f"{TAB*1}def func1():\n"
f'''{TAB*2}"""docstring"""\n'''
f"{TAB*2}pass\n\n"
f"{TAB*1}rule foo:\n"
f"{TAB*2}shell:\n"
f'{TAB*3}"echo bar"\n\n'
f"{TAB*1}def func2():\n"
f'''{TAB*2}"""this function should stay indented"""\n'''
f"{TAB*2}pass\n"
)
formatter = setup_formatter(snakecode)

assert formatter.get_formatted() == snakecode

0 comments on commit 399ec55

Please sign in to comment.