Merge branch 'master' of https://github.com/google/yapf
diff --git a/yapf/yapflib/format_token.py b/yapf/yapflib/format_token.py
index 31fa953..04a398c 100644
--- a/yapf/yapflib/format_token.py
+++ b/yapf/yapflib/format_token.py
@@ -119,9 +119,12 @@
comment_lines = [s.lstrip() for s in self.value.splitlines()]
self._node.value = ('\n' + spaces_before).join(comment_lines)
- self.whitespace_prefix = (
- '\n' * (self.newlines or newlines_before) + spaces_before
- )
+ if not self.whitespace_prefix:
+ self.whitespace_prefix = (
+ '\n' * (self.newlines or newlines_before) + spaces_before
+ )
+ else:
+ self.whitespace_prefix += spaces_before
def AdjustNewlinesBefore(self, newlines_before):
"""Change the number of newlines before this token."""
@@ -141,9 +144,8 @@
prev_lineno += previous.value.count('\n')
if cur_lineno != prev_lineno:
- if not previous.is_continuation:
- self.spaces_required_before = (
- self.column - first_column + depth * style.Get('INDENT_WIDTH'))
+ self.spaces_required_before = (
+ self.column - first_column + depth * style.Get('INDENT_WIDTH'))
return
cur_column = self.node.column
@@ -166,6 +168,8 @@
@property
def value(self):
+ if self.is_continuation:
+ return self._node.value.rstrip()
return self._node.value
@property
diff --git a/yapf/yapflib/line_joiner.py b/yapf/yapflib/line_joiner.py
index 51d2b01..84346c2 100644
--- a/yapf/yapflib/line_joiner.py
+++ b/yapf/yapflib/line_joiner.py
@@ -106,4 +106,4 @@
if lines[1].last.total_length >= limit:
# Don't merge lines if the result goes over the column limit.
return False
- return True
+ return style.Get('JOIN_MULTIPLE_LINES')
diff --git a/yapf/yapflib/reformatter.py b/yapf/yapflib/reformatter.py
index 105b87c..c9b13b4 100644
--- a/yapf/yapflib/reformatter.py
+++ b/yapf/yapflib/reformatter.py
@@ -64,12 +64,12 @@
if prev_uwline and prev_uwline.disable:
# Keep the vertical spacing between a disabled and enabled formatting
# region.
- _RetainVerticalSpacing(prev_uwline, uwline)
+ _RetainVerticalSpacing(uwline, prev_uwline)
if (_LineContainsI18n(uwline) or uwline.disable or
_LineHasContinuationMarkers(uwline)):
_RetainHorizontalSpacing(uwline)
- _RetainVerticalSpacing(prev_uwline, uwline)
+ _RetainVerticalSpacing(uwline, prev_uwline)
_EmitLineUnformatted(state)
elif _CanPlaceOnSingleLine(uwline):
# The unwrapped line fits on one line.
@@ -81,7 +81,7 @@
# it as is.
state = format_decision_state.FormatDecisionState(uwline, indent_amt)
_RetainHorizontalSpacing(uwline)
- _RetainVerticalSpacing(prev_uwline, uwline)
+ _RetainVerticalSpacing(uwline, prev_uwline)
_EmitLineUnformatted(state)
final_lines.append(uwline)
@@ -108,20 +108,27 @@
tok.RetainHorizontalSpacing(uwline.first.column, uwline.depth)
-def _RetainVerticalSpacing(prev_uwline, cur_uwline):
- """Retain all vertical spacing between lines."""
- if not prev_uwline:
- return
- if prev_uwline.last.is_string:
- prev_lineno = prev_uwline.last.lineno + prev_uwline.last.value.count('\n')
- else:
- prev_lineno = prev_uwline.last.lineno
- if cur_uwline.first.is_comment:
- cur_lineno = cur_uwline.first.lineno - cur_uwline.first.value.count('\n')
- else:
- cur_lineno = cur_uwline.first.lineno
- num_newlines = cur_lineno - prev_lineno
- cur_uwline.first.AdjustNewlinesBefore(num_newlines)
+def _RetainVerticalSpacing(cur_uwline, prev_uwline):
+ prev_tok = None
+ if prev_uwline is not None:
+ prev_tok = prev_uwline.last
+ cur_lineno = 0
+ prev_lineno = 0
+ for cur_tok in cur_uwline.tokens:
+ if prev_tok is not None:
+ if prev_tok.is_string:
+ prev_lineno = prev_tok.lineno + prev_tok.value.count('\n')
+ else:
+ prev_lineno = prev_tok.lineno
+
+ if cur_tok.is_comment:
+ cur_lineno = cur_tok.lineno - cur_tok.value.count('\n')
+ else:
+ cur_lineno = cur_tok.lineno
+
+ num_newlines = cur_lineno - prev_lineno
+ cur_tok.AdjustNewlinesBefore(num_newlines)
+ prev_tok = cur_tok
def _EmitLineUnformatted(state):
diff --git a/yapf/yapflib/style.py b/yapf/yapflib/style.py
index 65b8a7f..16bf0f1 100644
--- a/yapf/yapflib/style.py
+++ b/yapf/yapflib/style.py
@@ -69,6 +69,8 @@
# <------ this blank line
def method():
..."""),
+ JOIN_MULTIPLE_LINES=
+ "Join short lines into one line. E.g., single line 'if' statements.",
SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=textwrap.dedent("""\
Insert a space between the ending comma and closing bracket of a list,
etc."""),
@@ -102,6 +104,7 @@
INDENT_WIDTH=4,
CONTINUATION_INDENT_WIDTH=4,
BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=False,
+ JOIN_MULTIPLE_LINES=True,
SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=True,
SPACES_BEFORE_COMMENT=2,
SPLIT_BEFORE_LOGICAL_OPERATOR=False,
@@ -130,6 +133,7 @@
style = CreateGoogleStyle()
style['INDENT_IF_EXPR_CONTINUATION'] = 0
style['INDENT_WIDTH'] = 2
+ style['JOIN_MULTIPLE_LINES'] = False
return style
@@ -166,6 +170,7 @@
INDENT_WIDTH=int,
CONTINUATION_INDENT_WIDTH=int,
BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=_BoolConverter,
+ JOIN_MULTIPLE_LINES=_BoolConverter,
SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=_BoolConverter,
SPACES_BEFORE_COMMENT=int,
SPLIT_BEFORE_LOGICAL_OPERATOR=_BoolConverter,
diff --git a/yapftests/line_joiner_test.py b/yapftests/line_joiner_test.py
index aabf388..7cd4f2c 100644
--- a/yapftests/line_joiner_test.py
+++ b/yapftests/line_joiner_test.py
@@ -20,10 +20,15 @@
from yapf.yapflib import line_joiner
from yapf.yapflib import pytree_unwrapper
from yapf.yapflib import pytree_utils
+from yapf.yapflib import style
class LineJoinerTest(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ style.SetGlobalStyle(style.CreatePEP8Style())
+
def _ParseAndUnwrap(self, code):
"""Produces unwrapped lines from the given code.
diff --git a/yapftests/reformatter_test.py b/yapftests/reformatter_test.py
index 01de0d8..950dad8 100644
--- a/yapftests/reformatter_test.py
+++ b/yapftests/reformatter_test.py
@@ -873,15 +873,6 @@
uwlines = _ParseAndUnwrap(code)
self.assertCodeEqual(code, reformatter.Reformat(uwlines))
- def testSingleLineIfStatements(self):
- code = textwrap.dedent("""\
- if True: a = 42
- elif False: b = 42
- else: c = 42
- """)
- uwlines = _ParseAndUnwrap(code)
- self.assertCodeEqual(code, reformatter.Reformat(uwlines))
-
def testLineDepthOfSingleLineStatement(self):
unformatted_code = textwrap.dedent("""\
while True: continue
@@ -1505,14 +1496,18 @@
def testB19372573(self):
code = textwrap.dedent("""\
def f():
- if a: return 42
- while True:
- if b: continue
- if c: break
- return 0
+ if a: return 42
+ while True:
+ if b: continue
+ if c: break
+ return 0
""")
uwlines = _ParseAndUnwrap(code)
- self.assertCodeEqual(code, reformatter.Reformat(uwlines))
+ try:
+ style.SetGlobalStyle(style.CreatePEP8Style())
+ self.assertCodeEqual(code, reformatter.Reformat(uwlines))
+ finally:
+ style.SetGlobalStyle(style.CreateChromiumStyle())
def testB19353268(self):
code = textwrap.dedent("""\
@@ -1969,6 +1964,15 @@
uwlines = _ParseAndUnwrap(unformatted_code)
self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines))
+ def testSingleLineIfStatements(self):
+ code = textwrap.dedent("""\
+ if True: a = 42
+ elif False: b = 42
+ else: c = 42
+ """)
+ uwlines = _ParseAndUnwrap(code)
+ self.assertCodeEqual(code, reformatter.Reformat(uwlines))
+
def testNoBlankBetweenClassAndDef(self):
unformatted_code = textwrap.dedent("""\
class Foo:
diff --git a/yapftests/yapf_test.py b/yapftests/yapf_test.py
index 88ac024..6425370 100644
--- a/yapftests/yapf_test.py
+++ b/yapftests/yapf_test.py
@@ -34,8 +34,7 @@
class FormatCodeTest(unittest.TestCase):
def _Check(self, unformatted_code, expected_formatted_code):
- formatted_code = yapf_api.FormatCode(unformatted_code,
- style_config='chromium')
+ formatted_code = yapf_api.FormatCode(unformatted_code)
self.assertEqual(expected_formatted_code, formatted_code)
def testSimple(self):
@@ -704,6 +703,44 @@
self.assertYapfReformats(unformatted_code, expected_formatted_code,
extra_options=['--lines', '1-10'])
+ def testDisableFormattingInDataLiteral(self):
+ unformatted_code = textwrap.dedent(u"""\
+ def horrible():
+ oh_god()
+ why_would_you()
+ [
+ 'do',
+
+ 'that',
+ ]
+
+ def still_horrible():
+ oh_god()
+ why_would_you()
+ [
+ 'do',
+
+ 'that',
+ ]
+ """)
+ expected_formatted_code = textwrap.dedent(u"""\
+ def horrible():
+ oh_god()
+ why_would_you()
+ [
+ 'do',
+
+ 'that',
+ ]
+
+ def still_horrible():
+ oh_god()
+ why_would_you()
+ ['do', 'that', ]
+ """)
+ self.assertYapfReformats(unformatted_code, expected_formatted_code,
+ extra_options=['--lines', '10-18'])
+
class BadInputTest(unittest.TestCase):
"""Test yapf's behaviour when passed bad input."""