Skip to content

Commit 875ba1d

Browse files
authored
Merge pull request #403 from scala/backport-lts-3.3-23083
Backport "Improved edit span for import" to 3.3 LTS
2 parents cec5a3e + 79cc322 commit 875ba1d

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ object CheckUnused:
670670
else
671671
// If the rest of the line is blank, include it in the final edit position. (Delete trailing whitespace.)
672672
// If for deletion, and the prefix of the line is also blank, then include that, too. (Del blank line.)
673+
// If deleting a blank line and surrounded by blank lines, remove an adjoining blank line.
673674
def editPosAt(srcPos: SrcPos, forDeletion: Boolean): SrcPos =
674675
val start = srcPos.span.start
675676
val end = srcPos.span.end
@@ -682,7 +683,21 @@ object CheckUnused:
682683
val bump = if (deleteLine) 1 else 0 // todo improve to include offset of next line, endline + 1
683684
val p0 = srcPos.span
684685
val p1 = if (next >= 0 && emptyRight) p0.withEnd(next + bump) else p0
685-
val p2 = if (deleteLine) p1.withStart(prev + 1) else p1
686+
val p2 =
687+
if deleteLine then
688+
var newStart = prev + 1
689+
if srcPos.line > 1 then
690+
val source = srcPos.sourcePos.source
691+
import source.{lineToOffset, lineToOffsetOpt, offsetToLine}
692+
val startLine = offsetToLine(start)
693+
val endLine = offsetToLine(end)
694+
val preceding = lineToOffset(startLine - 1)
695+
lineToOffsetOpt(endLine + 2) match
696+
case Some(succeeding) if lineToOffset(startLine) - preceding == 1 && succeeding - end == 2 =>
697+
newStart = preceding
698+
case _ =>
699+
p1.withStart(newStart)
700+
else p1
686701
srcPos.sourcePos.withSpan(p2)
687702
def actionsOf(actions: (SrcPos, String)*): List[CodeAction] =
688703
val patches = actions.map((srcPos, replacement) => ActionPatch(srcPos.sourcePos, replacement)).toList

presentation-compiler/src/main/dotty/tools/pc/PcConvertToNamedLambdaParameters.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ final class PcConvertToNamedLambdaParameters(
3030
import PcConvertToNamedLambdaParameters._
3131

3232
def convertToNamedLambdaParameters: ju.List[l.TextEdit] = {
33-
val uri = params.uri
33+
val uri = params.uri.nn
3434
val filePath = Paths.get(uri)
3535
driver.run(
3636
uri,
37-
SourceFile.virtual(filePath.toString, params.text),
37+
SourceFile.virtual(filePath.toString, params.text.nn),
3838
)
3939
given newctx: Context = driver.localContext(params)
4040
val pos = driver.sourcePosition(params)

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]

tests/warn/i23033.scala

-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ object Test {
99
}
1010
object Useful:
1111
given [T](using @unused ec: ExecutionContext): AnyRef with {}
12-
object Syntax:
13-
given [T] => (@unused ec: ExecutionContext) => AnyRef

0 commit comments

Comments
 (0)