Handle java ctor case for Introduce import alias inspection
#KT-35824 #EA-218654 Fixed
This commit is contained in:
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.generators.tests
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.AbstractDataFlowValueRenderingTest
|
import org.jetbrains.kotlin.AbstractDataFlowValueRenderingTest
|
||||||
import org.jetbrains.kotlin.addImport.AbstractAddImportTest
|
import org.jetbrains.kotlin.addImport.AbstractAddImportTest
|
||||||
|
import org.jetbrains.kotlin.addImportAlias.AbstractAddImportAliasTest
|
||||||
import org.jetbrains.kotlin.allopen.AbstractBytecodeListingTestForAllOpen
|
import org.jetbrains.kotlin.allopen.AbstractBytecodeListingTestForAllOpen
|
||||||
import org.jetbrains.kotlin.android.parcel.AbstractParcelBytecodeListingTest
|
import org.jetbrains.kotlin.android.parcel.AbstractParcelBytecodeListingTest
|
||||||
import org.jetbrains.kotlin.android.synthetic.test.AbstractAndroidBoxTest
|
import org.jetbrains.kotlin.android.synthetic.test.AbstractAndroidBoxTest
|
||||||
@@ -692,6 +693,10 @@ fun main(args: Array<String>) {
|
|||||||
model("addImport", pattern = KT_WITHOUT_DOTS_IN_NAME)
|
model("addImport", pattern = KT_WITHOUT_DOTS_IN_NAME)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testClass<AbstractAddImportAliasTest> {
|
||||||
|
model("addImportAlias", pattern = KT_WITHOUT_DOTS_IN_NAME)
|
||||||
|
}
|
||||||
|
|
||||||
testClass<AbstractSmartSelectionTest> {
|
testClass<AbstractSmartSelectionTest> {
|
||||||
model("smartSelection", testMethod = "doTestSmartSelection", pattern = KT_WITHOUT_DOTS_IN_NAME)
|
model("smartSelection", testMethod = "doTestSmartSelection", pattern = KT_WITHOUT_DOTS_IN_NAME)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,7 +117,11 @@ fun PsiReference.matchesTarget(candidateTarget: PsiElement): Boolean {
|
|||||||
val importedFqName = importDirective.importedFqName ?: return false
|
val importedFqName = importDirective.importedFqName ?: return false
|
||||||
val importedDescriptors = importDirective.containingKtFile.resolveImportReference(importedFqName)
|
val importedDescriptors = importDirective.containingKtFile.resolveImportReference(importedFqName)
|
||||||
val importableTargets = unwrappedTargets.mapNotNull {
|
val importableTargets = unwrappedTargets.mapNotNull {
|
||||||
if (it is KtConstructor<*>) it.containingClassOrObject else it
|
when {
|
||||||
|
it is KtConstructor<*> -> it.containingClassOrObject
|
||||||
|
it is PsiMethod && it.isConstructor -> it.containingClass
|
||||||
|
else -> it
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val project = element.project
|
val project = element.project
|
||||||
|
|||||||
+6
-4
@@ -6,13 +6,13 @@
|
|||||||
package org.jetbrains.kotlin.idea.refactoring.introduce.introduceImportAlias
|
package org.jetbrains.kotlin.idea.refactoring.introduce.introduceImportAlias
|
||||||
|
|
||||||
import com.intellij.openapi.actionSystem.DataContext
|
import com.intellij.openapi.actionSystem.DataContext
|
||||||
import com.intellij.openapi.application.ApplicationManager
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.search.searches.ReferencesSearch
|
import com.intellij.psi.search.searches.ReferencesSearch
|
||||||
import com.intellij.refactoring.RefactoringActionHandler
|
import com.intellij.refactoring.RefactoringActionHandler
|
||||||
import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer
|
import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer
|
||||||
|
import org.jetbrains.annotations.TestOnly
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference
|
||||||
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
|
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
|
||||||
@@ -44,6 +44,9 @@ import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
|||||||
object KotlinIntroduceImportAliasHandler : RefactoringActionHandler {
|
object KotlinIntroduceImportAliasHandler : RefactoringActionHandler {
|
||||||
const val REFACTORING_NAME = "Introduce Import Alias"
|
const val REFACTORING_NAME = "Introduce Import Alias"
|
||||||
|
|
||||||
|
@get:TestOnly
|
||||||
|
var suggestedImportAliasNames: Collection<String> = emptyList()
|
||||||
|
|
||||||
fun doRefactoring(project: Project, editor: Editor, element: KtNameReferenceExpression) {
|
fun doRefactoring(project: Project, editor: Editor, element: KtNameReferenceExpression) {
|
||||||
val fqName = element.resolveMainReferenceToDescriptors().firstOrNull()?.importableFqName ?: return
|
val fqName = element.resolveMainReferenceToDescriptors().firstOrNull()?.importableFqName ?: return
|
||||||
val file = element.containingKtFile
|
val file = element.containingKtFile
|
||||||
@@ -82,6 +85,7 @@ object KotlinIntroduceImportAliasHandler : RefactoringActionHandler {
|
|||||||
|
|
||||||
val suggestionsName = KotlinNameSuggester.suggestNamesByFqName(fqName, validator = validator)
|
val suggestionsName = KotlinNameSuggester.suggestNamesByFqName(fqName, validator = validator)
|
||||||
val newName = suggestionsName.first()
|
val newName = suggestionsName.first()
|
||||||
|
suggestedImportAliasNames = suggestionsName
|
||||||
val newDirective = ImportInsertHelperImpl.addImport(project, file, fqName, false, Name.identifier(newName))
|
val newDirective = ImportInsertHelperImpl.addImport(project, file, fqName, false, Name.identifier(newName))
|
||||||
|
|
||||||
replaceUsages(usages, newName)
|
replaceUsages(usages, newName)
|
||||||
@@ -89,9 +93,7 @@ object KotlinIntroduceImportAliasHandler : RefactoringActionHandler {
|
|||||||
|
|
||||||
if (elementInImportDirective) editor.moveCaret(newDirective.alias?.nameIdentifier?.textOffset ?: newDirective.endOffset)
|
if (elementInImportDirective) editor.moveCaret(newDirective.alias?.nameIdentifier?.textOffset ?: newDirective.endOffset)
|
||||||
|
|
||||||
if (!ApplicationManager.getApplication().isUnitTestMode) {
|
invokeRename(project, editor, newDirective.alias, suggestionsName)
|
||||||
invokeRename(project, editor, newDirective.alias, suggestionsName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext?) {
|
override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext?) {
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
HashMap1
|
||||||
|
UtilHashMap
|
||||||
|
JavaUtilHashMap
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
import java.util.HashMap
|
||||||
|
|
||||||
|
fun x() {
|
||||||
|
val list = <caret>HashMap<String, String>()
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import java.util.HashMap as HashMap1
|
||||||
|
|
||||||
|
fun x() {
|
||||||
|
val list = HashMap1<String, String>()
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
class Some {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
Some1
|
||||||
|
FooSome
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
import foo.Some
|
||||||
|
|
||||||
|
val some = <caret>Some()
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import foo.Some as Some1
|
||||||
|
|
||||||
|
val some = Some1()
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 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.addImportAlias
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.AbstractImportsTest
|
||||||
|
import org.jetbrains.kotlin.idea.core.util.range
|
||||||
|
import org.jetbrains.kotlin.idea.refactoring.introduce.introduceImportAlias.KotlinIntroduceImportAliasHandler
|
||||||
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
|
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
|
||||||
|
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||||
|
import org.jetbrains.kotlin.utils.sure
|
||||||
|
|
||||||
|
abstract class AbstractAddImportAliasTest : AbstractImportsTest() {
|
||||||
|
override fun doTest(file: KtFile): String? {
|
||||||
|
val element = findNameReferenceExpression()
|
||||||
|
KotlinIntroduceImportAliasHandler.doRefactoring(project, editor, element)
|
||||||
|
|
||||||
|
val importAliasNames = KotlinIntroduceImportAliasHandler.suggestedImportAliasNames.joinToString("\n")
|
||||||
|
KotlinTestUtils.assertEqualsToFile(testDataFile(fileName().replace(".kt", ".expected.names")), importAliasNames)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun findNameReferenceExpression(): KtNameReferenceExpression {
|
||||||
|
val offset = myFixture.caretOffset
|
||||||
|
var element = file.findElementAt(offset)
|
||||||
|
while (element != null) {
|
||||||
|
if (element is KtNameReferenceExpression || element is KtFile) break
|
||||||
|
if (offset !in element.range) break
|
||||||
|
element = element.parent
|
||||||
|
}
|
||||||
|
return (element as? KtNameReferenceExpression).sure {
|
||||||
|
"caret has to be placed at KtNameReferenceExpression, current position at $offset"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
+40
@@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2020 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.addImportAlias;
|
||||||
|
|
||||||
|
import com.intellij.testFramework.TestDataPath;
|
||||||
|
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
||||||
|
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||||
|
import org.jetbrains.kotlin.test.TestMetadata;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
|
||||||
|
@SuppressWarnings("all")
|
||||||
|
@TestMetadata("idea/testData/addImportAlias")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public class AddImportAliasTestGenerated extends AbstractAddImportAliasTest {
|
||||||
|
private void runTest(String testDataFilePath) throws Exception {
|
||||||
|
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAllFilesPresentInAddImportAlias() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/addImportAlias"), Pattern.compile("^([^.]+)\\.kt$"), null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("JavaAlias.kt")
|
||||||
|
public void testJavaAlias() throws Exception {
|
||||||
|
runTest("idea/testData/addImportAlias/JavaAlias.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("SimpleAlias.kt")
|
||||||
|
public void testSimpleAlias() throws Exception {
|
||||||
|
runTest("idea/testData/addImportAlias/SimpleAlias.kt");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user