From da157fafdc531795939ba6b2046e5ff577591180 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 4 Jan 2018 12:42:32 +0100 Subject: [PATCH] Add inlay hints for values returned from lambdas #KT-20067 Fixed --- .../cfg/ControlFlowInformationProvider.kt | 15 +-- .../KotlinInlayParameterHintsProvider.kt | 83 +++++++++------- .../parameterInfo/LambdaReturnValueHints.kt | 50 ++++++++++ .../LambdaReturnValueHintsTest.kt | 99 +++++++++++++++++++ 4 files changed, 196 insertions(+), 51 deletions(-) create mode 100644 idea/src/org/jetbrains/kotlin/idea/parameterInfo/LambdaReturnValueHints.kt create mode 100644 idea/tests/org/jetbrains/kotlin/idea/parameterInfo/LambdaReturnValueHintsTest.kt diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProvider.kt b/compiler/frontend/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProvider.kt index b408ca0cb47..304b5c05a8b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProvider.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProvider.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 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. + * Copyright 2000-2018 JetBrains s.r.o. 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.cfg diff --git a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt index c850badffe0..b13ea55fd8a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 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. + * Copyright 2000-2018 JetBrains s.r.o. 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.idea.parameterInfo @@ -30,6 +19,7 @@ import org.jetbrains.kotlin.idea.quickfix.createFromUsage.callableBuilder.getRet import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset +import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.DefaultValueArgument import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull @@ -51,9 +41,10 @@ enum class HintType(desc: String, enabled: Boolean) { return providePropertyTypeHint(elem) } - override fun isApplicable(elem: PsiElement): Boolean = (elem is KtProperty && elem.getReturnTypeReference() == null && elem.isLocal) || - (elem is KtParameter && elem.isLoopParameter && elem.typeReference == null) || - (elem is KtDestructuringDeclarationEntry && elem.getReturnTypeReference() == null) + override fun isApplicable(elem: PsiElement): Boolean = + (elem is KtProperty && elem.getReturnTypeReference() == null && elem.isLocal) || + (elem is KtParameter && elem.isLoopParameter && elem.typeReference == null) || + (elem is KtDestructuringDeclarationEntry && elem.getReturnTypeReference() == null) }, FUNCTION_HINT("Show function return type hints", false) { @@ -66,8 +57,10 @@ enum class HintType(desc: String, enabled: Boolean) { return emptyList() } - override fun isApplicable(elem: PsiElement): Boolean = elem is KtNamedFunction && !(elem.hasBlockBody() || elem.hasDeclaredReturnType()) + override fun isApplicable(elem: PsiElement): Boolean = + elem is KtNamedFunction && !(elem.hasBlockBody() || elem.hasDeclaredReturnType()) }, + PARAMETER_TYPE_HINT("Show parameter type hints ", false) { override fun provideHints(elem: PsiElement): List { (elem as? KtParameter)?.let { param -> @@ -80,16 +73,27 @@ enum class HintType(desc: String, enabled: Boolean) { override fun isApplicable(elem: PsiElement): Boolean = elem is KtParameter && elem.typeReference == null && !elem.isLoopParameter }, + PARAMETER_HINT("Show argument name hints", true) { override fun provideHints(elem: PsiElement): List { - (elem as? KtCallElement)?.let { - return provideArgumentNameHints(it) + val callElement = elem.getStrictParentOfType() ?: return emptyList() + return provideArgumentNameHints(callElement) + } + + override fun isApplicable(elem: PsiElement): Boolean = elem is KtValueArgumentList + }, + + LAMBDA_RETURN_EXPRESSION("Show lambda return expression hints", true) { + override fun isApplicable(elem: PsiElement) = elem is KtExpression + + override fun provideHints(elem: PsiElement): List { + if (elem is KtExpression) { + return provideLambdaReturnValueHints(elem) } return emptyList() } - - override fun isApplicable(elem: PsiElement): Boolean = elem is KtCallElement - }; + } + ; companion object { @@ -99,8 +103,7 @@ enum class HintType(desc: String, enabled: Boolean) { val resolved = elem?.let { resolve(it) } ?: return null return if (resolved.enabled) { resolved - } - else { + } else { null } } @@ -118,26 +121,30 @@ class KotlinInlayParameterHintsProvider : InlayParameterHintsProvider { override fun getSupportedOptions(): List