Skip to content

Commit

Permalink
Merge pull request google#519 from alanhdu/master
Browse files Browse the repository at this point in the history
Expand list of allowable string prefixes
  • Loading branch information
bwendling authored Feb 1, 2018
2 parents fe6460f + 6606776 commit f5fd454
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
12 changes: 10 additions & 2 deletions yapf/yapflib/format_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,17 @@ def is_string(self):
@property
@py3compat.lru_cache()
def is_multiline_string(self):
if py3compat.PY3:
prefix = "("
prefix += "r|u|R|U|f|F|fr|Fr|fR|FR|rf|rF|Rf|RF" # strings
prefix += "|b|B|br|Br|bR|BR|rb|rB|Rb|RB" # bytes
prefix += ")?"
else:
prefix = "[uUbB]?[rR]?"

regex = r'^{prefix}(?P<delim>"""|\'\'\').*(?P=delim)$'.format(prefix=prefix)
return (self.is_string and
re.match(r'^[uUbB]?[rR]?(?P<delim>"""|\'\'\').*(?P=delim)$',
self.value, re.DOTALL) is not None)
re.match(regex, self.value, re.DOTALL) is not None)

@property
@py3compat.lru_cache()
Expand Down
7 changes: 7 additions & 0 deletions yapftests/format_token_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def testSimple(self):
self.assertEqual("FormatToken(name=COMMENT, value=# A comment)", str(tok))
self.assertTrue(tok.is_comment)

def testIsMultilineString(self):
tok = format_token.FormatToken(pytree.Leaf(token.STRING, '"""hello"""'))
self.assertTrue(tok.is_multiline_string)

tok = format_token.FormatToken(pytree.Leaf(token.STRING, 'r"""hello"""'))
self.assertTrue(tok.is_multiline_string)


if __name__ == "__main__":
unittest.main()
13 changes: 13 additions & 0 deletions yapftests/reformatter_python3_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,19 @@ def foo(self):
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))

def testMultilineFormatString(self):
if sys.version_info[1] < 6:
return
code = """\
# yapf: disable
(f'''
''')
# yapf: enable
"""
# https://github.com/google/yapf/issues/513
uwlines = yapf_test_helper.ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))


if __name__ == '__main__':
unittest.main()

0 comments on commit f5fd454

Please sign in to comment.