Skip to content

Commit 0ae343f

Browse files
authored
Improved edit span for import (#23083)
Left less vertical space when deleting lines of import. Extracted from local branch of #22316
1 parent 453f94d commit 0ae343f

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+16-1
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ object CheckUnused:
702702
else
703703
// If the rest of the line is blank, include it in the final edit position. (Delete trailing whitespace.)
704704
// If for deletion, and the prefix of the line is also blank, then include that, too. (Del blank line.)
705+
// If deleting a blank line and surrounded by blank lines, remove an adjoining blank line.
705706
def editPosAt(srcPos: SrcPos, forDeletion: Boolean): SrcPos =
706707
val start = srcPos.span.start
707708
val end = srcPos.span.end
@@ -714,7 +715,21 @@ object CheckUnused:
714715
val bump = if (deleteLine) 1 else 0 // todo improve to include offset of next line, endline + 1
715716
val p0 = srcPos.span
716717
val p1 = if (next >= 0 && emptyRight) p0.withEnd(next + bump) else p0
717-
val p2 = if (deleteLine) p1.withStart(prev + 1) else p1
718+
val p2 =
719+
if deleteLine then
720+
var newStart = prev + 1
721+
if srcPos.line > 1 then
722+
val source = srcPos.sourcePos.source
723+
import source.{lineToOffset, lineToOffsetOpt, offsetToLine}
724+
val startLine = offsetToLine(start)
725+
val endLine = offsetToLine(end)
726+
val preceding = lineToOffset(startLine - 1)
727+
lineToOffsetOpt(endLine + 2) match
728+
case Some(succeeding) if lineToOffset(startLine) - preceding == 1 && succeeding - end == 2 =>
729+
newStart = preceding
730+
case _ =>
731+
p1.withStart(newStart)
732+
else p1
718733
srcPos.sourcePos.withSpan(p2)
719734
def actionsOf(actions: (SrcPos, String)*): List[CodeAction] =
720735
val patches = actions.map((srcPos, replacement) => ActionPatch(srcPos.sourcePos, replacement)).toList

tests/rewrites/unused.check

+13
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,16 @@ package p11:
5353
package p12:
5454
import java.lang.System, java.lang.Runnable
5555
class C extends Runnable { def run() = System.out.println() }
56+
57+
package p13:
58+
import java.lang.{Runnable,
59+
60+
System}, System.out
61+
class C extends Runnable { def run() = out.println() }
62+
63+
package p14:
64+
import collection.mutable
65+
66+
import mutable.ListBuffer
67+
68+
def buf = ListBuffer.empty[String]

tests/rewrites/unused.scala

+17
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,20 @@ package p11:
5959
package p12:
6060
import collection.mutable, java.lang.System, java.lang.Runnable
6161
class C extends Runnable { def run() = System.out.println() }
62+
63+
package p13:
64+
import java.lang.{Runnable,
65+
66+
Thread, // leave one blank line instead of two
67+
68+
System}, System.out
69+
class C extends Runnable { def run() = out.println() }
70+
71+
package p14:
72+
import collection.mutable
73+
74+
import java.lang.{Runnable, Thread}
75+
76+
import mutable.ListBuffer
77+
78+
def buf = ListBuffer.empty[String]

0 commit comments

Comments
 (0)