idea-plugin / arrow.meta.ide.dsl.application / ApplicationSyntax / replaceAppService

replaceAppService

open fun <A : Any> MetaIde.replaceAppService(service: Class<A>, instance: (A?) -> A): ExtensionPhase

replaces an application service with instance. In this example this Annotator needs a different instance for MyService than what is currently provided - see our example in addAppService.

import arrow.meta.ide.MetaIde
import arrow.meta.ide.IdePlugin
import arrow.meta.ide.invoke
import arrow.meta.plugins.higherkind.isHigherKindedType
import com.intellij.lang.annotation.Annotator
import com.intellij.openapi.components.ServiceManager
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.utils.addToStdlib.safeAs

//sampleStart
val MetaIde.logAnnotator: IdePlugin
  get() = "Log Function Annotator" {
    meta(
      replaceAppService(MyService::class.java) { myOldService ->
        object : MyService {
          override fun printLn(ktclass: KtClass): Unit =
            println("Log ${ktclass.name}, which ${if (isHigherKindedType(ktclass)) "is" else "is not"} a Higher Kinded Type.")
        }
      },
      addAnnotator(
        annotator = Annotator { element, holder ->
          element.safeAs<KtClass>()?.let { f ->
            ServiceManager.getService(MyService::class.java)?.printLn(f)
          }
        }
      )
    )
  }

//sampleEnd
interface MyService {
  fun printLn(f: KtClass): Unit
}

The same technique applies for every service in the plugin dependencies.

See Also

AnnotatorSyntax

Do you like Arrow?

Arrow Org
<