Skip to content

Commit 409c269

Browse files
committed
improvement: Suggest to add using as a code action
This should add the code action to both Metals and Intellij. I will try to go over the existing rewrites to see if we can add more actions easily. Fixes #23071
1 parent c4531d4 commit 409c269

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

compiler/src/dotty/tools/dotc/reporting/Message.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,17 @@ trait NoDisambiguation extends Message:
424424
withoutDisambiguation()
425425

426426
/** The fallback `Message` containing no explanation and having no `kind` */
427-
final class NoExplanation(msgFn: Context ?=> String)(using Context) extends Message(ErrorMessageID.NoExplanationID) {
427+
final class NoExplanation(msgFn: Context ?=> String, actions: List[CodeAction] = List.empty)(using Context) extends Message(ErrorMessageID.NoExplanationID) {
428428
def msg(using Context): String = msgFn
429429
def explain(using Context): String = ""
430430
val kind: MessageKind = MessageKind.NoKind
431431

432+
override def actions(using Context): List[CodeAction] = actions
433+
432434
override def toString(): String = msg
435+
436+
def withActions(actions: List[CodeAction]): NoExplanation =
437+
new NoExplanation(msgFn, actions)
433438
}
434439

435440
/** The extractor for `NoExplanation` can be used to check whether any error

compiler/src/dotty/tools/dotc/typer/Migrations.scala

+25-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import NameKinds.ContextBoundParamName
2121
import rewrites.Rewrites.patch
2222
import util.Spans.Span
2323
import rewrites.Rewrites
24+
import dotty.tools.dotc.rewrites.Rewrites.ActionPatch
25+
import dotty.tools.dotc.util.SourcePosition
2426

2527
/** A utility trait containing source-dependent deprecation messages
2628
* and migrations.
@@ -139,14 +141,31 @@ trait Migrations:
139141
val rewriteMsg =
140142
if hasParentheses then
141143
Message.rewriteNotice("This code", mversion.patchFrom)
142-
else
143-
""
144-
report.errorOrMigrationWarning(
144+
else ""
145+
val message =
145146
em"""Implicit parameters should be provided with a `using` clause.$rewriteMsg
146-
|To disable the warning, please use the following option:
147+
|To disable the warning, please use the following option:
147148
| "-Wconf:msg=Implicit parameters should be provided with a `using` clause:s"
148-
|""",
149-
pt.args.head.srcPos, mversion)
149+
|"""
150+
val withActions = message.withActions(
151+
List(
152+
CodeAction(
153+
title = "Add `using` clause",
154+
description = None,
155+
patches = List(
156+
ActionPatch(
157+
pt.args.head.startPos.sourcePos,
158+
"using "
159+
)
160+
)
161+
)
162+
)
163+
)
164+
report.errorOrMigrationWarning(
165+
withActions,
166+
pt.args.head.srcPos,
167+
mversion
168+
)
150169
if hasParentheses && mversion.needsPatch then
151170
patch(Span(pt.args.head.span.start), "using ")
152171
end implicitParams

compiler/test/dotty/tools/dotc/reporting/CodeActionTest.scala

+13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ class CodeActionTest extends DottyTest:
5454
|""".stripMargin
5555
)
5656

57+
@Test def addUsingClause =
58+
checkCodeAction(
59+
"""|object Test:
60+
| def foo(implicit a: Int) = a
61+
| foo(123)
62+
|""".stripMargin,
63+
"Add `using` clause",
64+
"""|object Test:
65+
| def foo(implicit a: Int) = a
66+
| foo(using 123)
67+
|""".stripMargin
68+
)
69+
5770
@Test def insertMissingCases =
5871
checkCodeAction(
5972
code =

0 commit comments

Comments
 (0)