Skip to content

Commit

Permalink
Merge 5a7bed0 into 96a07df
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmego committed Mar 4, 2020
2 parents 96a07df + 5a7bed0 commit d1a92aa
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Expand Up @@ -11,6 +11,8 @@
- Don't over-indent a parameter list when not needed. But make sure it is
properly indented so that it doesn't collide with the lines afterwards.
- Don't split between two-word comparison operators: "is not", "not in", etc.
- Adds `FORCE_MULTILINE_DICT` knob to ensure dictionaries always split,
even when shorter than the max line length.
- New knob `SPACE_INSIDE_BRACKETS` to add spaces inside brackets, braces, and
parentheses.

Expand Down
4 changes: 4 additions & 0 deletions README.rst
Expand Up @@ -473,6 +473,10 @@ Knobs
``EACH_DICT_ENTRY_ON_SEPARATE_LINE``
Place each dictionary entry onto its own line.

``FORCE_MULTILINE_DICT``
Respect EACH_DICT_ENTRY_ON_SEPARATE_LINE even if the line is shorter than
COLUMN_LIMIT.

``I18N_COMMENT``
The regex for an internationalization comment. The presence of this comment
stops reformatting of that line, because the comments are required to be
Expand Down
3 changes: 3 additions & 0 deletions yapf/yapflib/reformatter.py
Expand Up @@ -257,6 +257,9 @@ def _CanPlaceOnSingleLine(uwline):
Returns:
True if the line can or should be added to a single line. False otherwise.
"""
token_names = [x.name for x in uwline.tokens]
if (style.Get('FORCE_MULTILINE_DICT') and 'LBRACE' in token_names):
return False
indent_amt = style.Get('INDENT_WIDTH') * uwline.depth
last = uwline.last
last_index = -1
Expand Down
9 changes: 9 additions & 0 deletions yapf/yapflib/style.py
Expand Up @@ -172,6 +172,13 @@ def method():
if the list is comma-terminated."""),
EACH_DICT_ENTRY_ON_SEPARATE_LINE=textwrap.dedent("""\
Place each dictionary entry onto its own line."""),
FORCE_MULTILINE_DICT=textwrap.dedent("""\
Require multiline dictionary even if it would normally fit on one line.
For example:
config = {
'key1': 'value1'
}"""),
I18N_COMMENT=textwrap.dedent("""\
The regex for an i18n comment. The presence of this comment stops
reformatting of that line, because the comments are required to be
Expand Down Expand Up @@ -383,6 +390,7 @@ def CreatePEP8Style():
INDENT_CLOSING_BRACKETS=False,
DISABLE_ENDING_COMMA_HEURISTIC=False,
EACH_DICT_ENTRY_ON_SEPARATE_LINE=True,
FORCE_MULTILINE_DICT=False,
I18N_COMMENT='',
I18N_FUNCTION_CALL='',
INDENT_DICTIONARY_VALUE=False,
Expand Down Expand Up @@ -566,6 +574,7 @@ def _IntOrIntListConverter(s):
INDENT_CLOSING_BRACKETS=_BoolConverter,
DISABLE_ENDING_COMMA_HEURISTIC=_BoolConverter,
EACH_DICT_ENTRY_ON_SEPARATE_LINE=_BoolConverter,
FORCE_MULTILINE_DICT=_BoolConverter,
I18N_COMMENT=str,
I18N_FUNCTION_CALL=_StringListConverter,
INDENT_DICTIONARY_VALUE=_BoolConverter,
Expand Down
43 changes: 43 additions & 0 deletions yapftests/file_resources_test.py
Expand Up @@ -438,6 +438,49 @@ def test_write_encoded_to_stdout(self):
None, s, in_place=False, encoding='utf-8')
self.assertEqual(stream.getvalue(), s)

class LineEndingTest(unittest.TestCase):
def test_line_ending_linefeed(self):
lines = [
'spam\n',
'spam\n'
]
actual = file_resources.LineEnding(lines)
self.assertEqual(actual, '\n')

def test_line_ending_carriage_return(self):
lines = [
'spam\r',
'spam\r'
]
actual = file_resources.LineEnding(lines)
self.assertEqual(actual, '\r')

def test_line_ending_combo(self):
lines = [
'spam\r\n',
'spam\r\n'
]
actual = file_resources.LineEnding(lines)
self.assertEqual(actual, '\r\n')

def test_line_ending_weighted(self):
lines = [
'spam\n',
'spam\n',
'spam\r',
'spam\r\n',
]
actual = file_resources.LineEnding(lines)
self.assertEqual(actual, '\n')

def test_line_ending_equal_weighted(self):
lines = [
'spam\n',
'spam\r',
'spam\r\n',
]
actual = file_resources.LineEnding(lines)
self.assertEqual(actual, '\r\n')

if __name__ == '__main__':
unittest.main()
36 changes: 34 additions & 2 deletions yapftests/reformatter_basic_test.py
Expand Up @@ -1937,8 +1937,8 @@ def mark_game_scored(gid):

def testDontAddBlankLineAfterMultilineString(self):
code = textwrap.dedent("""\
query = '''SELECT id
FROM table
query = '''SELECT id
FROM table
WHERE day in {}'''
days = ",".join(days)
""")
Expand Down Expand Up @@ -2954,6 +2954,38 @@ def b():
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))

def testForceMultilineDict_True(self):
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{force_multiline_dict: true}'))
unformatted_code = textwrap.dedent(
"responseDict = {'childDict': {'spam': 'eggs'}}\n")
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
actual = reformatter.Reformat(uwlines)
expected = textwrap.dedent("""\
responseDict = {
'childDict': {
'spam': 'eggs'
}
}
""")
self.assertCodeEqual(expected, actual)
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())

def testForceMultilineDict_False(self):
try:
style.SetGlobalStyle(
style.CreateStyleFromConfig('{force_multiline_dict: false}'))
unformatted_code = textwrap.dedent("""\
responseDict = {'childDict': {'spam': 'eggs'}}
""")
expected_formatted_code = unformatted_code
uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
finally:
style.SetGlobalStyle(style.CreateChromiumStyle())


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

0 comments on commit d1a92aa

Please sign in to comment.