VSpaceCode / vscode-which-key

which-key like menu for Visual Studio Code
https://vspacecode.github.io/docs/whichkey
MIT License
167 stars 17 forks source link

triggerKey Not Firing from edamagit #62

Open jaidetree opened 2 years ago

jaidetree commented 2 years ago

Bug description

Trying to create some bindings to recreate the use of forge to manage pull-requests. Reading the docs suggests triggerKey should do the job. If I bind it from settings.json to go through the normalModeKeyBindingsNonRecursive it works, but it rebinds the key everywhere.

Ideally the triggerKey binding should only function when in a magit buffer. I copied an example from how the x key is bound in magit like the following:

To Reproduce

  1. Open keybindings.json and add the following:
        {
            "key": "shift-2",
            "command": "whichkey.triggerKey",
            "args": {
                "key": "w"
            },
            "when": "editorTextFocus && editorLangId == 'magit' && vim.mode =~ /^(?!SearchInProgressMode|CommandlineInProgress).*$/"
        },
  2. Open up a magit status buffer SPC g s by default I think?
  3. Press shift+2 or @

Expected behavior

Given the configuration, my understanding is that it should trigger the window menu (SPC w) to open in the magit buffer. I'm using the window menu for test purposes but it shouldn't matter which menu is chosen, it has not worked with any of them.

Actual behavior

Nothing happens, the magit buffer stays open and I don't see the which-key menu.

Additional context

By replacing the command with workbench.action.showCommands the binding works and the command panel is displayed as expected which suggests whichkey.triggerKey is firing.

If you add the following to settings.json in a vim.normalModeKeyBindingsNonRecursive:

        {
            "before": [
                "@"
            ],
            "commands": [
                "vspacecode.space",
                {
                    "command": "whichkey.triggerKey",
                    "args": "w"
                }
            ]
        }

And press @ anywhere, it works as expected showing the window menu

Keybindings

Click to toggle contents of `keybindings.json`

```jsonc // Place your key bindings in this file to override the defaultsauto[] [ { "key": "ctrl+space", "command": "-extension.vim_ctrl+space", "when": "editorTextFocus && vim.active && vim.use && !inDebugRepl && vim.mode != 'Insert'" }, { "key": "cmd+k cmd+b", "command": "workbench.action.toggleSidebarVisibility" }, { "key": "ctrl+j", "command": "workbench.action.focusBelowGroup", "when": "editorTextFocus && vim.active && vim.use && !inDebugRepl" }, { "key": "ctrl+k", "command": "workbench.action.focusAboveGroup", "when": "editorTextFocus && vim.active && vim.use && !inDebugRepl" }, { "key": "ctrl+h", "command": "workbench.action.focusLeftGroup", "when": "editorTextFocus && vim.active && vim.use && !inDebugRepl" }, { "key": "ctrl+l", "command": "workbench.action.focusRightGroup", "when": "editorTextFocus && vim.active && vim.use && !inDebugRepl" }, { "key": "space", "command": "vspacecode.space", "when": "activeEditorGroupEmpty && focusedView == '' && !whichkeyActive && !inputFocus" }, { "key": "space", "command": "vspacecode.space", "when": "sideBarFocus && !inputFocus && !whichkeyActive" }, { "key": "tab", "command": "extension.vim_tab", "when": "editorFocus && vim.active && !inDebugRepl && vim.mode != 'Insert' && editorLangId != 'magit'" }, { "key": "tab", "command": "-extension.vim_tab", "when": "editorFocus && vim.active && !inDebugRepl && vim.mode != 'Insert'" }, { "key": "x", "command": "magit.discard-at-point", "when": "editorTextFocus && editorLangId == 'magit' && vim.mode =~ /^(?!SearchInProgressMode|CommandlineInProgress).*$/" }, { "key": "k", "command": "-magit.discard-at-point" }, { "key": "-", "command": "magit.reverse-at-point", "when": "editorTextFocus && editorLangId == 'magit' && vim.mode =~ /^(?!SearchInProgressMode|CommandlineInProgress).*$/" }, { "key": "v", "command": "-magit.reverse-at-point" }, { "key": "shift+-", "command": "magit.reverting", "when": "editorTextFocus && editorLangId == 'magit' && vim.mode =~ /^(?!SearchInProgressMode|CommandlineInProgress).*$/" }, { "key": "shift+v", "command": "-magit.reverting" }, { "key": "shift+o", "command": "magit.resetting", "when": "editorTextFocus && editorLangId == 'magit' && vim.mode =~ /^(?!SearchInProgressMode|CommandlineInProgress).*$/" }, { "key": "shift+x", "command": "-magit.resetting" }, { "key": "x", "command": "-magit.reset-mixed" }, { "key": "ctrl+u x", "command": "-magit.reset-hard" }, { "key": "y", "command": "-magit.show-refs" }, { "key": "y", "command": "vspacecode.showMagitRefMenu", "when": "editorTextFocus && editorLangId == 'magit' && vim.mode == 'Normal'" }, { "key": "ctrl+j", "command": "workbench.action.quickOpenSelectNext", "when": "inQuickOpen" }, { "key": "ctrl+k", "command": "workbench.action.quickOpenSelectPrevious", "when": "inQuickOpen" }, { "key": "ctrl+j", "command": "selectNextSuggestion", "when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus" }, { "key": "ctrl+k", "command": "selectPrevSuggestion", "when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus" }, { "key": "ctrl+l", "command": "acceptSelectedSuggestion", "when": "suggestWidgetMultipleSuggestions && suggestWidgetVisible && textInputFocus" }, { "key": "ctrl+j", "command": "showNextParameterHint", "when": "editorFocus && parameterHintsMultipleSignatures && parameterHintsVisible" }, { "key": "ctrl+k", "command": "showPrevParameterHint", "when": "editorFocus && parameterHintsMultipleSignatures && parameterHintsVisible" }, { "key": "ctrl+h", "command": "file-browser.stepOut", "when": "inFileBrowser" }, { "key": "ctrl+l", "command": "file-browser.stepIn", "when": "inFileBrowser" }, { "key": "shift-2", "command": "whichkey.triggerKey", "args": { "key": "w", "when": "editorTextFocus && editorLangId == 'magit' && vim.mode =~ /^(?!SearchInProgressMode|CommandlineInProgress).*$/" }, "when": "editorTextFocus && editorLangId == 'magit' && vim.mode =~ /^(?!SearchInProgressMode|CommandlineInProgress).*$/" }, ] ```

Settings

My settings file is gigantic right now having a copy of all the default which-key-bindings and is 7k lines long about 360 kb.

I've attached it in a zip manually below

settings.json.zip

System information

CPUs Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz (12 x 2600)
GPU Status 2d_canvas: enabled gpu_compositing: enabled metal: disabled_off multiple_raster_threads: enabled_on oop_rasterization: enabled opengl: enabled_on rasterization: enabled skia_renderer: disabled_off_ok video_decode: enabled webgl: enabled webgl2: enabled
Load (avg) 3, 3, 3
Memory (System) 32.00GB (0.19GB free)
Process Argv --crash-reporter-id 5a5beb4b-1aa2-4b88-b170-d9fb7e11c4bb
Screen Reader no
VM 0%

This tool is still great and I'm looking forward to using it more! 🙂

jaidetree commented 2 years ago

and must contain whichKeyVisible which limits this shortcut to be only applicable when the which-key menu is visible

I think I missed that caveat. That said is there a way to trigger a submenu without which-key already visible?

jaidetree commented 2 years ago

Found one solution:

{
  "key": "shift+2",
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "vspacecode.space",
      { "command": "whichkey.triggerKey", "args": "@" },
    ],
  },
  "when": "!whichKeyVisible && editorLangId == 'magit' && vim.mode =~ /^(?!SearchInProgressMode|CommandlineInProgress).*$/"
},

It works! But downside is requires the multiCommand extension. If there's a cleaner way, I'd love to see a better solution

stevenguh commented 2 years ago

Let me make sure I understand the problem. You are trying to have @ to open the which-key menu that's equal to SPC w when the current editor is magit editor.

Your workaround is probably the best bet currently. I thought about adding keys sequence to the show API which is used by vspacecode.space so I user can bind a command to do exactly what you are trying to do.

stevenguh commented 2 years ago

Also, per buffer remapping in vim was mentioned on https://github.com/VSCodeVim/Vim/issues/4765#issuecomment-706542295

jaidetree commented 2 years ago

Let me make sure I understand the problem. You are trying to have @ to open the which-key menu that's equal to SPC w when the current editor is magit editor.

That’s correct.

Your workaround is probably the best bet currently. I thought about adding keys sequence to the show API which is used by vspacecode.space so I user can bind a command to do exactly what you are trying to do.

I’m willing to give a PR for that a shot if you’re open to it.

Will try the per-buffer remapping approach as well

stevenguh commented 2 years ago

I’m willing to give a PR for that a shot if you’re open to it.

Sure :) Let me know if you have any questions.

Will try the per-buffer remapping approach as well

Unfortunately, that's just a thread mentioning that functionality doesn't exist today.

jaidetree commented 2 years ago

I’m willing to give a PR for that a shot if you’re open to it.

Sure :) Let me know if you have any questions.

Great will do!

Will try the per-buffer remapping approach as well

Unfortunately, that's just a thread mentioning that functionality doesn't exist today.

Oh my mistake, I misunderstood there but your reply probably just saved me a good chunk of time 😅