From 8ba1aff7e6fdc3f5f7e9f4e69cbc83c7c52f42e8 Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 31 May 2016 20:39:14 +0300 Subject: [PATCH] Rename: Show more detailed element descriptions in Rename dialog #KT-8544 Fixed --- ChangeLog.md | 1 + .../findUsages/KotlinFindUsagesProvider.kt | 52 ----------- .../findUsages/KotlinFindUsagesProvider.kt | 87 +++++++++++++++++++ 3 files changed, 88 insertions(+), 52 deletions(-) delete mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/findUsages/KotlinFindUsagesProvider.kt create mode 100644 idea/src/org/jetbrains/kotlin/idea/findUsages/KotlinFindUsagesProvider.kt diff --git a/ChangeLog.md b/ChangeLog.md index 84d68111b0f..dd9de1aa71c 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -186,6 +186,7 @@ ###### Issues fixed - [`KT-8541`](https://youtrack.jetbrains.com/issue/KT-8541), [`KT-8786`](https://youtrack.jetbrains.com/issue/KT-8786) Do now show 'Rename overloads' options if target function has no overloads +- [`KT-8544`](https://youtrack.jetbrains.com/issue/KT-8544) Show more detailed description in Rename dialog - [`KT-8860`](https://youtrack.jetbrains.com/issue/KT-8860) Allow renaming class by constructor delegation call referencing primary constructor - [`KT-8892`](https://youtrack.jetbrains.com/issue/KT-8892) Suggest renaming base declarations on overriding members in object literals - [`KT-9156`](https://youtrack.jetbrains.com/issue/KT-9156) Quote non-identifier names in Kotlin references diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/findUsages/KotlinFindUsagesProvider.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/findUsages/KotlinFindUsagesProvider.kt deleted file mode 100644 index 95ad4ef6465..00000000000 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/findUsages/KotlinFindUsagesProvider.kt +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.idea.findUsages - -import com.intellij.lang.findUsages.FindUsagesProvider -import com.intellij.psi.PsiElement -import com.intellij.psi.PsiNamedElement -import org.jetbrains.kotlin.psi.* - -class KotlinFindUsagesProvider : FindUsagesProvider { - override fun canFindUsagesFor(psiElement: PsiElement): Boolean = - psiElement is KtNamedDeclaration - - override fun getWordsScanner() = null - - override fun getHelpId(psiElement: PsiElement): String? = null - - override fun getType(element: PsiElement): String { - return when(element) { - is KtNamedFunction -> "function" - is KtClass -> "class" - is KtParameter -> "parameter" - is KtProperty -> if (element.isLocal) "variable" else "property" - is KtDestructuringDeclarationEntry -> "variable" - is KtTypeParameter -> "type parameter" - is KtSecondaryConstructor -> "constructor" - is KtObjectDeclaration -> "object" - else -> "" - } - } - - override fun getDescriptiveName(element: PsiElement): String { - return if (element is PsiNamedElement) element.name ?: "" else "" - } - - override fun getNodeText(element: PsiElement, useFullName: Boolean): String = - getDescriptiveName(element) -} diff --git a/idea/src/org/jetbrains/kotlin/idea/findUsages/KotlinFindUsagesProvider.kt b/idea/src/org/jetbrains/kotlin/idea/findUsages/KotlinFindUsagesProvider.kt new file mode 100644 index 00000000000..30d8453b9ef --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/findUsages/KotlinFindUsagesProvider.kt @@ -0,0 +1,87 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.findUsages + +import com.intellij.lang.findUsages.FindUsagesProvider +import com.intellij.lang.java.JavaFindUsagesProvider +import com.intellij.psi.PsiDirectory +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.psi.PsiPackage +import org.jetbrains.kotlin.descriptors.FunctionDescriptor +import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor +import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject +import org.jetbrains.kotlin.types.typeUtil.isUnit + +class KotlinFindUsagesProvider : FindUsagesProvider { + private val javaProvider by lazy { JavaFindUsagesProvider() } + + override fun canFindUsagesFor(psiElement: PsiElement): Boolean = + psiElement is KtNamedDeclaration + + override fun getWordsScanner() = null + + override fun getHelpId(psiElement: PsiElement): String? = null + + override fun getType(element: PsiElement): String { + return when(element) { + is KtNamedFunction -> "function" + is KtClass -> "class" + is KtParameter -> "parameter" + is KtProperty -> if (element.isLocal) "variable" else "property" + is KtDestructuringDeclarationEntry -> "variable" + is KtTypeParameter -> "type parameter" + is KtSecondaryConstructor -> "constructor" + is KtObjectDeclaration -> "object" + else -> "" + } + } + + private val KtDeclaration.containerDescription: String? + get() { + containingClassOrObject?.let { return getDescriptiveName(it) } + (parent as? KtFile)?.parent?.let { return getDescriptiveName(it) } + return null + } + + override fun getDescriptiveName(element: PsiElement): String { + return when (element) { + is PsiDirectory, is PsiPackage, is PsiFile -> javaProvider.getDescriptiveName(element) + is KtClassOrObject -> { + if (element is KtObjectDeclaration && element.isObjectLiteral()) return "" + element.fqName?.asString() ?: element.name ?: "" + } + is KtProperty -> (element.name ?: "") + (element.containerDescription?.let { " of $it" } ?: "") + is KtFunction -> { + val name = element.name ?: "" + val descriptor = element.resolveToDescriptor() as FunctionDescriptor + val renderer = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES + val paramsDescription = descriptor.valueParameters.joinToString(prefix = "(", postfix = ")") { renderer.renderType(it.type) } + val returnType = descriptor.returnType + val returnTypeDescription = if (returnType != null && !returnType.isUnit()) renderer.renderType(returnType) else null + val funDescription = "$name$paramsDescription" + (returnTypeDescription?.let { ": $it" } ?: "") + return funDescription + (element.containerDescription?.let { " of $it" } ?: "") + } + else -> "" + } + } + + override fun getNodeText(element: PsiElement, useFullName: Boolean): String = + getDescriptiveName(element) +}