Skip to content

Improved edit span for import #23083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ object CheckUnused:
else
// If the rest of the line is blank, include it in the final edit position. (Delete trailing whitespace.)
// If for deletion, and the prefix of the line is also blank, then include that, too. (Del blank line.)
// If deleting a blank line and surrounded by blank lines, remove an adjoining blank line.
def editPosAt(srcPos: SrcPos, forDeletion: Boolean): SrcPos =
val start = srcPos.span.start
val end = srcPos.span.end
Expand All @@ -714,7 +715,21 @@ object CheckUnused:
val bump = if (deleteLine) 1 else 0 // todo improve to include offset of next line, endline + 1
val p0 = srcPos.span
val p1 = if (next >= 0 && emptyRight) p0.withEnd(next + bump) else p0
val p2 = if (deleteLine) p1.withStart(prev + 1) else p1
val p2 =
if deleteLine then
var newStart = prev + 1
if srcPos.line > 1 then
val source = srcPos.sourcePos.source
import source.{lineToOffset, lineToOffsetOpt, offsetToLine}
val startLine = offsetToLine(start)
val endLine = offsetToLine(end)
val preceding = lineToOffset(startLine - 1)
lineToOffsetOpt(endLine + 2) match
case Some(succeeding) if lineToOffset(startLine) - preceding == 1 && succeeding - end == 2 =>
newStart = preceding
case _ =>
p1.withStart(newStart)
else p1
srcPos.sourcePos.withSpan(p2)
def actionsOf(actions: (SrcPos, String)*): List[CodeAction] =
val patches = actions.map((srcPos, replacement) => ActionPatch(srcPos.sourcePos, replacement)).toList
Expand Down
13 changes: 13 additions & 0 deletions tests/rewrites/unused.check
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,16 @@ package p11:
package p12:
import java.lang.System, java.lang.Runnable
class C extends Runnable { def run() = System.out.println() }

package p13:
import java.lang.{Runnable,

System}, System.out
class C extends Runnable { def run() = out.println() }

package p14:
import collection.mutable

import mutable.ListBuffer

def buf = ListBuffer.empty[String]
17 changes: 17 additions & 0 deletions tests/rewrites/unused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,20 @@ package p11:
package p12:
import collection.mutable, java.lang.System, java.lang.Runnable
class C extends Runnable { def run() = System.out.println() }

package p13:
import java.lang.{Runnable,

Thread, // leave one blank line instead of two

System}, System.out
class C extends Runnable { def run() = out.println() }

package p14:
import collection.mutable

import java.lang.{Runnable, Thread}

import mutable.ListBuffer

def buf = ListBuffer.empty[String]
Loading