Skip to content

Commit

Permalink
Merge f8b4751 into 6c6f029
Browse files Browse the repository at this point in the history
  • Loading branch information
brianmego committed Feb 25, 2020
2 parents 6c6f029 + f8b4751 commit d3ebbb5
Show file tree
Hide file tree
Showing 5 changed files with 52 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.

## [0.29.0] 2019-11-28
### Added
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 @@ -376,6 +383,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 @@ -558,6 +566,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
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 d3ebbb5

Please sign in to comment.