Patchwork D9056: fix: include adjacent blank lines in ranges to be fixed

login
register
mail settings
Submitter phabricator
Date Sept. 18, 2020, 7:31 p.m.
Message ID <differential-rev-PHID-DREV-xofgdpk4ckpmsnwaelzk-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47231/
State New
Headers show

Comments

phabricator - Sept. 18, 2020, 7:31 p.m.
msuozzo created this revision.
msuozzo added a reviewer: hooper.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  When you remove a statement in python and the two adjacent lines are blank,
  this can create lint errors due to improper spacing. I'm sure this is also the
  case with other whitespace-aware languages and file formats. The current fix
  command skips all removal diffs and so doesn't trigger the auto-formatting of
  that whitespace.
  
  Net Cost:
  
  - Two extra line checks for all diffs and fix ranges that could be 1-2 lines longer.
  - One extra fix range generated for each pure-removal diff.
  
  Net Benefit:
  
  - Whitespace-aware languages are able to format and resolve whitespace errors.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9056

AFFECTED FILES
  hgext/fix.py
  tests/test-fix.t

CHANGE DETAILS




To: msuozzo, hooper, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-fix.t b/tests/test-fix.t
--- a/tests/test-fix.t
+++ b/tests/test-fix.t
@@ -432,22 +432,23 @@ 
 Test that incremental fixing works on files with additions, deletions, and
 changes in multiple line ranges. Note that deletions do not generally cause
 neighboring lines to be fixed, so we don't return a line range for purely
-deleted sections. In the future we should support a :deletion config that
-allows fixers to know where deletions are located.
+deleted sections except in the case of adjacent whitespace. In the future we
+should support a :deletion config that allows fixers to unconditionally know
+where deletions are located.
 
   $ hg init incrementalfixedlines
   $ cd incrementalfixedlines
 
-  $ printf "a\nb\nc\nd\ne\nf\ng\n" > foo.txt
+  $ printf "a\nb\nc\nd\ne\nf\ng\nh\n\ni\n\n" > foo.txt
   $ hg commit -Aqm "foo"
-  $ printf "zz\na\nc\ndd\nee\nff\nf\ngg\n" > foo.txt
+  $ printf "zz\na\nc\ndd\nee\nff\nf\ngg\nh\n\n\n" > foo.txt
 
   $ hg --config "fix.fail:command=echo" \
   >    --config "fix.fail:linerange={first}:{last}" \
   >    --config "fix.fail:pattern=foo.txt" \
   >    fix --working-dir
   $ cat foo.txt
-  1:1 4:6 8:8
+  1:1 4:6 8:8 10:11
 
   $ cd ..
 
diff --git a/hgext/fix.py b/hgext/fix.py
--- a/hgext/fix.py
+++ b/hgext/fix.py
@@ -598,10 +598,18 @@ 
     [(2, 4)]
     """
     ranges = []
-    for lines, kind in mdiff.allblocks(content1, content2):
+    lines2 = mdiff.splitnewlines(content2)
+    for lines, kind in mdiff.allblocks(content1, content2, lines2=lines2):
         firstline, lastline = lines[2:4]
-        if kind == b'!' and firstline != lastline:
-            ranges.append((firstline + 1, lastline))
+        # Produce a line range whenever the two sources differ and EITHER there
+        # is an addition/modification OR there is an adjacent blank line.
+        if kind == b'!' and (firstline != lastline or
+                             b'\n' in lines2[firstline - 1: lastline + 1]):
+            # Generate diffs for blank lines adjacent to the change.
+            ranges.append((
+                firstline + 1 - int(b'\n' in lines2[firstline - 1: firstline]),
+                lastline + int(b'\n' in lines2[lastline: lastline + 1]),
+            ))
     return ranges