From 7c146fd7e8f72eba8cbb4ff1866d618519c6707e Mon Sep 17 00:00:00 2001 From: Dmitry Batrak Date: Tue, 26 Sep 2017 12:22:28 +0300 Subject: [PATCH] IDEA-178503 change shortcuts to switch method overloads --- .../JavaMethodOverloadSwitchDownAction.java | 24 +++++ .../JavaMethodOverloadSwitchHandler.java | 88 ++----------------- .../JavaMethodOverloadSwitchUpAction.java | 24 +++++ .../completion/CompletionHintsTest.java | 14 +-- .../hint/ParameterInfoComponent.java | 29 ++++-- .../hint/ParameterInfoController.java | 6 -- .../openapi/actionSystem/IdeActions.java | 3 + .../src/messages/ActionsBundle.properties | 3 + .../src/messages/CodeInsightBundle.properties | 3 +- .../src/keymaps/$default.xml | 3 + .../platform-resources/src/keymaps/Emacs.xml | 1 - .../src/keymaps/Mac OS X 10.5+.xml | 7 ++ .../src/keymaps/Mac OS X.xml | 7 ++ resources/src/META-INF/IdeaPlugin.xml | 7 +- 14 files changed, 109 insertions(+), 110 deletions(-) create mode 100644 java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchDownAction.java create mode 100644 java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchUpAction.java diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchDownAction.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchDownAction.java new file mode 100644 index 000000000000..1aef923d6217 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchDownAction.java @@ -0,0 +1,24 @@ +/* + * Copyright 2000-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. + */ +package com.intellij.codeInsight.editorActions; + +import com.intellij.openapi.editor.actionSystem.EditorAction; + +public class JavaMethodOverloadSwitchDownAction extends EditorAction { + public JavaMethodOverloadSwitchDownAction() { + super(new JavaMethodOverloadSwitchHandler(false)); + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchHandler.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchHandler.java index 502b2b994d64..75a4a7c8259f 100644 --- a/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchHandler.java +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchHandler.java @@ -22,13 +22,11 @@ import com.intellij.codeInsight.hint.ParameterInfoController; import com.intellij.codeInsight.hints.ParameterHintsPass; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; -import com.intellij.openapi.actionSystem.IdeActions; import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.editor.Caret; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.EditorModificationUtil; import com.intellij.openapi.editor.actionSystem.EditorActionHandler; -import com.intellij.openapi.editor.actionSystem.EditorActionManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.Key; @@ -43,23 +41,18 @@ import java.util.Arrays; import java.util.HashMap; import java.util.Map; -abstract class JavaMethodOverloadSwitchHandler extends EditorActionHandler { - private static final Key SWITCH_DISABLED = Key.create("switch.disabled"); +class JavaMethodOverloadSwitchHandler extends EditorActionHandler { private static final Key> ENTERED_PARAMETERS = Key.create("entered.parameters"); - private final EditorActionHandler myOriginalHandler; private final boolean mySwitchUp; - private JavaMethodOverloadSwitchHandler(EditorActionHandler originalHandler, boolean up) { - myOriginalHandler = originalHandler; + JavaMethodOverloadSwitchHandler(boolean up) { mySwitchUp = up; } @Override protected boolean isEnabledForCaret(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) { - if (myOriginalHandler.isEnabled(editor, caret, dataContext)) return true; - - if (editor.getUserData(SWITCH_DISABLED) != null || - !CodeInsightSettings.getInstance().SHOW_PARAMETER_NAME_HINTS_ON_COMPLETION || !ParameterInfoController.existsForEditor(editor)) return false; + if (!CodeInsightSettings.getInstance().SHOW_PARAMETER_NAME_HINTS_ON_COMPLETION || + !ParameterInfoController.existsForEditor(editor)) return false; Project project = CommonDataKeys.PROJECT.getData(dataContext); if (project == null) return false; @@ -82,14 +75,10 @@ abstract class JavaMethodOverloadSwitchHandler extends EditorActionHandler { @Override protected void doExecute(@NotNull Editor editor, @Nullable Caret caret, DataContext dataContext) { Project project = CommonDataKeys.PROJECT.getData(dataContext); - if (project != null && editor.getUserData(SWITCH_DISABLED) == null && - CodeInsightSettings.getInstance().SHOW_PARAMETER_NAME_HINTS_ON_COMPLETION && + if (project != null && CodeInsightSettings.getInstance().SHOW_PARAMETER_NAME_HINTS_ON_COMPLETION && ParameterInfoController.existsWithVisibleHintForEditor(editor, false)) { doSwitch(editor, caret == null ? editor.getCaretModel().getPrimaryCaret() : caret, project); } - else { - myOriginalHandler.execute(editor, caret, dataContext); - } } private void doSwitch(@NotNull final Editor editor, @NotNull Caret caret, @NotNull Project project) { @@ -172,71 +161,4 @@ abstract class JavaMethodOverloadSwitchHandler extends EditorActionHandler { PsiParameter parameter = method.getParameterList().getParameters()[parameterIndex]; return parameter.getName() + ":" + parameter.getType().getCanonicalText(); } - - public static class Up extends JavaMethodOverloadSwitchHandler { - public Up(EditorActionHandler originalHandler) { - super(originalHandler, true); - } - } - - public static class Down extends JavaMethodOverloadSwitchHandler { - public Down(EditorActionHandler originalHandler) { - super(originalHandler, false); - } - } - - public static class UpInEditor extends UpDownInEditor { - public UpInEditor(EditorActionHandler originalHandler) { - super(originalHandler, true); - } - } - - public static class DownInEditor extends UpDownInEditor { - public DownInEditor(EditorActionHandler originalHandler) { - super(originalHandler, false); - } - } - - private static abstract class UpDownInEditor extends EditorActionHandler { - private final EditorActionHandler myOriginalHandler; - private final boolean myUp; - - private UpDownInEditor(EditorActionHandler originalHandler, boolean up) { - myOriginalHandler = originalHandler; - myUp = up; - } - - @Override - protected boolean isEnabledForCaret(@NotNull Editor editor, @NotNull Caret caret, DataContext dataContext) { - return myOriginalHandler.isEnabled(editor, caret, dataContext) || - isEnabled(editor); - } - - private static boolean isEnabled(@NotNull Editor editor) { - return CodeInsightSettings.getInstance().SHOW_PARAMETER_NAME_HINTS_ON_COMPLETION && - ParameterInfoController.existsWithVisibleHintForEditor(editor, false); - } - - @Override - protected void doExecute(@NotNull Editor editor, @Nullable Caret caret, DataContext dataContext) { - if (isEnabled(editor)) { - ParameterInfoController.hideAllHints(editor); - } - // hints can be hidden asynchronously (with animation), so we disable switching explicitly here - editor.putUserData(SWITCH_DISABLED, Boolean.TRUE); - try { - if (myOriginalHandler.isEnabled(editor, caret, dataContext)) { - myOriginalHandler.execute(editor, caret, dataContext); - } - else { - EditorActionManager.getInstance().getActionHandler(myUp ? IdeActions.ACTION_EDITOR_MOVE_CARET_UP - : IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN) - .execute(editor, caret, dataContext); - } - } - finally { - editor.putUserData(SWITCH_DISABLED, null); - } - } - } } diff --git a/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchUpAction.java b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchUpAction.java new file mode 100644 index 000000000000..50ba335ad4c3 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/editorActions/JavaMethodOverloadSwitchUpAction.java @@ -0,0 +1,24 @@ +/* + * Copyright 2000-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. + */ +package com.intellij.codeInsight.editorActions; + +import com.intellij.openapi.editor.actionSystem.EditorAction; + +public class JavaMethodOverloadSwitchUpAction extends EditorAction { + public JavaMethodOverloadSwitchUpAction() { + super(new JavaMethodOverloadSwitchHandler(true)); + } +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/CompletionHintsTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/CompletionHintsTest.java index 188beb9a2126..208840250e02 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/CompletionHintsTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/CompletionHintsTest.java @@ -155,7 +155,7 @@ public class CompletionHintsTest extends LightFixtureCompletionTestCase { complete("toChars(int codePoint)"); checkResultWithInlays("class C { void m() { Character.toChars() } }"); showParameterInfo(); - down(); + methodOverloadDown(); checkResultWithInlays( "class C { void m() { Character.toChars(, , ) } }"); } @@ -166,7 +166,7 @@ public class CompletionHintsTest extends LightFixtureCompletionTestCase { type("123"); checkResultWithInlays("class C { void m() { Character.toChars(123) } }"); showParameterInfo(); - down(); + methodOverloadDown(); checkResultWithInlays("class C { void m() { Character.toChars(123, , ) } }"); } @@ -183,7 +183,7 @@ public class CompletionHintsTest extends LightFixtureCompletionTestCase { " void m() { some(, , ) }\n" + "}"); showParameterInfo(); - down(); + methodOverloadDown(); waitForAllAsyncStuff(); checkResultWithInlays("class C {\n" + " int some(int from, int to) { return 0; }\n" + @@ -589,7 +589,7 @@ public class CompletionHintsTest extends LightFixtureCompletionTestCase { waitForAllAsyncStuff(); checkHintContents("@NotNull String key\n" + "@NotNull String key, String def"); - myFixture.performEditorAction(IdeActions.ACTION_LOOKUP_UP); + myFixture.performEditorAction(IdeActions.ACTION_EDITOR_MOVE_CARET_UP); waitForAllAsyncStuff(); checkResultWithInlays("class C { void m() { System.getProperty(, ) } }"); checkHintContents(null); @@ -604,7 +604,7 @@ public class CompletionHintsTest extends LightFixtureCompletionTestCase { waitForAllAsyncStuff(); checkHintContents("@NotNull String key\n" + "@NotNull String key, String def"); - myFixture.performEditorAction(IdeActions.ACTION_LOOKUP_DOWN); + myFixture.performEditorAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN); waitForAllAsyncStuff(); checkResultWithInlays("class C { void m() { System.getProperty(, ) } }"); checkHintContents(null); @@ -650,8 +650,8 @@ public class CompletionHintsTest extends LightFixtureCompletionTestCase { myFixture.performEditorAction(IdeActions.ACTION_EDITOR_MOVE_CARET_RIGHT); } - private void down() { - myFixture.performEditorAction(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN); + private void methodOverloadDown() { + myFixture.performEditorAction(IdeActions.ACTION_METHOD_OVERLOAD_SWITCH_DOWN); } private void home() { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoComponent.java b/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoComponent.java index 20f62d42198a..6ad194d1723a 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoComponent.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoComponent.java @@ -20,8 +20,10 @@ import com.google.common.collect.ImmutableMap; import com.intellij.codeInsight.CodeInsightBundle; import com.intellij.lang.parameterInfo.ParameterInfoHandler; import com.intellij.lang.parameterInfo.ParameterInfoUIContextEx; +import com.intellij.openapi.actionSystem.IdeActions; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.keymap.KeymapUtil; import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; @@ -127,13 +129,22 @@ public class ParameterInfoComponent extends JPanel { pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); add(pane, BorderLayout.CENTER); - myShortcutLabel = new JLabel(CodeInsightBundle.message("parameter.info.switch.overload.shortcuts")); - myShortcutLabel.setForeground(new JBColor(0x787878, 0x787878)); - Font labelFont = UIUtil.getLabelFont(); - myShortcutLabel.setFont(labelFont.deriveFont(labelFont.getSize2D() - (SystemInfo.isWindows ? 1 : 2))); - myShortcutLabel.setBorder(new JBEmptyBorder(3, 0, 0, 0)); - add(myShortcutLabel, BorderLayout.SOUTH); - + String upShortcut = KeymapUtil.getFirstKeyboardShortcutText(IdeActions.ACTION_METHOD_OVERLOAD_SWITCH_UP); + String downShortcut = KeymapUtil.getFirstKeyboardShortcutText(IdeActions.ACTION_METHOD_OVERLOAD_SWITCH_DOWN); + if (upShortcut.isEmpty() && downShortcut.isEmpty()) { + myShortcutLabel = null; + } + else { + myShortcutLabel = new JLabel( + upShortcut.isEmpty() || downShortcut.isEmpty() + ? CodeInsightBundle.message("parameter.info.switch.overload.shortcuts.single", upShortcut.isEmpty() ? downShortcut : upShortcut) + : CodeInsightBundle.message("parameter.info.switch.overload.shortcuts", upShortcut, downShortcut)); + myShortcutLabel.setForeground(new JBColor(0x787878, 0x787878)); + Font labelFont = UIUtil.getLabelFont(); + myShortcutLabel.setFont(labelFont.deriveFont(labelFont.getSize2D() - (SystemInfo.isWindows ? 1 : 2))); + myShortcutLabel.setBorder(new JBEmptyBorder(3, 0, 0, 0)); + add(myShortcutLabel, BorderLayout.SOUTH); + } myCurrentParameterIndex = -1; } @@ -266,7 +277,9 @@ public class ParameterInfoComponent extends JPanel { } } - myShortcutLabel.setVisible(!singleParameterInfo && myObjects.length > 1 && myHandler.supportsOverloadSwitching()); + if (myShortcutLabel != null) { + myShortcutLabel.setVisible(!singleParameterInfo && myObjects.length > 1 && myHandler.supportsOverloadSwitching()); + } invalidate(); validate(); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoController.java b/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoController.java index ec0c9b18c352..d087c7b47d14 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoController.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/hint/ParameterInfoController.java @@ -131,12 +131,6 @@ public class ParameterInfoController implements Disposable { return getAllControllers(editor).stream().anyMatch(c -> c.isHintShown(anyHintType)); } - public static void hideAllHints(@NotNull Editor editor) { - getAllControllers(editor).forEach(c -> { - if (c.myHint.isVisible()) c.myHint.hide(); - }); - } - public boolean isHintShown(boolean anyType) { return myHint.isVisible() && (!mySingleParameterInfo || anyType); } diff --git a/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java b/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java index 98b4f43523d0..289b5bed6361 100644 --- a/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java +++ b/platform/platform-api/src/com/intellij/openapi/actionSystem/IdeActions.java @@ -337,4 +337,7 @@ public interface IdeActions { String ACTION_SWITCHER = "Switcher"; @NonNls String INSPECTION_TOOL_WINDOW_TREE_POPUP = "InspectionToolWindow.TreePopup"; + + @NonNls String ACTION_METHOD_OVERLOAD_SWITCH_UP = "MethodOverloadSwitchUp"; + @NonNls String ACTION_METHOD_OVERLOAD_SWITCH_DOWN = "MethodOverloadSwitchDown"; } diff --git a/platform/platform-resources-en/src/messages/ActionsBundle.properties b/platform/platform-resources-en/src/messages/ActionsBundle.properties index e70a69758d4c..8a5fa2d948b1 100644 --- a/platform/platform-resources-en/src/messages/ActionsBundle.properties +++ b/platform/platform-resources-en/src/messages/ActionsBundle.properties @@ -1613,3 +1613,6 @@ action.TogglePresentationMode.exit=Exit Presentation Mode action.TogglePresentationMode.enter=Enter Presentation Mode action.EmojiAndSymbols.description=Input special characters using macOS system palette + +action.MethodOverloadSwitchUp.text=Previous Method Overload +action.MethodOverloadSwitchDown.text=Next Method Overload \ No newline at end of file diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index bc0956495caa..bbe31c0f782a 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -559,4 +559,5 @@ block.comment.nested.comment=Selected region contained block {0, choice, 1#comme intention.unroll.loop.family=Unroll loop -parameter.info.switch.overload.shortcuts=Switch with \u2191 or \u2193 \ No newline at end of file +parameter.info.switch.overload.shortcuts=Switch with {0} or {1} +parameter.info.switch.overload.shortcuts.single=Switch with {0} \ No newline at end of file diff --git a/platform/platform-resources/src/keymaps/$default.xml b/platform/platform-resources/src/keymaps/$default.xml index 3d3ea5246808..ab8d8a4097c5 100644 --- a/platform/platform-resources/src/keymaps/$default.xml +++ b/platform/platform-resources/src/keymaps/$default.xml @@ -77,6 +77,9 @@ + + + diff --git a/platform/platform-resources/src/keymaps/Emacs.xml b/platform/platform-resources/src/keymaps/Emacs.xml index 856a603d8347..49b90da7f04f 100644 --- a/platform/platform-resources/src/keymaps/Emacs.xml +++ b/platform/platform-resources/src/keymaps/Emacs.xml @@ -24,7 +24,6 @@ - diff --git a/platform/platform-resources/src/keymaps/Mac OS X 10.5+.xml b/platform/platform-resources/src/keymaps/Mac OS X 10.5+.xml index c97cd4448f91..aac21c82cc38 100644 --- a/platform/platform-resources/src/keymaps/Mac OS X 10.5+.xml +++ b/platform/platform-resources/src/keymaps/Mac OS X 10.5+.xml @@ -594,4 +594,11 @@ + + + + + + + \ No newline at end of file diff --git a/platform/platform-resources/src/keymaps/Mac OS X.xml b/platform/platform-resources/src/keymaps/Mac OS X.xml index 247e1c6255c3..5c8c0793a187 100644 --- a/platform/platform-resources/src/keymaps/Mac OS X.xml +++ b/platform/platform-resources/src/keymaps/Mac OS X.xml @@ -389,4 +389,11 @@ + + + + + + + diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 0580eb84e008..e44a51622943 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -1437,10 +1437,6 @@ - - - - + + +