diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/inlays/ToggleInlineHintsActionTest.kt b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/inlays/ToggleInlineHintsActionTest.kt new file mode 100644 index 000000000000..132087a8626d --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/inlays/ToggleInlineHintsActionTest.kt @@ -0,0 +1,82 @@ +/* + * Copyright 2000-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 com.intellij.codeInsight.daemon.inlays + +import com.intellij.codeInsight.hints.isPossibleHintNearOffset +import com.intellij.openapi.editor.ex.EditorSettingsExternalizable +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase +import org.assertj.core.api.Assertions.assertThat + +class ToggleInlineHintsActionTest : LightCodeInsightFixtureTestCase() { + + private var before: Boolean = false + + override fun setUp() { + super.setUp() + before = EditorSettingsExternalizable.getInstance().isShowParameterNameHints + EditorSettingsExternalizable.getInstance().isShowParameterNameHints = false + } + + override fun tearDown() { + EditorSettingsExternalizable.getInstance().isShowParameterNameHints = before + super.tearDown() + } + + fun `test is enabled near method with possible hints`() { + myFixture.configureByText("A.java", """" +class Test { + Test(int time) {} + void initialize(int loadTime) {} + static void s_test() { + Test test = new Test(10); + test.initialize(10000); + } +} +""") + + val file = myFixture.file + val caretModel = myFixture.editor.caretModel + + assertThat(caretModel.caretCount).isEqualTo(4) + + editor.caretModel.allCarets.forEach { + val hintCanBeAtCaret = isPossibleHintNearOffset(file, it.offset) + assertThat(hintCanBeAtCaret).isTrue() + } + } + + fun `test is disabled in random places`() { + myFixture.configureByText("A.java", """" +class Test { + static void s_test() { + int a = 2; + List list = null; + } +} +""") + + val file = myFixture.file + val caretModel = myFixture.editor.caretModel + + assertThat(caretModel.caretCount).isEqualTo(3) + + editor.caretModel.allCarets.forEach { + val hintCanBeAtCaret = isPossibleHintNearOffset(file, it.offset) + assertThat(hintCanBeAtCaret).isFalse() + } + } + +} \ No newline at end of file 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 cbbe24034532..aba36f650a9e 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt +++ b/platform/lang-impl/src/com/intellij/codeInsight/hints/PopupActions.kt @@ -23,6 +23,7 @@ import com.intellij.codeInsight.hints.settings.ParameterNameHintsSettings import com.intellij.codeInsight.intention.HighPriorityAction import com.intellij.codeInsight.intention.IntentionAction import com.intellij.injected.editor.EditorWindow +import com.intellij.openapi.actionSystem.ActionPlaces import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.CommonDataKeys @@ -98,16 +99,32 @@ class ToggleInlineHintsAction : AnAction() { } override fun update(e: AnActionEvent) { - if (InlayParameterHintsExtension.hasAnyExtensions()) { - e.presentation.isEnabledAndVisible = true - val isShow = EditorSettingsExternalizable.getInstance().isShowParameterNameHints - e.presentation.text = if (isShow) disableText else enableText + if (!InlayParameterHintsExtension.hasAnyExtensions()) { + e.presentation.isEnabledAndVisible = false + return } - else { - e.presentation.isEnabledAndVisible = false + + val isHintsShownNow = EditorSettingsExternalizable.getInstance().isShowParameterNameHints + e.presentation.text = if (isHintsShownNow) disableText else enableText + e.presentation.isEnabledAndVisible = true + + if (isInMainEditorPopup(e)) { + val file = CommonDataKeys.PSI_FILE.getData(e.dataContext) ?: return + val editor = CommonDataKeys.EDITOR.getData(e.dataContext) ?: return + val caretOffset = editor.caretModel.offset + e.presentation.isEnabledAndVisible = !isHintsShownNow && isPossibleHintNearOffset(file, caretOffset) } } + private fun isInMainEditorPopup(e: AnActionEvent): Boolean { + if (e.place != ActionPlaces.EDITOR_POPUP) return false + + val editor = CommonDataKeys.EDITOR.getData(e.dataContext) ?: return false + val offset = editor.caretModel.offset + + return !editor.inlayModel.hasInlineElementAt(offset) + } + override fun actionPerformed(e: AnActionEvent) { val settings = EditorSettingsExternalizable.getInstance() val before = settings.isShowParameterNameHints @@ -156,4 +173,19 @@ private fun addMethodAtCaretToBlackList(editor: Editor, file: PsiFile) { ParameterNameHintsSettings.getInstance().addIgnorePattern(file.language, pattern) refreshAllOpenEditors() -} \ No newline at end of file +} + +fun isPossibleHintNearOffset(file: PsiFile, offset: Int): Boolean { + val hintProvider = InlayParameterHintsExtension.forLanguage(file.language) ?: return false + + var element = file.findElementAt(offset) + for (i in 0..3) { + if (element == null) return false + + val hints = hintProvider.getParameterHints(element) + if (hints.isNotEmpty()) return true + element = element.parent + } + + return false +} diff --git a/platform/platform-resources/src/idea/LangActions.xml b/platform/platform-resources/src/idea/LangActions.xml index 79fc2e5f0658..a0c79c8bff5b 100644 --- a/platform/platform-resources/src/idea/LangActions.xml +++ b/platform/platform-resources/src/idea/LangActions.xml @@ -306,6 +306,7 @@ +