Show inlay hints for implicit parameters and receivers of lambdas

#KT-20533 Fixed
This commit is contained in:
Dmitry Jemerov
2018-01-04 15:51:01 +01:00
parent da157fafdc
commit 6c23d3a220
4 changed files with 124 additions and 18 deletions
@@ -84,7 +84,8 @@ enum class HintType(desc: String, enabled: Boolean) {
},
LAMBDA_RETURN_EXPRESSION("Show lambda return expression hints", true) {
override fun isApplicable(elem: PsiElement) = elem is KtExpression
override fun isApplicable(elem: PsiElement) =
elem is KtExpression && elem !is KtLambdaExpression && elem !is KtFunctionLiteral
override fun provideHints(elem: PsiElement): List<InlayInfo> {
if (elem is KtExpression) {
@@ -92,8 +93,18 @@ enum class HintType(desc: String, enabled: Boolean) {
}
return emptyList()
}
}
;
},
LAMBDA_IMPLICIT_PARAMETER_RECEIVER("Show hints for implicit receivers and parameters of lambdas", true) {
override fun isApplicable(elem: PsiElement) = elem is KtLambdaExpression
override fun provideHints(elem: PsiElement): List<InlayInfo> {
(elem as? KtLambdaExpression)?.let {
return provideLambdaImplicitHints(elem)
}
return emptyList()
}
};
companion object {
@@ -0,0 +1,61 @@
/*
* 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
import com.intellij.codeInsight.hints.InlayInfo
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiComment
import com.intellij.psi.TokenType
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.psiUtil.siblings
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.typeUtil.isUnit
fun provideLambdaImplicitHints(lambda: KtLambdaExpression): List<InlayInfo> {
val lbrace = lambda.leftCurlyBrace
if (!lbrace.isFollowedByNewLine()) {
return emptyList()
}
val bindingContext = lambda.analyze(BodyResolveMode.PARTIAL)
val functionDescriptor = bindingContext[BindingContext.FUNCTION, lambda.functionLiteral] ?: return emptyList()
val implicitReceiver = functionDescriptor.extensionReceiverParameter
if (implicitReceiver != null) {
val text = buildString {
append(TYPE_INFO_PREFIX)
append("this: ")
append(inlayHintsTypeRenderer.renderType(implicitReceiver.type))
}
return listOf(InlayInfo(text, lbrace.textRange.endOffset))
}
val singleParameter = functionDescriptor.valueParameters.singleOrNull()
if (singleParameter != null && bindingContext[BindingContext.AUTO_CREATED_IT, singleParameter] == true) {
val type = singleParameter.type
if (type.isUnit()) return emptyList()
val text = buildString {
append(TYPE_INFO_PREFIX)
append("it: ")
append(inlayHintsTypeRenderer.renderType(type))
}
return listOf(InlayInfo(text, lbrace.textRange.endOffset))
}
return emptyList()
}
private fun ASTNode.isFollowedByNewLine(): Boolean {
for (sibling in siblings()) {
if (sibling.elementType != TokenType.WHITE_SPACE && sibling.psi !is PsiComment) {
return false
}
if (sibling.elementType == TokenType.WHITE_SPACE && sibling.textContains('\n')) {
return true
}
}
return false
}
@@ -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
@@ -35,7 +24,7 @@ import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes
//hack to separate type presentation from param info presentation
const val TYPE_INFO_PREFIX = "@TYPE@"
private val typeRenderer = DescriptorRenderer.COMPACT_WITH_SHORT_TYPES.withOptions {
val inlayHintsTypeRenderer = DescriptorRenderer.COMPACT_WITH_SHORT_TYPES.withOptions {
textFormat = RenderingFormat.PLAIN
renderUnabbreviatedType = false
}
@@ -76,7 +65,7 @@ fun provideTypeHint(element: KtCallableDeclaration, offset: Int): List<InlayInfo
append(":")
if (settings.SPACE_AFTER_TYPE_COLON)
append(" ")
append(typeRenderer.renderType(type))
append(inlayHintsTypeRenderer.renderType(type))
}
listOf(InlayInfo(declString, offset))
}
@@ -0,0 +1,45 @@
/*
* 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
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
class LambdaImplicitHintsTest : KotlinLightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor(): KotlinLightProjectDescriptor = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
fun check(text: String) {
myFixture.configureByText("A.kt", text)
myFixture.testInlays()
}
fun testSimpleIt() {
check(
"""
val x = listOf("").filter {<hint text="it: String" />
it.startsWith("")
}"""
)
}
fun testSimpleThis() {
check(
"""
val x = buildString {<hint text="this: StringBuilder" />
append("foo")
}"""
)
}
fun testSingleLine() {
check(
"""
val x = listOf("").filter { it.startsWith("") }
"""
)
}
}