Skip to content

Commit

Permalink
fix issues with action for class methods and extra curly brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
ag91 committed Oct 29, 2024
1 parent 826cdc4 commit cc5b6e6
Showing 1 changed file with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand All @@ -321,15 +325,22 @@ final class InferredMethodProvider(
template
) =>
val insertPos: Position =
inferEditPosition(params.text, template)
inferEditPosition(template)

// val ret = prettyType(methodParams(retIndex).tpe)
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}"
},
Expand All @@ -341,15 +352,22 @@ final class InferredMethodProvider(
template
) =>
val insertPos: Position =
inferEditPosition(params.text, template)
inferEditPosition(template)

// val ret = prettyType(methodParams(retIndex).tpe)
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}"
},
Expand All @@ -366,7 +384,7 @@ final class InferredMethodProvider(
template
) =>
val insertPos: Position =
inferEditPosition(params.text, template)
inferEditPosition(template)

// val ret = prettyType(methodParams(retIndex).tpe)
signature(
Expand All @@ -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}"
},
Expand All @@ -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}"
},
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit cc5b6e6

Please sign in to comment.