diff --git a/ChangeLog.md b/ChangeLog.md index b4f18673b4d..7337788e038 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -218,6 +218,7 @@ #### Refactorings ###### New features +- [`KT-6372`](https://youtrack.jetbrains.com/issue/KT-6372) Add name suggestions to Rename dialog - [`KT-7851`](https://youtrack.jetbrains.com/issue/KT-7851) Respect naming conventions in automatic variable rename - [`KT-8044`](https://youtrack.jetbrains.com/issue/KT-8044), [`KT-9432`](https://youtrack.jetbrains.com/issue/KT-9432) Support @JvmName annotation in rename refactoring - [`KT-8512`](https://youtrack.jetbrains.com/issue/KT-8512) Support "Rename tests" options in Rename dialog diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinNameSuggestionProvider.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinNameSuggestionProvider.kt new file mode 100644 index 00000000000..0c06e9963b5 --- /dev/null +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinNameSuggestionProvider.kt @@ -0,0 +1,73 @@ +/* + * 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.core + +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiVariable +import com.intellij.psi.codeStyle.JavaCodeStyleManager +import com.intellij.psi.codeStyle.SuggestedNameInfo +import com.intellij.psi.statistics.JavaStatisticsManager +import com.intellij.refactoring.rename.NameSuggestionProvider +import org.jetbrains.kotlin.asJava.toLightElements +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor +import org.jetbrains.kotlin.psi.KtCallableDeclaration +import org.jetbrains.kotlin.psi.KtParameter +import org.jetbrains.kotlin.psi.KtProperty +import org.jetbrains.kotlin.utils.SmartList +import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull + +class KotlinNameSuggestionProvider : NameSuggestionProvider { + override fun getSuggestedNames(element: PsiElement, nameSuggestionContext: PsiElement?, result: MutableSet): SuggestedNameInfo? { + if (element is KtCallableDeclaration) { + val context = nameSuggestionContext ?: element.parent + val target = if (element is KtProperty || element is KtParameter) { + NewDeclarationNameValidator.Target.VARIABLES + } + else { + NewDeclarationNameValidator.Target.FUNCTIONS_AND_CLASSES + } + val validator = NewDeclarationNameValidator(context, element, target, listOf(element)) + val names = SmartList().apply { + val name = element.name + if (!name.isNullOrBlank()) { + this += KotlinNameSuggester.getCamelNames(name!!, validator, name.first().isLowerCase()) + } + + val type = (element.resolveToDescriptor() as CallableDescriptor).returnType + if (type != null) { + this += KotlinNameSuggester.suggestNamesByType(type, validator) + } + } + result += names + + return object : SuggestedNameInfo(names.toTypedArray()) { + override fun nameChosen(name: String?) { + val psiVariable = element.toLightElements().firstIsInstanceOrNull() ?: return + JavaStatisticsManager.incVariableNameUseCount( + name, + JavaCodeStyleManager.getInstance(element.project).getVariableKind(psiVariable), + psiVariable.name, + psiVariable.type + ) + } + } + } + + return null + } +} diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 8e0e91cd2dc..dfd0e8ea403 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -682,6 +682,8 @@ + + org.jetbrains.kotlin.idea.intentions.IfNullToElvisIntention Kotlin