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.keymap.Keymap;
import com.intellij.openapi.keymap.KeymapManager;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
@@ -476,14 +477,13 @@ public class OverrideImplementUtil extends OverrideImplementExploreUtil {
final boolean toImplement,
@NotNull MemberChooser<PsiMethodMember> chooser) {
final JComponent preferredFocusedComponent = chooser.getPreferredFocusedComponent();
final Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
@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(
((KeyboardShortcut)shortcuts[0]).getFirstKeyStroke(), s
((KeyboardShortcut)shortcut).getFirstKeyStroke(), s
);
preferredFocusedComponent.getActionMap().put(

View File

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

View File

@@ -76,8 +76,7 @@ public class ExternalSystemShortcutsManager implements Disposable {
}
public boolean hasShortcuts(@NotNull String actionId) {
Keymap activeKeymap = KeymapManager.getInstance().getActiveKeymap();
return activeKeymap.getShortcuts(actionId).length > 0;
return KeymapUtil.getPrimaryShortcut(actionId) != null;
}
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();
DefaultActionGroup actionGroup = new DefaultActionGroup();
Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
Shortcut[] shortcuts = keymap.getShortcuts("GotoNextError");
String shortcutText = shortcuts.length > 0 ? " (" + KeymapUtil.getShortcutText(shortcuts[0]) + ")" : "";
Shortcut shortcut = KeymapUtil.getPrimaryShortcut("GotoNextError");
String shortcutText = shortcut != null ? " (" + KeymapUtil.getShortcutText(shortcut) + ")" : "";
DefaultActionGroup gotoGroup = new DefaultActionGroup("'Next Error' Action" + shortcutText + " Goes Through", true);
gotoGroup.add(new ToggleAction(EditorBundle.message("errors.panel.go.to.errors.first.radio")) {
@Override

View File

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

View File

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

View File

@@ -203,6 +203,17 @@ public class KeymapUtil {
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
public static String getFirstKeyboardShortcutText(@NotNull String actionId) {
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) {
Keymap activeKeymap = KeymapManager.getInstance().getActiveKeymap();
Shortcut[] shortcuts = activeKeymap.getShortcuts(actionId);
if (shortcuts != null && shortcuts.length > 0) {
appearance.getEnding().addText(" (" + KeymapUtil.getShortcutText(shortcuts[0]) + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
Shortcut shortcut = KeymapUtil.getPrimaryShortcut(actionId);
if (shortcut != null) {
appearance.getEnding().addText(" (" + KeymapUtil.getShortcutText(shortcut) + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
return true;
} else return false;
}

View File

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

View File

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