Skip to content

Commit

Permalink
Allow containers to wrap gracefully
Browse files Browse the repository at this point in the history
And do not force breaks on commas in the middle of for comprehensions
  • Loading branch information
jesse-sony committed Oct 18, 2023
1 parent 9d1b7f7 commit b7991b3
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions yapf/yapflib/format_decision_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ def MustSplit(self):
# Allow the fallthrough code to handle the closing bracket.
if current != opening.matching_bracket:
# If the container doesn't fit in the current line, must split
return not self._ContainerFitsOnStartLine(opening)
if (subtypes.COMP_FOR not in current.subtypes and
not self._ContainerFitsOnStartLine(opening)):
return True

if (self.stack[-1].split_before_closing_bracket and
(current.value in '}]' and style.Get('SPLIT_BEFORE_CLOSING_BRACKET') or
Expand Down Expand Up @@ -546,9 +548,14 @@ def SurroundedByParens(token):
return True
else:
# Split after the opening of a container if it doesn't fit on the
# current line.
# current line, including checking for wrapping.
if not self._FitsOnLine(previous, previous.matching_bracket):
return True
arg_lengths = _CalculateArgLengths(previous)
start_col = self.column + len(current.value) + len(previous.value)
for length in arg_lengths:
length += start_col
if length > self.column_limit:
return True

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


_COMPOUND_STMTS = frozenset({
'for', 'while', 'if', 'elif', 'with', 'except', 'def', 'class', 'match',
'case'
})
_COMPOUND_STMTS = frozenset({'for', 'while', 'if', 'elif', 'with', 'except',
'def', 'class', 'match', 'case'})


def _IsCompoundStatement(token):
Expand Down

0 comments on commit b7991b3

Please sign in to comment.