Skip to content

Commit

Permalink
fix: formatting of triple quoted strings [#152]
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhall88 committed Nov 8, 2022
1 parent beca978 commit 764e11d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
9 changes: 6 additions & 3 deletions snakefmt/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ def align_strings(self, string: str, target_indent: int) -> str:
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)
indented += first

is_multiline_string = re.match(
r"[bfru]?\"\"\"|'''", first.lstrip(), flags=re.IGNORECASE
)
indented += first

if len(all_lines) > 2:
if is_multiline_string:
middle = "".join(all_lines[1:-1])
Expand All @@ -213,7 +213,10 @@ def align_strings(self, string: str, target_indent: int) -> str:
)
indented += middle
if len(all_lines) > 1:
last = textwrap.indent(textwrap.dedent(all_lines[-1]), used_indent)
if is_multiline_string:
last = all_lines[-1]
else:
last = textwrap.indent(textwrap.dedent(all_lines[-1]), used_indent)
indented += last
pos = match.end()
indented += textwrap.indent(string[pos:], used_indent)
Expand Down
33 changes: 31 additions & 2 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def test_tpq_alignment_and_keep_relative_indenting(self):
{TAB * 0} Hello
{TAB * 1}World
{TAB * 2} Tabbed
{TAB * 2}"""
{TAB * 1}"""
'''
assert formatter.get_formatted() == expected

Expand Down Expand Up @@ -737,12 +737,41 @@ def test_docstrings_get_retabbed_for_snakecode_only(self):
rule a:
{TAB * 1}"""The rule a
{TAB * 1}"""
{TAB * 0}"""
{TAB * 1}message:
{TAB * 2}"a"
'''
assert formatter.get_formatted() == expected

def test_tpq_inside_run_block(self):
snakecode = '''rule cutadapt:
input:
"a.txt",
output:
"b.txt",
run:
if True:
shell(
"""
cutadapt \
-m 30 \
{input} \
-o {output}
"""
)
else:
shell(
"""
cutadapt \
{input} \
-o {output}
"""
)
'''
formatter = setup_formatter(snakecode)

assert formatter.get_formatted() == snakecode


class TestReformatting_SMK_BREAK:
"""
Expand Down

0 comments on commit 764e11d

Please sign in to comment.