Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,48 @@ class InsertInferredMethodSuite extends BaseCodeActionSuite:
|""".stripMargin,
)

@Test def `extension-method` =
checkEdit(
"""|object Main:
| val x = 1
| x.<<incr>>
|""".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.<<add>>(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.<<shout>>
|""".stripMargin,
"""|object Main:
| val s = "hello"
| extension (x: String)
| def shout = ???
| s.shout
|""".stripMargin,
)

def checkEdit(
original: String,
expected: String
Expand Down
Loading