Skip to content

Commit

Permalink
add test and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ag91 committed Nov 9, 2024
1 parent bec58e3 commit 57bd83e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,9 @@ object ServerCommands {
"insert-inferred-method",
"Insert inferred method",
"""|Try and create a method from the error symbol at the current position
|where that position points to a name of form for example:
|- `nonExisting(param)`
|- `obj.nonExisting()`
|where that position points to a name of form for example:
|- `nonExisting(param)`
|- `obj.nonExisting()`
|""".stripMargin,
"""|Object with `document` and `position`
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ final class InferredMethodProvider(

val tpe = methodParams(retIndex).tpe
tpe match {
// def method1(s : (String, Float) => Int) = 123
// method1(<<otherMethod>>)
case TypeRef(_, _, args) if definitions.isFunctionType(tpe) =>
val params = args.take(args.size - 1)
val paramsString =
Expand Down Expand Up @@ -462,12 +464,14 @@ final class InferredMethodProvider(
* `class Foo extends Bar {}` => retuning position would be right after the opening brace.
* `class Foo extends Bar` => retuning position would be right after `Bar`.
*
* @param text the text of the original source code.
* @param t the enclosing template for the class/object/trait we are implementing.
*/
private def inferEditPosition(t: Template): Position = {
// get text via reflection because Template could be in a different file
val text = t.pos.source.content.map(_.toString).mkString
// to get text via reflection because Template could be in a different file
// we decided to not support other files, but we may want to go back to it
//
// val text = t.pos.source.content.map(_.toString).mkString
val text = params.text()
hasBody(text, t)
.map { offset => t.pos.withStart(offset + 1).withEnd(offset + 1) }
.getOrElse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ class CreateNewSymbolLspSuite extends BaseCodeActionLspSuite("createNew") {
|""".stripMargin,
)

checkNewSymbol(
"infer-method",
"""| object X {
| <<otherMethod>>(1)
|}
|""".stripMargin,
s"""|${CreateNewSymbol.method("otherMethod")}""".stripMargin,
selectedActionIndex = 0,
pickedKind = "infer-method",
newFile = "a/src/main/scala/a/A.scala" ->
"""| object X {
| def otherMethod(arg0: Int) = ???
| otherMethod(1)
|}
|""".stripMargin,
)

checkNewSymbol(
"trait",
"""|package a
Expand Down

0 comments on commit 57bd83e

Please sign in to comment.