Split before term paren rather than exceed col lim
diff --git a/CHANGELOG b/CHANGELOG
index 765e7c2..ed8eb7d 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -2,6 +2,11 @@
# All notable changes to this project will be documented in this file.
# This project adheres to [Semantic Versioning](http://semver.org/).
+## [0.12.1] UNRELEASED
+### Fixed
+- Prefer to split before a terminating r-paren in an argument list if the line
+ would otherwise go over the column limit.
+
## [0.12.0] 2016-09-25
### Added
- Support formatting of typed names. Typed names are formatted a similar way to
diff --git a/yapf/yapflib/format_decision_state.py b/yapf/yapflib/format_decision_state.py
index 396ac73..922c067 100644
--- a/yapf/yapflib/format_decision_state.py
+++ b/yapf/yapflib/format_decision_state.py
@@ -282,8 +282,8 @@
ntoken = current
while ntoken:
if ntoken.value == '(':
- total_len = (ntoken.matching_bracket.total_length -
- current.total_length)
+ total_len = (
+ ntoken.matching_bracket.total_length - current.total_length)
break
ntoken = ntoken.next_token
diff --git a/yapf/yapflib/split_penalty.py b/yapf/yapflib/split_penalty.py
index 0f08100..9960c33 100644
--- a/yapf/yapflib/split_penalty.py
+++ b/yapf/yapflib/split_penalty.py
@@ -24,6 +24,7 @@
# TODO(morbo): Document the annotations in a centralized place. E.g., the
# README file.
UNBREAKABLE = 1000 * 1000
+VERY_STRONGLY_CONNECTED = 3000
DOTTED_NAME = 2500
STRONGLY_CONNECTED = 2000
CONTIGUOUS_LIST = 500
@@ -210,7 +211,12 @@
last_child_node = last_child_node.prev_sibling
if not style.Get('DEDENT_CLOSING_BRACKETS'):
if _LastChildNode(last_child_node.prev_sibling).value != ',':
- self._SetUnbreakable(last_child_node)
+ if last_child_node.value == ']':
+ self._SetUnbreakable(last_child_node)
+ else:
+ pytree_utils.SetNodeAnnotation(
+ last_child_node, pytree_utils.Annotation.SPLIT_PENALTY,
+ VERY_STRONGLY_CONNECTED)
if _FirstChildNode(trailer).lineno == last_child_node.lineno:
# If the trailer was originally on one line, then try to keep it
diff --git a/yapftests/reformatter_test.py b/yapftests/reformatter_test.py
index 37bc142..599ffe7 100644
--- a/yapftests/reformatter_test.py
+++ b/yapftests/reformatter_test.py
@@ -1768,6 +1768,24 @@
def setUpClass(cls):
style.SetGlobalStyle(style.CreateChromiumStyle())
+ def testB26034238(self):
+ unformatted_code = textwrap.dedent("""\
+ class Thing:
+
+ def Function(self):
+ thing.Scrape('/aaaaaaaaa/bbbbbbbbbb/ccccc/dddd/eeeeeeeeeeeeee/ffffffffffffff').AndReturn(42)
+ """)
+ expected_formatted_code = textwrap.dedent("""\
+ class Thing:
+
+ def Function(self):
+ thing.Scrape(
+ '/aaaaaaaaa/bbbbbbbbbb/ccccc/dddd/eeeeeeeeeeeeee/ffffffffffffff'
+ ).AndReturn(42)
+ """)
+ uwlines = _ParseAndUnwrap(unformatted_code)
+ self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
+
def testB30536435(self):
unformatted_code = textwrap.dedent("""\
def main(unused_argv):
diff --git a/yapftests/split_penalty_test.py b/yapftests/split_penalty_test.py
index 51f8f3e..6bc80e0 100644
--- a/yapftests/split_penalty_test.py
+++ b/yapftests/split_penalty_test.py
@@ -24,6 +24,7 @@
from yapf.yapflib import split_penalty
UNBREAKABLE = split_penalty.UNBREAKABLE
+VERY_STRONGLY_CONNECTED = split_penalty.VERY_STRONGLY_CONNECTED
DOTTED_NAME = split_penalty.DOTTED_NAME
STRONGLY_CONNECTED = split_penalty.STRONGLY_CONNECTED
CONTIGUOUS_LIST = split_penalty.CONTIGUOUS_LIST
@@ -163,7 +164,9 @@
""")
tree = self._ParseAndComputePenalties(code)
self._CheckPenalties(tree, [
- ('a', None), ('=', None), ('{', None),
+ ('a', None),
+ ('=', None),
+ ('{', None),
("'x'", None),
(':', STRONGLY_CONNECTED),
('42', None),
@@ -174,12 +177,12 @@
('a', UNBREAKABLE),
(':', UNBREAKABLE),
('23', UNBREAKABLE),
- (')', UNBREAKABLE),
+ (')', VERY_STRONGLY_CONNECTED),
(':', STRONGLY_CONNECTED),
('37', None),
(',', None),
('}', None),
- ]) # yapf: disable
+ ])
# Test list comprehension.
code = textwrap.dedent(r"""
@@ -200,7 +203,7 @@
('==', STRONGLY_CONNECTED),
('37', STRONGLY_CONNECTED),
(']', STRONGLY_CONNECTED),
- ]) # yapf: disable
+ ])
def testFuncCalls(self):
code = 'foo(1, 2, 3)\n'
@@ -213,7 +216,8 @@
('2', CONTIGUOUS_LIST),
(',', CONTIGUOUS_LIST),
('3', CONTIGUOUS_LIST),
- (')', UNBREAKABLE)]) # yapf: disable
+ (')', VERY_STRONGLY_CONNECTED),
+ ])
# Now a method call, which has more than one trailer
code = 'foo.bar.baz(1, 2, 3)\n'
@@ -230,7 +234,8 @@
('2', CONTIGUOUS_LIST),
(',', CONTIGUOUS_LIST),
('3', CONTIGUOUS_LIST),
- (')', UNBREAKABLE)]) # yapf: disable
+ (')', VERY_STRONGLY_CONNECTED),
+ ])
if __name__ == '__main__':