ShortenReferences: cleanup code & add tests for property-function conflicts

This commit is contained in:
Alexander Podkhalyuzin
2019-09-20 14:07:10 -07:00
committed by Dmitry Gridin
parent 917a7f572d
commit 9a2da67919
9 changed files with 87 additions and 41 deletions
@@ -0,0 +1,7 @@
package a
class Goo {
fun x() {
foo<caret>
}
}
@@ -0,0 +1,5 @@
package a.b
fun foo(i: Int) {}
internal val Foo.foo: Int get() = 123
class Foo {}
@@ -0,0 +1,9 @@
package a
import a.b.foo
class Goo {
fun x() {
foo()
}
}
@@ -69,6 +69,10 @@ class CompletionMultiFileHandlerTest : KotlinCompletionTestCase() {
doTest() doTest()
} }
fun testPropertyFunctionConflict() {
doTest()
}
fun testExclCharInsertImport() { fun testExclCharInsertImport() {
doTest('!') doTest('!')
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2000-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. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -37,7 +37,6 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue
import org.jetbrains.kotlin.resolve.scopes.utils.findFirstClassifierWithDeprecationStatus import org.jetbrains.kotlin.resolve.scopes.utils.findFirstClassifierWithDeprecationStatus
import org.jetbrains.kotlin.resolve.scopes.utils.findPackage import org.jetbrains.kotlin.resolve.scopes.utils.findPackage
import org.jetbrains.kotlin.resolve.source.getPsi import org.jetbrains.kotlin.resolve.source.getPsi
import java.util.*
class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT }) { class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT }) {
data class Options( data class Options(
@@ -393,40 +392,41 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
} }
} }
override fun analyzeQualifiedElement(element: KtUserType, bindingContext: BindingContext): AnalyzeQualifiedElementResult = override fun analyzeQualifiedElement(element: KtUserType, bindingContext: BindingContext): AnalyzeQualifiedElementResult {
mainAnalyzeQualifiedElement(element, bindingContext).let { fun eval(element: KtUserType, bindingContext: BindingContext): AnalyzeQualifiedElementResult {
if (it is AnalyzeQualifiedElementResult.Skip && element.qualifier?.text == ROOT_PREFIX_FOR_IDE_RESOLUTION_MODE) if (element.qualifier == null) return AnalyzeQualifiedElementResult.Skip
AnalyzeQualifiedElementResult.ShortenNow val referenceExpression = element.referenceExpression ?: return AnalyzeQualifiedElementResult.Skip
else
it val target = referenceExpression.targets(bindingContext).singleOrNull()
?: return AnalyzeQualifiedElementResult.Skip
val scope = element.getResolutionScope(bindingContext, resolutionFacade)
val name = target.name
val targetByName: DeclarationDescriptor?
val isDeprecated: Boolean
if (target is ClassifierDescriptor) {
val classifierWithDeprecation = scope.findFirstClassifierWithDeprecationStatus(name, NoLookupLocation.FROM_IDE)
targetByName = classifierWithDeprecation?.descriptor
isDeprecated = classifierWithDeprecation?.isDeprecated ?: false
} else {
targetByName = scope.findPackage(name)
isDeprecated = false
}
val canShortenNow = targetByName?.asString() == target.asString() && !isDeprecated
return if (canShortenNow) AnalyzeQualifiedElementResult.ShortenNow else AnalyzeQualifiedElementResult.ImportDescriptors(
listOfNotNull(target)
)
} }
private fun mainAnalyzeQualifiedElement(element: KtUserType, bindingContext: BindingContext): AnalyzeQualifiedElementResult { val result = eval(element, bindingContext)
if (element.qualifier == null) return AnalyzeQualifiedElementResult.Skip if (result is AnalyzeQualifiedElementResult.Skip &&
val referenceExpression = element.referenceExpression ?: return AnalyzeQualifiedElementResult.Skip element.qualifier?.text == ROOT_PREFIX_FOR_IDE_RESOLUTION_MODE
) return AnalyzeQualifiedElementResult.ShortenNow
val target = referenceExpression.targets(bindingContext).singleOrNull() return result
?: return AnalyzeQualifiedElementResult.Skip
val scope = element.getResolutionScope(bindingContext, resolutionFacade)
val name = target.name
val targetByName: DeclarationDescriptor?
val isDeprecated: Boolean
if (target is ClassifierDescriptor) {
val classifierWithDeprecation = scope.findFirstClassifierWithDeprecationStatus(name, NoLookupLocation.FROM_IDE)
targetByName = classifierWithDeprecation?.descriptor
isDeprecated = classifierWithDeprecation?.isDeprecated ?: false
} else {
targetByName = scope.findPackage(name)
isDeprecated = false
}
val canShortenNow = targetByName?.asString() == target.asString() && !isDeprecated
return if (canShortenNow) AnalyzeQualifiedElementResult.ShortenNow else AnalyzeQualifiedElementResult.ImportDescriptors(
listOfNotNull(target)
)
} }
override fun shortenElement(element: KtUserType, options: Options): KtElement { override fun shortenElement(element: KtUserType, options: Options): KtElement {
@@ -486,15 +486,9 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
override fun analyzeQualifiedElement( override fun analyzeQualifiedElement(
element: KtDotQualifiedExpression, element: KtDotQualifiedExpression,
bindingContext: BindingContext bindingContext: BindingContext
): AnalyzeQualifiedElementResult = if (element.receiverExpression.text == ROOT_PREFIX_FOR_IDE_RESOLUTION_MODE)
AnalyzeQualifiedElementResult.ShortenNow
else
mainAnalyzeQualifiedElement(element, bindingContext)
private fun mainAnalyzeQualifiedElement(
element: KtDotQualifiedExpression,
bindingContext: BindingContext
): AnalyzeQualifiedElementResult { ): AnalyzeQualifiedElementResult {
if (element.receiverExpression.text == ROOT_PREFIX_FOR_IDE_RESOLUTION_MODE) return AnalyzeQualifiedElementResult.ShortenNow
if (PsiTreeUtil.getParentOfType( if (PsiTreeUtil.getParentOfType(
element, element,
KtImportDirective::class.java, KtPackageDirective::class.java KtImportDirective::class.java, KtPackageDirective::class.java
@@ -0,0 +1,6 @@
package a.b
fun foo(s: String) {}
fun foo(i: Int) {}
internal val Foo.foo: Int get() = 123
class Foo {}
+7
View File
@@ -0,0 +1,7 @@
package a
class Goo {
fun x() {
<selection>_Qfadj4tPV.a.b.foo()</selection>
}
}
@@ -0,0 +1,9 @@
package a
import a.b.foo
class Goo {
fun x() {
foo()
}
}
@@ -124,6 +124,11 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest {
runTest("idea/testData/shortenRefs/PackagesToUseStarImports.kt"); runTest("idea/testData/shortenRefs/PackagesToUseStarImports.kt");
} }
@TestMetadata("PropertyFunctionConflict.kt")
public void testPropertyFunctionConflict() throws Exception {
runTest("idea/testData/shortenRefs/PropertyFunctionConflict.kt");
}
@TestMetadata("removeCompanionRefInCalleeExpression.kt") @TestMetadata("removeCompanionRefInCalleeExpression.kt")
public void testRemoveCompanionRefInCalleeExpression() throws Exception { public void testRemoveCompanionRefInCalleeExpression() throws Exception {
runTest("idea/testData/shortenRefs/removeCompanionRefInCalleeExpression.kt"); runTest("idea/testData/shortenRefs/removeCompanionRefInCalleeExpression.kt");