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 4390ea9e861..a9a6a8c7e92 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 @@ -96,7 +96,7 @@ final class InferredMethodProvider( val lastApplyPos = position.getOrElse(insertPosition()) val fileUri = scala.util - .Try(lastApplyPos.source.toString()) + .Try(lastApplyPos.source.toString()) // in tests this is undefined .toOption .getOrElse(params.uri().toString()) val indentString = @@ -303,7 +303,11 @@ final class InferredMethodProvider( // we need to get the type of the container of our undefined method val containerSymbol = context.lookupSymbol(container, _ => true) if (containerSymbol.isSuccess) { - containerSymbol.symbol.tpe match { + (if (containerSymbol.symbol.tpe.isInstanceOf[NullaryMethodType]) + containerSymbol.symbol.tpe + .asInstanceOf[NullaryMethodType] + .resultType + else containerSymbol.symbol.tpe) match { case TypeRef(_, classSymbol, _) => // we get the position of the container // class because we want to add the method @@ -321,7 +325,7 @@ final class InferredMethodProvider( template ) => val insertPos: Position = - inferEditPosition(params.text, template) + inferEditPosition(template) // val ret = prettyType(methodParams(retIndex).tpe) signature( @@ -329,7 +333,14 @@ final class InferredMethodProvider( paramsString = argumentsString(arguments).getOrElse(""), retType = None, postProcess = method => { - if (hasBody(params.text, template).isDefined) + if ( + hasBody( + template.pos.source.content + .map(_.toString) + .mkString, + template + ).isDefined + ) s"\n $method" else s" {\n $method}" }, @@ -341,7 +352,7 @@ final class InferredMethodProvider( template ) => val insertPos: Position = - inferEditPosition(params.text, template) + inferEditPosition(template) // val ret = prettyType(methodParams(retIndex).tpe) signature( @@ -349,7 +360,14 @@ final class InferredMethodProvider( paramsString = argumentsString(arguments).getOrElse(""), retType = None, postProcess = method => { - if (hasBody(params.text, template).isDefined) + if ( + hasBody( + template.pos.source.content + .map(_.toString) + .mkString, + template + ).isDefined + ) s"\n $method" else s" {\n $method}" }, @@ -366,7 +384,7 @@ final class InferredMethodProvider( template ) => val insertPos: Position = - inferEditPosition(params.text, template) + inferEditPosition(template) // val ret = prettyType(methodParams(retIndex).tpe) signature( @@ -375,7 +393,14 @@ final class InferredMethodProvider( argumentsString(arguments).getOrElse(""), retType = None, postProcess = method => { - if (hasBody(params.text, template).isDefined) + if ( + hasBody( + template.pos.source.content + .map(_.toString) + .mkString, + template + ).isDefined + ) s"\n $method" else s" {\n $method}" }, @@ -387,14 +412,21 @@ final class InferredMethodProvider( template ) => val insertPos: Position = - inferEditPosition(params.text, template) + inferEditPosition(template) signature( name = errorMethod.name.toString(), paramsString = argumentsString(arguments).getOrElse(""), retType = None, postProcess = method => { - if (hasBody(params.text, template).isDefined) + if ( + hasBody( + template.pos.source.content + .map(_.toString) + .mkString, + template + ).isDefined + ) s"\n $method" else s" {\n $method}" }, @@ -457,7 +489,9 @@ final class InferredMethodProvider( * @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(text: String, t: Template): Position = { + 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 hasBody(text, t) .map { offset => t.pos.withStart(offset + 1).withEnd(offset + 1) } .getOrElse(