Skip to content

Commit

Permalink
fix: do not align the inside of multiline strings [#123]]
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhall88 committed Nov 8, 2022
1 parent bcc5371 commit bb4aabf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
6 changes: 4 additions & 2 deletions snakefmt/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ def align_strings(self, string: str, target_indent: int) -> str:
first = textwrap.indent(textwrap.dedent(all_lines[0]), used_indent)
indented += first

is_r_string = re.match(r"r['\"]", first.lstrip())
is_multiline_string = re.match(
r"[bfru]?\"\"\"|'''", first.lstrip(), flags=re.IGNORECASE
)
if len(all_lines) > 2:
if is_r_string:
if is_multiline_string:
middle = "".join(all_lines[1:-1])
else:
middle = textwrap.indent(
Expand Down
2 changes: 1 addition & 1 deletion snakefmt/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LogConfig:
log_template = "[%(levelname)s]{0} %(message)s"

@classmethod
def init(cls, log_level):
def init(cls, log_level: int):
cls.logger = logging.getLogger("snakefmt")
cls.handler = logging.StreamHandler()
cls.logger.addHandler(cls.handler)
Expand Down
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
from io import StringIO

from snakefmt.formatter import Formatter
from snakefmt.logging import LogConfig
from snakefmt.parser.parser import Snakefile


def setup_formatter(snake: str, line_length: int = None, black_config_file=None):
stream = StringIO(snake)
smk = Snakefile(stream)
LogConfig.init(logging.DEBUG)
return Formatter(smk, line_length=line_length, black_config_file=black_config_file)
28 changes: 25 additions & 3 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,9 @@ def test_tpq_alignment_and_keep_relative_indenting(self):
rule a:
{TAB * 1}shell:
{TAB * 2}"""Starts here
{TAB * 2}Hello
{TAB * 2} World
{TAB * 4}Tabbed
{TAB * 0} Hello
{TAB * 1}World
{TAB * 2} Tabbed
{TAB * 2}"""
'''
assert formatter.get_formatted() == expected
Expand All @@ -671,6 +671,28 @@ def test_tpq_alignment_and_keep_relative_indenting_for_r_string(self):

assert formatter.get_formatted() == snakecode

def test_tpq_alignment_and_keep_relative_indenting_for_multiline_string(self):
snakecode = (
"rule a:\n"
f'{TAB * 1}shell: """\n'
f'{TAB * 2}python -c "\n'
f"{TAB * 0}if True:\n"
f"{TAB * 1}print('Hello, world!')\n"
f'{TAB * 2}"""'
)
formatter = setup_formatter(snakecode)
expected = (
"rule a:\n"
f"{TAB * 1}shell:\n"
f'{TAB * 2}"""\n'
f'{TAB * 2}python -c "\n'
f"{TAB * 0}if True:\n"
f"{TAB * 1}print('Hello, world!')\n"
f'{TAB * 2}"""\n'
)

assert formatter.get_formatted() == expected

def test_single_quoted_multiline_string_proper_tabbing(self):
snakecode = f"""
rule a:
Expand Down

0 comments on commit bb4aabf

Please sign in to comment.