Allow splitting a subscript if line goes over col limit
diff --git a/CHANGELOG b/CHANGELOG
index a334d68..6e6621d 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -12,6 +12,7 @@
- Make sure that tuples and the like are formatted nicely if they all can't fit
on a single line. This is similar to how we format function calls within an
argument list.
+- Allow splitting in a subscript if it goes over the line limit.
## [0.23.0] 2018-08-27
### Added
diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index 318726f..cd6a729 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -44,9 +44,6 @@
next_token: The next token to be formatted.
paren_level: The level of nesting inside (), [], and {}.
lowest_level_on_line: The lowest paren_level on the current line.
- newline: Indicates if a newline is added along the edge to this format
- decision state node.
- previous: The previous format decision state in the decision tree.
stack: A stack (of _ParenState) keeping track of properties applying to
parenthesis levels.
comp_stack: A stack (of ComprehensionState) keeping track of properties
@@ -74,8 +71,6 @@
self.stack = [_ParenState(first_indent, first_indent)]
self.comp_stack = []
self.first_indent = first_indent
- self.newline = False
- self.previous = None
self.column_limit = style.Get('COLUMN_LIMIT')
def Clone(self):
@@ -89,8 +84,6 @@
new.lowest_level_on_line = self.lowest_level_on_line
new.ignore_stack_for_comparison = self.ignore_stack_for_comparison
new.first_indent = self.first_indent
- new.newline = self.newline
- new.previous = self.previous
new.stack = [state.Clone() for state in self.stack]
new.comp_stack = [state.Clone() for state in self.comp_stack]
return new
@@ -186,7 +179,8 @@
if (self.stack[-1].split_before_closing_bracket and
current.value in '}]' and style.Get('SPLIT_BEFORE_CLOSING_BRACKET')):
# Split before the closing bracket if we can.
- return current.node_split_penalty != split_penalty.UNBREAKABLE
+ if format_token.Subtype.SUBSCRIPT_BRACKET not in current.subtypes:
+ return current.node_split_penalty != split_penalty.UNBREAKABLE
if (current.value == ')' and previous.value == ',' and
not _IsSingleElementTuple(current.matching_bracket)):
diff --git a/yapftests/reformatter_buganizer_test.py b/yapftests/reformatter_buganizer_test.py
index 3cbc104..2d20cee 100644
--- a/yapftests/reformatter_buganizer_test.py
+++ b/yapftests/reformatter_buganizer_test.py
@@ -28,6 +28,25 @@
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())
+ def testB112651423(self):
+ unformatted_code = """\
+def potato(feeditems, browse_use_case=None):
+ for item in turnip:
+ if kumquat:
+ if not feeds_variants.variants['FEEDS_LOAD_PLAYLIST_VIDEOS_FOR_ALL_ITEMS'] and item.video:
+ continue
+"""
+ expected_formatted_code = """\
+def potato(feeditems, browse_use_case=None):
+ for item in turnip:
+ if kumquat:
+ if not feeds_variants.variants[
+ 'FEEDS_LOAD_PLAYLIST_VIDEOS_FOR_ALL_ITEMS'] and item.video:
+ continue
+"""
+ uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
+
def testB80484938(self):
code = """\
for sssssss, aaaaaaaaaa in [