From 7078302e6c84eace00ff19a0773bd8eb010dfb9a Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Wed, 3 Jul 2019 17:10:56 +0900 Subject: [PATCH] Light classes: Fix getTextOffset() for identifiers Before this commit, getTextOffset() and getTextRange() implementations were inconsistent. --- .../asJava/elements/KtLightIdentifier.kt | 1 + .../lightClasses/LightClassBehaviorTest.kt | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 idea/tests/org/jetbrains/kotlin/idea/lightClasses/LightClassBehaviorTest.kt diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightIdentifier.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightIdentifier.kt index d2a5293d9bf..bafefe82977 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightIdentifier.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightIdentifier.kt @@ -46,4 +46,5 @@ open class KtLightIdentifier( override fun getParent() = lightOwner override fun getContainingFile() = lightOwner.containingFile override fun getTextRange() = origin?.textRange ?: TextRange.EMPTY_RANGE + override fun getTextOffset(): Int = origin?.textOffset ?: -1 } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/lightClasses/LightClassBehaviorTest.kt b/idea/tests/org/jetbrains/kotlin/idea/lightClasses/LightClassBehaviorTest.kt new file mode 100644 index 00000000000..ad63fce8e89 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/lightClasses/LightClassBehaviorTest.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.lightClasses + +import com.intellij.psi.PsiMethod +import com.intellij.testFramework.LightProjectDescriptor +import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase +import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor +import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner +import org.junit.runner.RunWith + +@RunWith(JUnit3WithIdeaConfigurationRunner::class) +class LightClassBehaviorTest : KotlinLightCodeInsightFixtureTestCase() { + fun testIdentifierOffsets() { + myFixture.configureByText( + "test.kt", """ + class A { + fun foo() {} + } + """.trimIndent() + ) + + val aClass = myFixture.javaFacade.findClass("A")!! + val fooMethodName = (aClass.findMethodsByName("foo").single() as PsiMethod).nameIdentifier!! + + val offset = fooMethodName.textOffset + val range = fooMethodName.textRange + + assert(offset > 0) + assert(offset == range.startOffset) + } + + override fun getProjectDescriptor(): LightProjectDescriptor { + return KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE + } +} \ No newline at end of file