未验证 提交 67ee0bc3 编写于 作者: I IceMimosa 提交者: GitHub

IDEA plugin - `@apply` support generic & currying (#68)

上级 e1f34eae
...@@ -18,4 +18,4 @@ examples2-13/target/ ...@@ -18,4 +18,4 @@ examples2-13/target/
intellij-plugin/target/ intellij-plugin/target/
intellij-plugin/project/project/ intellij-plugin/project/project/
intellij-plugin/project/target/ intellij-plugin/project/target/
/.idea/ .run/
\ No newline at end of file
...@@ -19,15 +19,37 @@ abstract class AbsProcessor extends Processor { ...@@ -19,15 +19,37 @@ abstract class AbsProcessor extends Processor {
* @return name and type * @return name and type
*/ */
protected def getConstructorParameters(clazz: ScClass, withSecond: Boolean = true): Seq[(String, String)] = { protected def getConstructorParameters(clazz: ScClass, withSecond: Boolean = true): Seq[(String, String)] = {
this.getConstructorCurryingParameters(clazz, withSecond, withCurrying = false).head
}
/**
* get constructor parameters with currying
*
* @return
* if `withCurrying` = true, return (name: type, name: type)(name: type)...
* else return (name: type, name: type, name: type, ...)
*/
protected def getConstructorCurryingParameters(clazz: ScClass, withSecond: Boolean = true, withCurrying: Boolean = true): Seq[Seq[(String, String)]] = {
val constructors = if (withSecond) { val constructors = if (withSecond) {
clazz.constructors.map(Some(_)) clazz.constructors.map(Some(_))
} else { } else {
Seq(clazz.constructor.map(_.asInstanceOf[ScMethodLike])) Seq(clazz.constructor.map(_.asInstanceOf[ScMethodLike]))
} }
constructors.flatten.flatMap(_.getParameterList.getParameters) if (withCurrying) {
.collect { constructors.flatten.flatMap { c =>
case p: ScClassParameter => c.effectiveParameterClauses.map(_.effectiveParameters.collect {
p.name -> p.`type`().toOption.map(_.toString).getOrElse("Unit") case p: ScClassParameter =>
p.name -> p.`type`().toOption.map(_.toString).getOrElse("Unit")
})
} }
} else {
Seq(
constructors.flatten.flatMap(_.getParameterList.getParameters)
.collect {
case p: ScClassParameter =>
p.name -> p.`type`().toOption.map(_.toString).getOrElse("Unit")
}
)
}
} }
} }
...@@ -21,10 +21,11 @@ class ApplyProcessor extends AbsProcessor { ...@@ -21,10 +21,11 @@ class ApplyProcessor extends AbsProcessor {
source match { source match {
case obj: ScObject => case obj: ScObject =>
val clazz = obj.fakeCompanionClassOrCompanionClass.asInstanceOf[ScClass] val clazz = obj.fakeCompanionClassOrCompanionClass.asInstanceOf[ScClass]
val nameAndTypes = getConstructorParameters(clazz, withSecond = false) val nameAndTypes = getConstructorCurryingParameters(clazz, withSecond = false)
.map(o => s"${o._1}: ${o._2}") .map(_.map(o => s"${o._1}: ${o._2}").mkString("(", ", ", ")"))
.mkString(", ") .mkString
Seq(s"def apply($nameAndTypes): ${clazz.getName} = ???") val genericTypes = clazz.typeParamString
Seq(s"def apply$genericTypes$nameAndTypes: ${clazz.getName}$genericTypes = ???")
case _ => Nil case _ => Nil
} }
case _ => Nil case _ => Nil
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册