Repository: google/yapf
Pull request #864 (2020-09-18 16:36:10)

Allow `lines` parameter to fix whitespace between statements.
The previous logic largely ignored `lines` values that were not part of
statements. This means that yapf required a statement to be formatted to
make whitespace-only changes.

This logic tweak allows `lines` to be taken into account even when the
adjacent statements are disabled (i.e. not in `lines`).

coveralls: 
[![Coverage Status](https://coveralls.io/builds/33573125/badge)](https://coveralls.io/builds/33573125)

Coverage increased (+0.001%) to 96.324% when pulling **032a57d9af67076d010cede5315c37779a890e4f on msuozzo:master** into **a3dacba6bf82e154e19bcf267d2e7ee9dc09d198 on google:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/33573125/badge)](https://coveralls.io/builds/33573125)

Coverage increased (+0.001%) to 96.324% when pulling **032a57d9af67076d010cede5315c37779a890e4f on msuozzo:master** into **a3dacba6bf82e154e19bcf267d2e7ee9dc09d198 on google:master**.

msuozzo: Coverage doesn't really look like it decreased. Could have just been the result of removing some code.

Patch:
diff --git a/yapf/yapflib/reformatter.py b/yapf/yapflib/reformatter.py
index 4ca799b..bfc7121 100644
--- a/yapf/yapflib/reformatter.py
+++ b/yapf/yapflib/reformatter.py
@@ -115,10 +115,6 @@ def _RetainHorizontalSpacing(uwline):
 
 def _RetainRequiredVerticalSpacing(cur_uwline, prev_uwline, lines):
   """Retain all vertical spacing between lines."""
-  if cur_uwline.disable and (not prev_uwline or prev_uwline.disable):
-    # If both lines are disabled we aren't allowed to reformat anything.
-    lines = set()
-
   prev_tok = None
   if prev_uwline is not None:
     prev_tok = prev_uwline.last
@@ -160,7 +156,7 @@ def _RetainRequiredVerticalSpacingBetweenTokens(cur_tok, prev_tok, lines):
   if cur_tok.is_comment and not prev_tok.is_comment:
     # Don't adjust between a comment and non-comment.
     pass
-  elif lines and (cur_lineno in lines or prev_lineno in lines):
+  elif lines and lines.intersection(range(prev_lineno, cur_lineno + 1)):
     desired_newlines = cur_tok.whitespace_prefix.count('\n')
     whitespace_lines = range(prev_lineno + 1, cur_lineno)
     deletable_lines = len(lines.intersection(whitespace_lines))


Test patch:
diff --git a/yapftests/yapf_test.py b/yapftests/yapf_test.py
index 23c4fca..e3e3df3 100644
--- a/yapftests/yapf_test.py
+++ b/yapftests/yapf_test.py
@@ -1016,6 +1016,29 @@ x = {
         expected_formatted_code,
         extra_options=['--lines', '4-7'])
 
+  def testRetainVerticalFormattingBetweenDisabledLines(self):
+    unformatted_code = textwrap.dedent("""\
+        class A(object):
+            def aaaaaaaaaaaaa(self):
+                pass
+
+
+            def bbbbbbbbbbbbb(self):  # 5
+                pass
+        """)
+    expected_formatted_code = textwrap.dedent("""\
+        class A(object):
+            def aaaaaaaaaaaaa(self):
+                pass
+
+            def bbbbbbbbbbbbb(self):  # 5
+                pass
+        """)
+    self.assertYapfReformats(
+        unformatted_code,
+        expected_formatted_code,
+        extra_options=['--lines', '4-4'])
+
   def testFormatLinesSpecifiedInMiddleOfExpression(self):
     unformatted_code = textwrap.dedent("""\
         class A(object):