From f19f38a4db4779393ad51a56b8ef3b7e0da5f929 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Wed, 9 Jan 2019 12:40:25 +0300 Subject: [PATCH] Rework "lambda return expression" hint (KT-28870) Move the hint to the end of the line, to avoid breaking indentation. Also use `EditorLinePainter` API for painting to prevent problems with typing at the line end, when caret is placed after the hint (See IDEA-204702 for implementing such hints in platform). #KT-28870 Fixed --- idea/resources/META-INF/plugin-common.xml | 11 ++ .../KotlinInlayParameterHintsProvider.kt | 21 ++- .../DisableReturnLambdaHintOptionAction.kt | 50 +++++ .../custom/KotlinCodeHintsModel.kt | 120 ++++++++++++ .../custom/KotlinCodeHintsPass.kt | 103 +++++++++++ .../custom/ReturnHintLinePainter.kt | 39 ++++ .../org/jetbrains/kotlin/idea/util/editors.kt | 27 +++ .../LambdaReturnValueHintsTest.kt | 174 +++++++++++++++--- 8 files changed, 514 insertions(+), 31 deletions(-) create mode 100644 idea/src/org/jetbrains/kotlin/idea/parameterInfo/custom/DisableReturnLambdaHintOptionAction.kt create mode 100644 idea/src/org/jetbrains/kotlin/idea/parameterInfo/custom/KotlinCodeHintsModel.kt create mode 100644 idea/src/org/jetbrains/kotlin/idea/parameterInfo/custom/KotlinCodeHintsPass.kt create mode 100644 idea/src/org/jetbrains/kotlin/idea/parameterInfo/custom/ReturnHintLinePainter.kt create mode 100644 idea/src/org/jetbrains/kotlin/idea/util/editors.kt diff --git a/idea/resources/META-INF/plugin-common.xml b/idea/resources/META-INF/plugin-common.xml index 12690a1199e..508810dcd94 100644 --- a/idea/resources/META-INF/plugin-common.xml +++ b/idea/resources/META-INF/plugin-common.xml @@ -3,6 +3,9 @@ org.jetbrains.kotlin.idea.highlighter.KotlinBeforeResolveHighlightingPass$Factory + + org.jetbrains.kotlin.idea.parameterInfo.custom.KotlinCodeHintsPass$Companion$Factory + org.jetbrains.kotlin.idea.refactoring.cutPaste.MoveDeclarationsPassFactory @@ -26,6 +29,9 @@ org.jetbrains.kotlin.idea.configuration.KotlinMigrationProjectComponent + + org.jetbrains.kotlin.idea.parameterInfo.custom.KotlinCodeHintsModel + @@ -378,6 +384,7 @@ + @@ -1694,6 +1701,10 @@ Kotlin + + org.jetbrains.kotlin.idea.parameterInfo.custom.DisableReturnLambdaHintOptionAction + + org.jetbrains.kotlin.idea.intentions.AddUnderscoresToNumericLiteralIntention Kotlin diff --git a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt index 328745c47d9..3a7540d4674 100644 --- a/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/parameterInfo/KotlinInlayParameterHintsProvider.kt @@ -24,7 +24,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe -enum class HintType(desc: String, enabled: Boolean) { +enum class HintType(val desc: String, defaultEnabled: Boolean) { PROPERTY_HINT("Show property type hints", false) { override fun provideHints(elem: PsiElement): List { @@ -86,9 +86,11 @@ enum class HintType(desc: String, enabled: Boolean) { elem is KtExpression && elem !is KtFunctionLiteral && !elem.isNameReferenceInCall() override fun provideHints(elem: PsiElement): List { - if (elem is KtExpression) { - return provideLambdaReturnValueHints(elem) - } + // Will be painted with ReturnHintLinePainter + + // Enable/Disable setting will be present in the list with other hints. + // Enable action will be provided by the platform. + // Disable action need to be reimplemented as hints are not actually added, see DisableReturnLambdaHintOptionAction. return emptyList() } @@ -133,11 +135,12 @@ enum class HintType(desc: String, enabled: Boolean) { abstract fun isApplicable(elem: PsiElement): Boolean abstract fun provideHints(elem: PsiElement): List - val option = Option("SHOW_${this.name}", desc, enabled) + val option = Option("SHOW_${this.name}", desc, defaultEnabled) val enabled get() = option.get() } +@Suppress("UnstableApiUsage") class KotlinInlayParameterHintsProvider : InlayParameterHintsProvider { override fun getSupportedOptions(): List