KT-33372 Remove renaming file reference to the contents of the file

- There is still a hack with returning null from `getLastFileReference`,
it is here to keep KT-25674 issue fixed
- Overrides of `bindToElement` are removed, they caused renames of the
file references to their contents
- Code of `KotlinFilePathReferenceContributor.kt` is refactored
- ^KT-33372 ^KT-32514 ^KT-36306 Fixed
This commit is contained in:
Roman Golyshev
2020-02-14 11:48:14 +03:00
committed by Roman Golyshev
parent 50c477bee1
commit 3461effd47
19 changed files with 86 additions and 31 deletions
@@ -0,0 +1,5 @@
//INVOCATION_COUNT: 2
fun main() {
"Integ<caret>"
}
//ELEMENT: Integer
@@ -0,0 +1,5 @@
//INVOCATION_COUNT: 2
fun main() {
"java.lang.Integer"
}
//ELEMENT: Integer
@@ -143,6 +143,11 @@ public class BasicCompletionHandlerTestGenerated extends AbstractBasicCompletion
runTest("idea/idea-completion/testData/handlers/basic/KT23627.kt");
}
@TestMetadata("KT36306.kt")
public void testKT36306() throws Exception {
runTest("idea/idea-completion/testData/handlers/basic/KT36306.kt");
}
@TestMetadata("NestedTypeArg.kt")
public void testNestedTypeArg() throws Exception {
runTest("idea/idea-completion/testData/handlers/basic/NestedTypeArg.kt");
@@ -143,6 +143,11 @@ public class PerformanceBasicCompletionHandlerTestGenerated extends AbstractPerf
runTest("idea/idea-completion/testData/handlers/basic/KT23627.kt");
}
@TestMetadata("KT36306.kt")
public void testKT36306() throws Exception {
runTest("idea/idea-completion/testData/handlers/basic/KT36306.kt");
}
@TestMetadata("NestedTypeArg.kt")
public void testNestedTypeArg() throws Exception {
runTest("idea/idea-completion/testData/handlers/basic/NestedTypeArg.kt");
@@ -37,43 +37,40 @@ class KotlinFilePathReferenceContributor : PsiReferenceContributor() {
if (element !is KtStringTemplateExpression) return PsiReference.EMPTY_ARRAY
if (!element.isPlain()) return PsiReference.EMPTY_ARRAY
val refByElem = getReferencesByElement(element, element.plainContent, element.getContentRange().startOffset, true)
val res = refByElem
.map {
if (it is FileReference) {
object : FileReference(it.fileReferenceSet, it.rangeInElement, it.index, it.text) {
override fun getVariants(): Array<out Any> {
return super.getVariants()
.map {
(it as? LookupElement)?.apply {
putUserData(KotlinCompletionCharFilter.SUPPRESS_ITEM_SELECTION_BY_CHARS_ON_TYPING, Unit)
} ?: it
}
.toTypedArray()
}
// Note: this is a hack that prevents IDE from renaming paths in string literals when doing move.
// TODO: find another way of doing this (that may need re-implementing move files refactoring using .java move)
override fun getLastFileReference() = null
override fun bindToElement(element: PsiElement): PsiElement {
return rename(element.text)
}
override fun bindToElement(element: PsiElement, absolute: Boolean): PsiElement {
return rename(element.text)
}
}
} else it
}
.toTypedArray()
return res
return refByElem.map { if (it is FileReference) FixedFileReference(it) else it }.toTypedArray()
}
}
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
registrar.registerReferenceProvider(
PlatformPatterns.psiElement(KtStringTemplateExpression::class.java),
KotlinFilePathReferenceProvider
KotlinFilePathReferenceProvider,
)
}
}
private class FixedFileReference(fileReference: FileReference) : FileReference(fileReference) {
override fun getVariants(): Array<out Any> {
val variants = super.getVariants()
variants.forEach {
if (it is LookupElement) {
it.putUserData(KotlinCompletionCharFilter.SUPPRESS_ITEM_SELECTION_BY_CHARS_ON_TYPING, Unit)
}
}
return variants
}
/**
* We have a bug that turns file references in code like `"."` and `".."` into `""` during file move because .kt files
* are moved by [org.jetbrains.kotlin.idea.refactoring.move.moveDeclarations.KotlinAwareMoveFilesOrDirectoriesProcessor],
* which is an extender of [com.intellij.refactoring.move.moveFilesOrDirectories.MoveFilesOrDirectoriesProcessor]. It uses
* this method to obtain the referenced file, and if it's null, it will not try to replace the reference to it.
*
* However, this is a hack, because there are probably some usages where this reference should not be null.
*
* TODO Try to resolve this issue again when IDEA-232942 is resolved (maybe this won't be an issue anymore)
*/
override fun getLastFileReference(): FileReference? = null
}
@@ -0,0 +1,2 @@
val a = "."
val b = ".."
@@ -0,0 +1,2 @@
val a = "."
val b = ".."
@@ -0,0 +1,5 @@
{
"mainFile": "a/a/main.kt",
"type": "MOVE_FILES",
"targetDirectory": "b"
}
@@ -0,0 +1,6 @@
fun take(a: String) {}
fun usage() {
take("new_resource.txt")
take("new_resource.txt")
}
@@ -0,0 +1 @@
resource content
@@ -0,0 +1,6 @@
fun take(a: String) {}
fun usage() {
take("resource.txt")
take("./resource.txt")
}
@@ -0,0 +1 @@
resource content
@@ -0,0 +1,5 @@
{
"type": "FILE",
"file": "resource.txt",
"newName": "new_resource.txt"
}
@@ -263,6 +263,11 @@ public class MoveTestGenerated extends AbstractMoveTest {
runTest("idea/testData/refactoring/move/kotlin/moveFile/moveFileToFile/moveFileToFile.test");
}
@TestMetadata("kotlin/moveFile/moveFileWithDotsAsFileReferences/moveFileWithDotsAsFileReferences.test")
public void testKotlin_moveFile_moveFileWithDotsAsFileReferences_MoveFileWithDotsAsFileReferences() throws Exception {
runTest("idea/testData/refactoring/move/kotlin/moveFile/moveFileWithDotsAsFileReferences/moveFileWithDotsAsFileReferences.test");
}
@TestMetadata("kotlin/moveFile/moveFileWithoutDeclarations/moveFileWithoutDeclarations.test")
public void testKotlin_moveFile_moveFileWithoutDeclarations_MoveFileWithoutDeclarations() throws Exception {
runTest("idea/testData/refactoring/move/kotlin/moveFile/moveFileWithoutDeclarations/moveFileWithoutDeclarations.test");
@@ -1148,6 +1148,11 @@ public class RenameTestGenerated extends AbstractRenameTest {
runTest("idea/testData/refactoring/rename/renamePropertyInEnumCompanionWithEntryConflict/renameKotlinPropertyInEnumCompanionWithEntryConflict.test");
}
@TestMetadata("renameReferencedResourceFile/renameReferencedResourceFile.test")
public void testRenameReferencedResourceFile_RenameReferencedResourceFile() throws Exception {
runTest("idea/testData/refactoring/rename/renameReferencedResourceFile/renameReferencedResourceFile.test");
}
@TestMetadata("renameSet/set.test")
public void testRenameSet_Set() throws Exception {
runTest("idea/testData/refactoring/rename/renameSet/set.test");