Commit 6302e7e5 authored by Jinseong Jeon's avatar Jinseong Jeon
Browse files

KT UAST: delegate textRange of local variable

parent 0fd44a8c
Showing with 45 additions and 0 deletions
+45 -0
......@@ -2,6 +2,7 @@
package org.jetbrains.uast.kotlin.psi
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.util.TextRange
import com.intellij.psi.*
import com.intellij.psi.impl.light.LightTypeElement
import org.jetbrains.annotations.ApiStatus
......@@ -57,6 +58,8 @@ class UastKotlinPsiVariable private constructor(
override fun getText(): String = ktElement.text
override fun getTextRange(): TextRange = ktElement.textRange
override fun getParent() = psiParent
override fun hasInitializer() = ktInitializer != null
......
......@@ -18,6 +18,7 @@ import com.intellij.psi.PsiAnnotation
import com.intellij.psi.PsiParameter
import com.intellij.psi.PsiTypes
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.uast.kotlin.BaseKotlinUastResolveProviderService
import org.jetbrains.uast.util.isConstructorCall
......@@ -1080,4 +1081,37 @@ interface UastApiFixtureTestBase : UastPluginSelection {
.orFail("cant convert to UCallExpression")
TestCase.assertEquals("Foo", uCallExpression.receiverType?.canonicalText)
}
fun checkTextRangeOfLocalVariable(myFixture: JavaCodeInsightTestFixture) {
myFixture.configureByText(
"main.kt", """
fun foo(p: Any) {
val bar = { arg ->
arg == p
}
boo(p = b<caret>ar)
}
fun boo(p: (Any) -> Boolean): Boolean {
return p.invoke(42)
}
""".trimIndent()
)
val nameReferenceExpression = myFixture.file.findElementAt(myFixture.caretOffset)
?.getParentOfType<KtNameReferenceExpression>(strict = true)
.orFail("Cannot find KtNameReferenceExpression")
val uNameReferenceExpression = nameReferenceExpression.toUElementOfType<USimpleNameReferenceExpression>()
.orFail("Cannot convert to KotlinUSimpleReferenceExpression")
val localPsiVariable = uNameReferenceExpression.resolve()
.orFail("Cannot find the local variable")
// val bar = ...
TestCase.assertNotNull(localPsiVariable.textRange)
// boo(p = bar)
TestCase.assertNotNull(uNameReferenceExpression.textRange)
TestCase.assertNotSame(localPsiVariable.textRange, uNameReferenceExpression.textRange)
}
}
\ No newline at end of file
......@@ -128,4 +128,8 @@ class FirUastApiFixtureTest : KotlinLightCodeInsightFixtureTestCase(), UastApiFi
fun testReceiverTypeOfExtensionFunction() {
checkReceiverTypeOfExtensionFunction(myFixture)
}
fun testTextRangeOfLocalVariable() {
checkTextRangeOfLocalVariable(myFixture)
}
}
\ No newline at end of file
......@@ -126,4 +126,8 @@ class FE1UastApiFixtureTest : KotlinLightCodeInsightFixtureTestCase(), UastApiFi
fun testReceiverTypeOfExtensionFunction() {
checkReceiverTypeOfExtensionFunction(myFixture)
}
fun testTextRangeOfLocalVariable() {
checkTextRangeOfLocalVariable(myFixture)
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment