diff --git a/presentation-compiler/src/main/dotty/tools/pc/InferredMethodProvider.scala b/presentation-compiler/src/main/dotty/tools/pc/InferredMethodProvider.scala index e6f27781bc64..1bf2d20f5ee7 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/InferredMethodProvider.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/InferredMethodProvider.scala @@ -227,7 +227,25 @@ final class InferredMethodProvider( s"\n$indent$signature", ) ) ::: imports - case None => Nil + case None => + extensionMethodEdits(signature, container) + + def extensionMethodEdits(signature: String, container: Tree): List[TextEdit] = + val containerTypeStr = printType(container.tpe.widenDealias) + + val pos = insertPosition() + val indent = indentation(params.text(), pos.start - 1) + val extensionSignature = s"extension (x: $containerTypeStr)\n $indent$signature" + + val lspPos = pos.toLsp + lspPos.setEnd(lspPos.getStart()) + + List( + TextEdit( + lspPos, + s"$extensionSignature\n$indent", + ) + ) ::: imports path match /** diff --git a/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredMethodSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredMethodSuite.scala index 2b8e2ef32ef5..23abd4a7e5a4 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredMethodSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/edit/InsertInferredMethodSuite.scala @@ -495,6 +495,48 @@ class InsertInferredMethodSuite extends BaseCodeActionSuite: |""".stripMargin, ) + @Test def `extension-method` = + checkEdit( + """|object Main: + | val x = 1 + | x.<> + |""".stripMargin, + """|object Main: + | val x = 1 + | extension (x: Int) + | def incr = ??? + | x.incr + |""".stripMargin, + ) + + @Test def `extension-method-1` = + checkEdit( + """|object Main: + | val x = 1 + | x.<>(2) + |""".stripMargin, + """|object Main: + | val x = 1 + | extension (x: Int) + | def add(arg0: Int) = ??? + | x.add(2) + |""".stripMargin, + ) + + @Test def `extension-method-2` = + checkEdit( + """|object Main: + | val s = "hello" + | s.<> + |""".stripMargin, + """|object Main: + | val s = "hello" + | extension (x: String) + | def shout = ??? + | s.shout + |""".stripMargin, + ) + def checkEdit( original: String, expected: String