com.blackbuild.klum.ast.gdsl.PolymorphicMethods.gdsl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of klum-ast-runtime Show documentation
Show all versions of klum-ast-runtime Show documentation
Runtime Library for KlumAST
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Stephan Pauxberger
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.blackbuild.klum.ast.gdsl
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiType
import com.intellij.psi.impl.source.PsiImmediateClassType
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression
/**
* This gdsl provides code completion and insights for polymorphic methods:
*
* component(XyComponent) {
* bla "x"
* }
*
* Normally, it can't be resolved, since DelegatesTo can only point to the Class of
* argument, but we need a delegation to the XyComponent._RW in this case.
*/
def ctx = context(scope:closureScope())
contributor(ctx) {
GrMethodCallExpression method = findContainingClosureMethod(place)
if (!method) return
GrReferenceExpression classArgument = findFirstClassArgumentOf(method)
if (!classArgument) return
PsiType type = getActualClassValueOf(classArgument)
if (!type) return
PsiClass rwClass = findClass(type.resolve().qualName + '._RW')
if (rwClass)
delegatesTo(rwClass)
}
private PsiType getActualClassValueOf(GrReferenceExpression classArgument) {
// This is rather complicated, but seems to be the only way
PsiImmediateClassType argumentType = classArgument.type as PsiImmediateClassType
return (argumentType.resolveGenerics().substitutor.substitutionMap.values() ?: null)?.first()
}
private GrReferenceExpression findFirstClassArgumentOf(GrMethodCallExpression method) {
return method.argumentList.allArguments.find {
it instanceof GrReferenceExpression && it.type instanceof PsiImmediateClassType
}
}
private GrMethodCallExpression findContainingClosureMethod(PsiElement candidate) {
while (candidate != null) {
if (candidate instanceof GrClosableBlock && candidate.context instanceof GrMethodCallExpression)
return candidate.context as GrMethodCallExpression
// short circuit
if (candidate instanceof PsiDirectory)
return null
candidate = candidate.context
}
return null
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy