Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2 from zapier/single-quote-option-triples
Browse files Browse the repository at this point in the history
Keep using double quotes for triple-quoted strings with --single-quote.
  • Loading branch information
bryanhelmig authored May 30, 2018
2 parents df83086 + 4cdef98 commit 96253c3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ black [OPTIONS] [SRC]...
Options:
-l, --line-length INTEGER Where to wrap around. [default: 88]
--single-quote Use single quotes instead of double quotes in
strings.
strings except for triple-quoted strings.
--check Don't write the files back, just return the
status. Return code 0 means nothing would
change. Return code 1 means some files would be
Expand Down
14 changes: 8 additions & 6 deletions black.py
Original file line number Diff line number Diff line change
Expand Up @@ -2338,17 +2338,19 @@ def normalize_string_quotes(leaf: Leaf, single_quote: bool = False) -> None:

quote_char = '"'
alt_quote_char = "'"
triple_quote_chars = '"""'
alt_triple_quote_chars = "'''"

if single_quote:
quote_char = "'"
alt_quote_char = '"'

if value[:3] == (quote_char * 3):
if value[:3] == triple_quote_chars:
return

elif value[:3] == (alt_quote_char * 3):
orig_quote = alt_quote_char * 3
new_quote = quote_char * 3
elif value[:3] == alt_triple_quote_chars:
orig_quote = alt_triple_quote_chars
new_quote = triple_quote_chars
elif value[0] == quote_char:
orig_quote = quote_char
new_quote = alt_quote_char
Expand Down Expand Up @@ -2381,9 +2383,9 @@ def normalize_string_quotes(leaf: Leaf, single_quote: bool = False) -> None:
leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
new_body = sub_twice(unescaped_new_quote, rf"\1\\{new_quote}", new_body)
if new_quote == (quote_char * 3) and new_body[-1] == quote_char:
if new_quote == triple_quote_chars and new_body[-1] == triple_quote_chars[0]:
# edge case:
new_body = new_body[:-1] + "\\" + quote_char
new_body = new_body[:-1] + "\\" + triple_quote_chars[0]
orig_escape_count = body.count("\\")
new_escape_count = new_body.count("\\")
if new_escape_count > orig_escape_count:
Expand Down

0 comments on commit 96253c3

Please sign in to comment.