From d70019ac8d2dd3a53e9075f327ee55d239934c69 Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Wed, 26 Apr 2017 15:44:35 +0300 Subject: [PATCH] [param hints] allow to enable OptionInfo hints with alt-enter --- .../hints/InlayParameterHintsProvider.kt | 5 ++ .../codeInsight/hints/PopupActions.kt | 58 ++++++++++++++++++- .../src/META-INF/LangExtensions.xml | 3 + 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/platform/lang-api/src/com/intellij/codeInsight/hints/InlayParameterHintsProvider.kt b/platform/lang-api/src/com/intellij/codeInsight/hints/InlayParameterHintsProvider.kt index 9cf1b2dc530d..bb3164401ea1 100644 --- a/platform/lang-api/src/com/intellij/codeInsight/hints/InlayParameterHintsProvider.kt +++ b/platform/lang-api/src/com/intellij/codeInsight/hints/InlayParameterHintsProvider.kt @@ -67,6 +67,9 @@ sealed class HintInfo { } open val optionName = option.name + + fun isOptionEnabled(): Boolean = option.isEnabled() + } } @@ -75,6 +78,8 @@ data class Option(val id: String, val name: String, val defaultValue: Boolean) { + fun isEnabled() = get() + fun get(): Boolean { return ParameterNameHintsSettings.getInstance().getOption(id) ?: defaultValue } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt b/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt index 9ee71a8c884d..c3b0254a110c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt @@ -35,6 +35,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.ex.EditorSettingsExternalizable +import com.intellij.openapi.editor.impl.EditorImpl import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.project.Project import com.intellij.openapi.project.ProjectManager @@ -200,6 +201,60 @@ class DisableCustomHintsOption: IntentionAction, HighPriorityAction { override fun startInWriteAction() = false } +class EnableCustomHintsOption: IntentionAction, HighPriorityAction { + + companion object { + private val presentableFamilyName = CodeInsightBundle.message("inlay.hints.intention.family.name") + } + + private var lastOptionName = "" + + override fun getText(): String { + if (lastOptionName.startsWith("show", ignoreCase = true)) { + return lastOptionName.capitalizeFirstLetter() + } + return CodeInsightBundle.message("inlay.hints.enable.custom.option", lastOptionName) + } + + override fun getFamilyName(): String = presentableFamilyName + + override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean { + if (!EditorSettingsExternalizable.getInstance().isShowParameterNameHints) return false + if (editor !is EditorImpl) return false + + InlayParameterHintsExtension.forLanguage(file.language) ?: return false + + val option = getDisabledOptionInfoAtCaretOffset(editor, file) ?: return false + lastOptionName = option.optionName + + return true + } + + private fun getDisabledOptionInfoAtCaretOffset(editor: Editor, file: PsiFile): HintInfo.OptionInfo? { + val offset = editor.caretModel.offset + + val element = file.findElementAt(offset) ?: return null + val provider = InlayParameterHintsExtension.forLanguage(file.language) ?: return null + + val target = PsiTreeUtil.findFirstParent(element, { provider.hasDisabledOptionHintInfo(it) }) ?: return null + return provider.getHintInfo(target) as? HintInfo.OptionInfo + } + + override fun invoke(project: Project, editor: Editor, file: PsiFile) { + val option = getDisabledOptionInfoAtCaretOffset(editor, file) ?: return + option.enable() + } + + override fun startInWriteAction() = false + +} + + +private fun InlayParameterHintsProvider.hasDisabledOptionHintInfo(element: PsiElement): Boolean { + val info = getHintInfo(element) + return info is HintInfo.OptionInfo && !info.isOptionEnabled() +} + class ToggleInlineHintsAction : AnAction() { @@ -309,4 +364,5 @@ fun isPossibleHintNearOffset(file: PsiFile, offset: Int): Boolean { fun MethodInfo.toPattern() = this.fullyQualifiedName + '(' + this.paramNames.joinToString(",") + ')' -private fun String.capitalize() = StringUtil.capitalizeWords(this, true) \ No newline at end of file +private fun String.capitalize() = StringUtil.capitalizeWords(this, true) +private fun String.capitalizeFirstLetter() = StringUtil.capitalize(this) \ No newline at end of file diff --git a/platform/platform-resources/src/META-INF/LangExtensions.xml b/platform/platform-resources/src/META-INF/LangExtensions.xml index 378105915c9e..e9ee2dc7cd28 100644 --- a/platform/platform-resources/src/META-INF/LangExtensions.xml +++ b/platform/platform-resources/src/META-INF/LangExtensions.xml @@ -704,6 +704,9 @@ com.intellij.codeInsight.hints.DisableCustomHintsOption + + com.intellij.codeInsight.hints.EnableCustomHintsOption + com.intellij.codeInsight.intention.impl.QuickEditAction Language Injection