diff --git a/metals/src/main/scala/scala/meta/internal/metals/ServerCommands.scala b/metals/src/main/scala/scala/meta/internal/metals/ServerCommands.scala index 44f629b7236..58379faa258 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/ServerCommands.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/ServerCommands.scala @@ -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` | diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/InferredMethodProvider.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/InferredMethodProvider.scala index b68fc8a8a94..a33e5935cf8 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/pc/InferredMethodProvider.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/InferredMethodProvider.scala @@ -164,6 +164,8 @@ final class InferredMethodProvider( val tpe = methodParams(retIndex).tpe tpe match { + // def method1(s : (String, Float) => Int) = 123 + // method1(<>) case TypeRef(_, _, args) if definitions.isFunctionType(tpe) => val params = args.take(args.size - 1) val paramsString = @@ -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( diff --git a/tests/unit/src/test/scala/tests/codeactions/CreateNewSymbolLspSuite.scala b/tests/unit/src/test/scala/tests/codeactions/CreateNewSymbolLspSuite.scala index 67e0dc7527f..e56f680c647 100644 --- a/tests/unit/src/test/scala/tests/codeactions/CreateNewSymbolLspSuite.scala +++ b/tests/unit/src/test/scala/tests/codeactions/CreateNewSymbolLspSuite.scala @@ -33,6 +33,23 @@ class CreateNewSymbolLspSuite extends BaseCodeActionLspSuite("createNew") { |""".stripMargin, ) + checkNewSymbol( + "infer-method", + """| object X { + | <>(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