Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDEA support annotation apply & constructor #55

Merged
merged 2 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions intellij-plugin/src/main/resources/patch/change.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<ul> 0.2.0:
<ol>
<li>support @builder, @log, @json, @apply</li>
</ol>
</ul>
<ul> 0.1.2:
<ol>
<li>First release, support @builder, @log, @json</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package io.github.dreamylost.plugin

import com.intellij.openapi.Disposable
import io.github.dreamylost.plugin.processor.Processor
import io.github.dreamylost.plugin.processor.clazz.{ BuilderProcessor, JsonProcessor, LogProcessor }
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ ScClass, ScObject, ScTypeDefinition }
import io.github.dreamylost.plugin.processor.clazz.{ApplyProcessor, BuilderProcessor, ConstructorProcessor, JsonProcessor, LogProcessor}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScClass, ScObject, ScTypeDefinition}

import scala.collection.mutable

Expand All @@ -18,6 +18,8 @@ class ScalaMacroProcessorProvider extends Disposable {
regist(ScalaMacroNames.BUILDER, new BuilderProcessor())
regist(ScalaMacroNames.LOG, new LogProcessor())
regist(ScalaMacroNames.JSON, new JsonProcessor())
regist(ScalaMacroNames.APPLY, new ApplyProcessor())
regist(ScalaMacroNames.CONSTRUCTOR, new ConstructorProcessor())

override def dispose(): Unit = {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.github.dreamylost.plugin.processor

import org.jetbrains.plugins.scala.lang.psi.api.base.ScMethodLike
import org.jetbrains.plugins.scala.lang.psi.api.statements.params.ScClassParameter
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScClass

/**
* Mail: chk19940609@gmail.com
* Created by IceMimosa
Expand All @@ -9,4 +13,21 @@ abstract class AbsProcessor extends Processor {

override def needCompanion: Boolean = false

/**
* get constructor parameters
*
* @return name and type
*/
protected def getConstructorParameters(clazz: ScClass, withSecond: Boolean = true): Seq[(String, String)] = {
val constructors = if (withSecond) {
clazz.constructors.map(Some(_))
} else {
Seq(clazz.constructor.map(_.asInstanceOf[ScMethodLike]))
}
constructors.flatten.flatMap(_.getParameterList.getParameters)
.collect {
case p: ScClassParameter =>
p.name -> p.`type`().toOption.map(_.toString).getOrElse("Unit")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.dreamylost.plugin.processor.clazz

import io.github.dreamylost.plugin.processor.ProcessType.ProcessType
import io.github.dreamylost.plugin.processor.{AbsProcessor, ProcessType}
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScClass, ScObject, ScTypeDefinition}

/**
* Desc: Processor for annotation apply
*
* Mail: chk19940609@gmail.com
* Created by IceMimosa
* Date: 2021/7/8
*/
class ApplyProcessor extends AbsProcessor {

override def needCompanion: Boolean = true

override def process(source: ScTypeDefinition, typ: ProcessType): Seq[String] = {
typ match {
case ProcessType.Method =>
source match {
case obj: ScObject =>
val clazz = obj.fakeCompanionClassOrCompanionClass.asInstanceOf[ScClass]
val nameAndTypes = getConstructorParameters(clazz, withSecond = false)
.map(o => s"${o._1}: ${o._2}")
.mkString(", ")
Seq(s"def apply($nameAndTypes): ${clazz.getName} = ???")
case _ => Nil
}
case _ => Nil
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import io.github.dreamylost.plugin.processor.{ AbsProcessor, ProcessType }
import io.github.dreamylost.plugin.processor.ProcessType.ProcessType
import org.jetbrains.plugins.scala.lang.psi.api.statements.params.ScClassParameter
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ ScClass, ScObject, ScTypeDefinition }
import org.jetbrains.plugins.scala.lang.psi.impl.toplevel.typedef.ScClassImpl

/**
* Desc: Processor for annotation builder
Expand Down Expand Up @@ -40,11 +39,7 @@ class BuilderProcessor extends AbsProcessor {
val clazz = obj.fakeCompanionClassOrCompanionClass
val className = clazz.getName
// support constructor and second constructor
val nameAndTypes = clazz.asInstanceOf[ScClassImpl].constructors.flatMap(_.getParameterList.getParameters)
.map {
case p: ScClassParameter =>
p.name -> p.`type`().toOption.getOrElse("Unit")
}
val nameAndTypes = getConstructorParameters(clazz.asInstanceOf[ScClass])
val assignMethods = nameAndTypes.map(term =>
s"def ${term._1}(${term._1}: ${term._2}): ${genBuilderName(className, returnType = true)} = ???"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.github.dreamylost.plugin.processor.clazz

import io.github.dreamylost.plugin.ScalaMacroNames
import io.github.dreamylost.plugin.processor.ProcessType.ProcessType
import io.github.dreamylost.plugin.processor.{AbsProcessor, ProcessType}
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScMethodCall
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScVariableDefinition
import org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.{ScClass, ScTypeDefinition}
import org.jetbrains.plugins.scala.lang.psi.types.ScLiteralType

/**
* Desc: Processor for annotation constructor
*
* Mail: chk19940609@gmail.com
* Created by IceMimosa
* Date: 2021/7/8
*/
class ConstructorProcessor extends AbsProcessor {

private val excludeFieldsName = "excludeFields"

override def needCompanion: Boolean = true

override def process(source: ScTypeDefinition, typ: ProcessType): Seq[String] = {
typ match {
case ProcessType.Method =>
source match {
case clazz: ScClass =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immortal code(神仙代码) 🙁

val consFields = getConstructorParameters(clazz, withSecond = false)
val excludeFields = clazz.annotations(ScalaMacroNames.CONSTRUCTOR).lastOption match {
case Some(an) =>
// get excludeFields function call
an.getParameterList.getAttributes.findLast(_.getAttributeName == excludeFieldsName).map(_.getDetachedValue)
.collect {
case call: ScMethodCall =>
// get call parameters
call.argumentExpressions.flatMap(_.`type`().toOption)
.collect {
case str: ScLiteralType => str.value.value.toString
}
.mkString(", ")
}.getOrElse("")
case None => ""
}
val varFields = clazz.extendsBlock.members
.collect {
// var, others: ScPatternDefinition, ScFunctionDefinition
case `var`: ScVariableDefinition => `var`
}
.flatMap { v =>
v.declaredNames.map(n => (n, v.`type`().toOption.map(_.toString).getOrElse("Unit")))
}
.filter(v => !excludeFields.contains(v._1))

val consFieldsStr = consFields.map(_._1).mkString(", ")
val allFieldsStr = (consFields ++ varFields).map(f => s"${f._1}: ${f._2}").mkString(", ")

Seq(s"def this($allFieldsStr) = this($consFieldsStr)")
case _ => Nil
}
case _ => Nil
}
}
}