Skip to content

Commit

Permalink
Merge pull request #10681 from som-snytt/issue/12940-rewritten-msg
Browse files Browse the repository at this point in the history
Announce quickfix only if there were edits
  • Loading branch information
lrytz committed Feb 6, 2024
2 parents 521cb82 + aaa6a8f commit 75f762d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
20 changes: 10 additions & 10 deletions src/compiler/scala/tools/nsc/Reporting.scala
Expand Up @@ -86,23 +86,23 @@ trait Reporting extends internal.Reporting { self: ast.Positions with Compilatio
private val skipRewriteAction = Set(Action.WarningSummary, Action.InfoSummary, Action.Silent)

private def registerTextEdit(m: Message): Boolean =
if (quickfixFilters.exists(f => f.matches(m))) {
textEdits.addAll(m.actions.flatMap(_.edits))
m.actions.exists(_.edits.nonEmpty) &&
quickfixFilters.exists(_.matches(m)) && {
m.actions.foreach(action => textEdits.addAll(action.edits))
true
}
else false

private def registerErrorTextEdit(pos: Position, msg: String, actions: List[CodeAction]): Boolean = {
val matches = quickfixFilters.exists({
private def registerErrorTextEdit(pos: Position, msg: String, actions: List[CodeAction]): Boolean =
actions.exists(_.edits.nonEmpty) &&
quickfixFilters.exists {
case MessageFilter.Any => true
case mp: MessageFilter.MessagePattern => mp.check(msg)
case sp: MessageFilter.SourcePattern => sp.check(pos)
case _ => false
})
if (matches)
textEdits.addAll(actions.flatMap(_.edits))
matches
}
} && {
actions.foreach(action => textEdits.addAll(action.edits))
true
}

private val summarizedWarnings: mutable.Map[WarningCategory, mutable.LinkedHashMap[Position, Message]] = mutable.HashMap.empty
private val summarizedInfos: mutable.Map[WarningCategory, mutable.LinkedHashMap[Position, Message]] = mutable.HashMap.empty
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/typechecker/Namers.scala
Expand Up @@ -1122,7 +1122,7 @@ trait Namers extends MethodSynthesis {
val leg = legacy.toString
val help = if (pts != leg) s" instead of $leg" else ""
val msg = s"under -Xsource:3-cross, the inferred type changes to $pts$help"
val src = currentUnit.source
val src = tree.pos.source
val pos = {
val eql = src.indexWhere(_ == '=', start = tree.rhs.pos.start, step = -1)
val declEnd = src.indexWhere(!_.isWhitespace, start = eql - 1, step = -1) + 1
Expand Down
11 changes: 10 additions & 1 deletion test/junit/scala/tools/nsc/QuickfixTest.scala
Expand Up @@ -10,15 +10,19 @@ import scala.tools.testkit.BytecodeTesting
import scala.tools.testkit.ReleasablePath._
import scala.util.Using

import reporters.StoreReporter

class QuickfixTest extends BytecodeTesting {
def testQuickfix(a: String, b: String, args: String): Unit = if (!scala.util.Properties.isWin) {
def testQuickfix(a: String, b: String, args: String, checkInfo: StoreReporter.Info => Boolean = _ => true): Unit = if (!scala.util.Properties.isWin) {
Using.resource(Files.createTempFile("unitSource", "scala")) { src =>
Files.write(src, a.getBytes)
val c = BytecodeTesting.newCompiler(extraArgs = args)
val r = c.newRun()
val f = AbstractFile.getFile(src.toFile.getAbsolutePath)
r.compileSources(List(new BatchSourceFile(f)))
assertEquals(b, new String(Files.readAllBytes(src)))
for (info <- c.global.reporter.asInstanceOf[StoreReporter].infos)
assert(checkInfo(info), info)
}
}

Expand Down Expand Up @@ -81,4 +85,9 @@ class QuickfixTest extends BytecodeTesting {
testQuickfix(c, c, "-quickfix:cat=deprecation") // without -deprecation, the warning is not shown and not rewritten
testQuickfix(c, d, "-deprecation -quickfix:cat=deprecation")
}

@Test def `do not lie about fixing`: Unit = {
val a = "import foo.bar"
testQuickfix(a, a, "-quickfix:any", !_.msg.contains("[rewritten by -quickfix]"))
}
}

0 comments on commit 75f762d

Please sign in to comment.