Skip to content

Commit b7991b3

Browse files
committed
Allow containers to wrap gracefully
And do not force breaks on commas in the middle of for comprehensions
1 parent 9d1b7f7 commit b7991b3

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

yapf/yapflib/format_decision_state.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ def MustSplit(self):
203203
# Allow the fallthrough code to handle the closing bracket.
204204
if current != opening.matching_bracket:
205205
# If the container doesn't fit in the current line, must split
206-
return not self._ContainerFitsOnStartLine(opening)
206+
if (subtypes.COMP_FOR not in current.subtypes and
207+
not self._ContainerFitsOnStartLine(opening)):
208+
return True
207209

208210
if (self.stack[-1].split_before_closing_bracket and
209211
(current.value in '}]' and style.Get('SPLIT_BEFORE_CLOSING_BRACKET') or
@@ -546,9 +548,14 @@ def SurroundedByParens(token):
546548
return True
547549
else:
548550
# Split after the opening of a container if it doesn't fit on the
549-
# current line.
551+
# current line, including checking for wrapping.
550552
if not self._FitsOnLine(previous, previous.matching_bracket):
551-
return True
553+
arg_lengths = _CalculateArgLengths(previous)
554+
start_col = self.column + len(current.value) + len(previous.value)
555+
for length in arg_lengths:
556+
length += start_col
557+
if length > self.column_limit:
558+
return True
552559

553560
###########################################################################
554561
# Original Formatting Splitting
@@ -1106,10 +1113,8 @@ def _ContainerFitsOnStartLine(self, opening):
11061113
self.stack[-1].indent) <= self.column_limit
11071114

11081115

1109-
_COMPOUND_STMTS = frozenset({
1110-
'for', 'while', 'if', 'elif', 'with', 'except', 'def', 'class', 'match',
1111-
'case'
1112-
})
1116+
_COMPOUND_STMTS = frozenset({'for', 'while', 'if', 'elif', 'with', 'except',
1117+
'def', 'class', 'match', 'case'})
11131118

11141119

11151120
def _IsCompoundStatement(token):

0 commit comments

Comments
 (0)