pbb
pbb

Reputation: 165

VS Code keybinding: Accept quickfix codeAction

I'm trying to implement a small function by adding a shortcut for auto-correcting the last misspelled word, and this is what I get currently:

{
    "key": "cmd+l",
    "command": "extension.multiCommand.execute",
    "args": {
        "sequence": [
            "cSpell.goToPreviousSpellingIssue",
            "editor.action.quickFix",
            "workbench.action.navigateToLastEditLocation",
            "acceptSelectedSuggestionOnEnter",
            "acceptSelectedSuggestion"
        ]
    }
},

The idea is simple: jump back to the first misspelled word, and just select the first suggestion my spelling checker gives me, finally jump back to the previous edited position.

The code above is using multiCommand plug-in. Question is that I can't find any keystroke events to let me actually select the first suggestion my spelling checker gives me. As in the config, I'm using cSpell for checking my spelling. To be specific, after I hit cmd+l, this is what I get: snap shot Clearly, I manage to move to the previous spelling issue, and evoke quickFix to let the suggestion widget pop up, and then move my cursor back to where I was initially. Hence, the only problem is what is the event to select that suggestion?

Really appreciate every helps, or if there is a better method to do the same thing please tell me! (I have tried every keyword I can think of, and there are not many references out there both in the official document from VS Code and google)

Upvotes: 2

Views: 996

Answers (1)

Mark
Mark

Reputation: 181499

Because the quickfix menu is a different beast than a suggestions menu, the nextSuggestion or acceptSuggestion type of commands will not work in it. There is an open issue for navigation commands in the quickfix menu, see Missing keybinding for navigation in Quick Fix contextual menu .

But you can get what you want another way. I found Keybinding for applying a specific code action and in it is a method for applying a quickfix with a command:

{
  "key": "ctrl+shift+r ctrl+e",
  "command": "editor.action.codeAction",
  "args": {
    "kind": "refactor.extract.function",
    "apply": "first"
  }
}

Valid values for "apply":

"first" — Always automatically the first available code action.
"ifSingle" — Default. Automatically the code action if only one is available. Otherwise, show the context menu.
"never" — Always show the code action context menu, even if only a single code action is available.

Adapting that for your use case, try this:

{
  "key": "cmd+l",                      // "ctrl+l" for Windows
  "command": "extension.multiCommand.execute",
  "args": {
      "sequence": [
          "cSpell.goToPreviousSpellingIssue",
          {
            "command": "editor.action.codeAction",
            "args": {
              "kind": "quickfix",
              "apply": "first"   // you indicated you want the first choice
            }
          },
          // "workbench.action.navigateToLastEditLocation"
          "cursorUndo"               // better than the above I believe
      ]
  }
}

code action in a command demo

Upvotes: 2

Related Questions