KeymapUtil#getPrimaryShortcut

GitOrigin-RevId: 28370d4a5bda45fdb27537601e4a9af151ef39ba
This commit is contained in:
Tagir Valeev
2020-01-26 14:15:12 +07:00
committed by intellij-monorepo-bot
parent 2324f6f5d4
commit d7869b5a91
10 changed files with 40 additions and 37 deletions

View File

@@ -30,6 +30,7 @@ import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypeManager; import com.intellij.openapi.fileTypes.FileTypeManager;
import com.intellij.openapi.keymap.Keymap; import com.intellij.openapi.keymap.Keymap;
import com.intellij.openapi.keymap.KeymapManager; import com.intellij.openapi.keymap.KeymapManager;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.Messages;
@@ -476,14 +477,13 @@ public class OverrideImplementUtil extends OverrideImplementExploreUtil {
final boolean toImplement, final boolean toImplement,
@NotNull MemberChooser<PsiMethodMember> chooser) { @NotNull MemberChooser<PsiMethodMember> chooser) {
final JComponent preferredFocusedComponent = chooser.getPreferredFocusedComponent(); final JComponent preferredFocusedComponent = chooser.getPreferredFocusedComponent();
final Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
@NonNls final String s = toImplement ? "OverrideMethods" : "ImplementMethods"; @NonNls final String s = toImplement ? "OverrideMethods" : "ImplementMethods";
final Shortcut[] shortcuts = keymap.getShortcuts(s); final Shortcut shortcut = KeymapUtil.getPrimaryShortcut(s);
if (shortcuts.length > 0 && shortcuts[0] instanceof KeyboardShortcut) { if (shortcut instanceof KeyboardShortcut) {
preferredFocusedComponent.getInputMap().put( preferredFocusedComponent.getInputMap().put(
((KeyboardShortcut)shortcuts[0]).getFirstKeyStroke(), s ((KeyboardShortcut)shortcut).getFirstKeyStroke(), s
); );
preferredFocusedComponent.getActionMap().put( preferredFocusedComponent.getActionMap().put(

View File

@@ -396,17 +396,16 @@ public class JavaVariableInplaceIntroducer extends AbstractJavaInplaceIntroducer
final PsiType type, final PsiType type,
final boolean hasTypeSuggestion) { final boolean hasTypeSuggestion) {
final VariablesProcessor processor = ReassignVariableUtil.findVariablesOfType(declaration, type); final VariablesProcessor processor = ReassignVariableUtil.findVariablesOfType(declaration, type);
final Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
if (processor.size() > 0) { if (processor.size() > 0) {
final Shortcut[] shortcuts = keymap.getShortcuts("IntroduceVariable"); final Shortcut shortcut = KeymapUtil.getPrimaryShortcut("IntroduceVariable");
if (shortcuts.length > 0) { if (shortcut != null) {
return "Press " + KeymapUtil.getShortcutText(shortcuts[0]) + " to reassign existing variable"; return "Press " + KeymapUtil.getShortcutText(shortcut) + " to reassign existing variable";
} }
} }
if (hasTypeSuggestion) { if (hasTypeSuggestion) {
final Shortcut[] shortcuts = keymap.getShortcuts("PreviousTemplateVariable"); final Shortcut shortcut = KeymapUtil.getPrimaryShortcut("PreviousTemplateVariable");
if (shortcuts.length > 0) { if (shortcut != null) {
return "Press " + KeymapUtil.getShortcutText(shortcuts[0]) + " to change type"; return "Press " + KeymapUtil.getShortcutText(shortcut) + " to change type";
} }
} }
return null; return null;

View File

@@ -76,8 +76,7 @@ public class ExternalSystemShortcutsManager implements Disposable {
} }
public boolean hasShortcuts(@NotNull String actionId) { public boolean hasShortcuts(@NotNull String actionId) {
Keymap activeKeymap = KeymapManager.getInstance().getActiveKeymap(); return KeymapUtil.getPrimaryShortcut(actionId) != null;
return activeKeymap.getShortcuts(actionId).length > 0;
} }
private Shortcut @NotNull [] getShortcuts(@Nullable String projectPath, @Nullable String taskName) { private Shortcut @NotNull [] getShortcuts(@Nullable String projectPath, @Nullable String taskName) {

View File

@@ -46,9 +46,8 @@ public class DaemonEditorPopup extends PopupHandler {
ActionManager actionManager = ActionManager.getInstance(); ActionManager actionManager = ActionManager.getInstance();
DefaultActionGroup actionGroup = new DefaultActionGroup(); DefaultActionGroup actionGroup = new DefaultActionGroup();
Keymap keymap = KeymapManager.getInstance().getActiveKeymap(); Shortcut shortcut = KeymapUtil.getPrimaryShortcut("GotoNextError");
Shortcut[] shortcuts = keymap.getShortcuts("GotoNextError"); String shortcutText = shortcut != null ? " (" + KeymapUtil.getShortcutText(shortcut) + ")" : "";
String shortcutText = shortcuts.length > 0 ? " (" + KeymapUtil.getShortcutText(shortcuts[0]) + ")" : "";
DefaultActionGroup gotoGroup = new DefaultActionGroup("'Next Error' Action" + shortcutText + " Goes Through", true); DefaultActionGroup gotoGroup = new DefaultActionGroup("'Next Error' Action" + shortcutText + " Goes Through", true);
gotoGroup.add(new ToggleAction(EditorBundle.message("errors.panel.go.to.errors.first.radio")) { gotoGroup.add(new ToggleAction(EditorBundle.message("errors.panel.go.to.errors.first.radio")) {
@Override @Override

View File

@@ -614,10 +614,9 @@ public abstract class InplaceRefactoring {
} }
protected void showDialogAdvertisement(final String actionId) { protected void showDialogAdvertisement(final String actionId) {
final Keymap keymap = KeymapManager.getInstance().getActiveKeymap(); final Shortcut shortcut = KeymapUtil.getPrimaryShortcut(actionId);
final Shortcut[] shortcuts = keymap.getShortcuts(actionId); if (shortcut != null) {
if (shortcuts.length > 0) { setAdvertisementText("Press " + KeymapUtil.getShortcutText(shortcut) + " to show dialog with more options");
setAdvertisementText("Press " + KeymapUtil.getShortcutText(shortcuts[0]) + " to show dialog with more options");
} }
} }

View File

@@ -132,10 +132,9 @@ public class GotoTestOrCodeHandler extends GotoTargetHandler {
@Override @Override
protected String getAdText(PsiElement source, int length) { protected String getAdText(PsiElement source, int length) {
if (length > 0 && !TestFinderHelper.isTest(source)) { if (length > 0 && !TestFinderHelper.isTest(source)) {
final Keymap keymap = KeymapManager.getInstance().getActiveKeymap(); final Shortcut shortcut = KeymapUtil.getPrimaryShortcut(DefaultRunExecutor.getRunExecutorInstance().getContextActionId());
final Shortcut[] shortcuts = keymap.getShortcuts(DefaultRunExecutor.getRunExecutorInstance().getContextActionId()); if (shortcut != null) {
if (shortcuts.length > 0) { return ("Press " + KeymapUtil.getShortcutText(shortcut) + " to run selected tests");
return ("Press " + KeymapUtil.getShortcutText(shortcuts[0]) + " to run selected tests");
} }
} }
return null; return null;

View File

@@ -203,6 +203,17 @@ public class KeymapUtil {
return new CustomShortcutSet(keymapManager.getActiveKeymap().getShortcuts(actionId)); return new CustomShortcutSet(keymapManager.getActiveKeymap().getShortcuts(actionId));
} }
/**
* @param actionId action to find the shortcut for
* @return first keyboard shortcut that activates given action in active keymap; null if not found
*/
@Nullable
public static Shortcut getPrimaryShortcut(@Nullable String actionId) {
KeymapManager keymapManager = KeymapManager.getInstance();
if (keymapManager == null || actionId == null) return null;
return ArrayUtil.getFirstElement(keymapManager.getActiveKeymap().getShortcuts(actionId));
}
@NotNull @NotNull
public static String getFirstKeyboardShortcutText(@NotNull String actionId) { public static String getFirstKeyboardShortcutText(@NotNull String actionId) {
for (Shortcut shortcut : getActiveKeymapShortcuts(actionId).getShortcuts()) { for (Shortcut shortcut : getActiveKeymapShortcuts(actionId).getShortcuts()) {

View File

@@ -110,10 +110,9 @@ final class AntTargetNodeDescriptor extends AntNodeDescriptor {
} }
public static boolean addShortcutText(String actionId, CompositeAppearance appearance) { public static boolean addShortcutText(String actionId, CompositeAppearance appearance) {
Keymap activeKeymap = KeymapManager.getInstance().getActiveKeymap(); Shortcut shortcut = KeymapUtil.getPrimaryShortcut(actionId);
Shortcut[] shortcuts = activeKeymap.getShortcuts(actionId); if (shortcut != null) {
if (shortcuts != null && shortcuts.length > 0) { appearance.getEnding().addText(" (" + KeymapUtil.getShortcutText(shortcut) + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
appearance.getEnding().addText(" (" + KeymapUtil.getShortcutText(shortcuts[0]) + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
return true; return true;
} else return false; } else return false;
} }

View File

@@ -61,10 +61,9 @@ public abstract class GrInplaceVariableIntroducer extends GrAbstractInplaceIntro
@Nullable @Nullable
private static String getAdvertisementText() { private static String getAdvertisementText() {
final Keymap keymap = KeymapManager.getInstance().getActiveKeymap(); final Shortcut shortcut = KeymapUtil.getPrimaryShortcut("PreviousTemplateVariable");
final Shortcut[] shortcuts = keymap.getShortcuts("PreviousTemplateVariable"); if (shortcut != null) {
if (shortcuts.length > 0) { return "Press " + KeymapUtil.getShortcutText(shortcut) + " to change type";
return "Press " + KeymapUtil.getShortcutText(shortcuts[0]) + " to change type";
} }
return null; return null;
} }

View File

@@ -99,14 +99,13 @@ internal class BaseOpenInBrowserAction(private val browser: WebBrowser) : DumbAw
var description = templatePresentation.text var description = templatePresentation.text
if (ActionPlaces.CONTEXT_TOOLBAR == e.place) { if (ActionPlaces.CONTEXT_TOOLBAR == e.place) {
val shortcutInfo = buildString { val shortcutInfo = buildString {
val shortcuts = KeymapManager.getInstance().activeKeymap.getShortcuts("WebOpenInAction") val shortcut = KeymapUtil.getPrimaryShortcut("WebOpenInAction")
val exists = shortcuts.isNotEmpty() if (shortcut != null) {
if (exists) { append(KeymapUtil.getShortcutText(shortcut))
append(KeymapUtil.getShortcutText(shortcuts[0]))
} }
if (HtmlUtil.isHtmlFile(result.file)) { if (HtmlUtil.isHtmlFile(result.file)) {
append(if (exists) ", " else "") append(if (shortcut != null) ", " else "")
append("hold Shift to open URL of local file") append("hold Shift to open URL of local file")
} }
} }