idea-plugin / arrow.meta.ide.dsl.editor.hints / HintingSyntax / addParameterInfoHandlerForKotlin

addParameterInfoHandlerForKotlin

open fun <Type : DeclarationDescriptor, A> IdeMetaPlugin.addParameterInfoHandlerForKotlin(fetchTypeParameters: (descriptor: Type) -> List<TypeParameterDescriptor>, findParameterOwners: (argumentList: KtTypeArgumentList) -> Collection<Type>?, argumentListAllowedParentClasses: Set<Class<A>>): ExtensionPhase

registers a KotlinTypeArgumentInfoHandlerBase This extension is used for DeclarationDescriptor’s, the Owner is KtTypeArgumentList and the ActualType is KtTypeProjection. The following example provides Hints for ClassDescriptor from org.jetbrains.kotlin.idea.parameterInfo.KotlinClassTypeArgumentInfoHandler:

import arrow.meta.ide.IdePlugin
import arrow.meta.ide.IdeMetaPlugin
import arrow.meta.ide.invoke
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.idea.references.resolveMainReferenceToDescriptors
import org.jetbrains.kotlin.psi.KtTypeArgumentList
import org.jetbrains.kotlin.psi.KtUserType
import org.jetbrains.kotlin.utils.addToStdlib.safeAs

//sampleStart
val IdeMetaPlugin.parameterHints: IdePlugin
 get() =  "Hints for ClassDescriptor" {
  meta(
   addParameterInfoHandlerForKotlin(
    fetchTypeParameters = { descriptor: ClassDescriptor -> descriptor.typeConstructor.parameters },
    findParameterOwners = { argumentList: KtTypeArgumentList ->
     argumentList.parent?.safeAs<KtUserType>()?.referenceExpression?.resolveMainReferenceToDescriptors()?.mapNotNull { it.safeAs<ClassDescriptor>() }
    },
    argumentListAllowedParentClasses = setOf(KtUserType::class.java)
   )
  )
 }
//sampleEnd

See Also

addParameterInfoHandlerForKotlin

open fun <ArgumentList : KtElement, Argument : KtElement, A> IdeMetaPlugin.addParameterInfoHandlerForKotlin(argumentList: KClass<ArgumentList>, argument: KClass<Argument>, actualParameters: (o: ArgumentList) -> Array<Argument>, actualParametersRBraceType: IElementType, argumentListAllowedParentClasses: Set<Class<A>>, parameterIndex: (ctx: UpdateParameterInfoContext, argumentList: ArgumentList) -> Int = { ctx, list -> list.allChildren.takeWhile { it.startOffset < ctx.offset }.count { it.node.elementType == KtTokens.COMMA } }): ExtensionPhase

registers a KotlinParameterInfoWithCallHandlerBase This is used for FunctionDescriptor’s, the Owner is ArgumentList and the ActualType is Argument. Naturally, Type is a FunctionDescriptor. The following example is for KtLambdaArguments from org.jetbrains.kotlin.idea.parameterInfo.KotlinLambdaParameterInfoHandler:

import arrow.meta.ide.IdePlugin
import arrow.meta.ide.IdeMetaPlugin
import arrow.meta.ide.invoke
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtCallElement
import org.jetbrains.kotlin.psi.KtLambdaArgument
import org.jetbrains.kotlin.utils.addToStdlib.safeAs

//sampleStart
val IdeMetaPlugin.parameterHints: IdePlugin
 get() =  "Hints for FunctionDescriptor" {
  meta(
   addParameterInfoHandlerForKotlin(
    argument = KtLambdaArgument::class,
    argumentList = KtLambdaArgument::class,
    actualParameters = { arrayOf(it) },
    actualParametersRBraceType = KtTokens.RBRACE,
    argumentListAllowedParentClasses = setOf(KtLambdaArgument::class.java),
    parameterIndex = { _, argumentList ->
     argumentList.parent?.safeAs<KtCallElement>()?.valueArguments?.size?.dec() ?: 0
    }
   )
  )
 }
//sampleEnd

Parameters

parameterIndex - default count’s each occurrence of a KtTokens.COMMA

See Also

addParameterInfoHandlerForKotlin

KotlinParameterInfoWithCallHandlerBase

Do you like Arrow?

Arrow Org
<