mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[Parameter Name Hints] add ToggleInlineHints action to parameter hints popup menu. In regular popup menu action will be shown only if hints are disabled and there is a hint to show at caret.
This commit is contained in:
+82
@@ -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 T<caret>est(<caret>10);
|
||||
test.initial<caret>ize(100<caret>00);
|
||||
}
|
||||
}
|
||||
""")
|
||||
|
||||
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_<caret>test() {
|
||||
int a <caret>= 2;
|
||||
Li<caret>st<String> 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()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -306,6 +306,7 @@
|
||||
|
||||
<action id="ToggleInlineHintsAction" class="com.intellij.codeInsight.hints.ToggleInlineHintsAction">
|
||||
<add-to-group group-id="EditorPopupMenu"/>
|
||||
<add-to-group group-id="ParameterNameHints"/>
|
||||
</action>
|
||||
|
||||
<!-- Analyze -->
|
||||
|
||||
Reference in New Issue
Block a user