简体   繁体   中英

VS code extension : Add new keybindings in package.json based on a boolean value in extension configuration

I have created a vs code extension with these contributes in package.json

Here I have a configuration called addKeyBoardShortcuts which is a boolean and when user checks this I want the keybindings array to be updated only when the boolean is true

Right now the editor.emmet.action.incrementNumberByOne command keybinding is always there when this extension is enabled, but I want this keybinding to only be present when addKeyBoardShortcuts is checked by the user.

Any help is much appreciated !

  "contributes": {
    "keybindings": [
      {
          "command": "editor.emmet.action.incrementNumberByOne",
          "key": "ctrl+shift+i",
          "mac": "cmd+shift+i"
      }
    ],
    "configuration": {
      "type": "object",
      "title": "Rem to Px comment configuration",
      "properties": {
        "remToPxComment.commentColor": {
          "type": "string",
          "default": "#36C210",
          "description": "Decoration color for the comment value"
        },
        "remToPxComment.remConversionValue": {
          "type": "number",
          "default": "16",
          "description": "value to convert px to rem, default is 16px"
        },
        "remToPxComment.convertToRemOrPx": {
          "type": "string",
          "default": "px",
          "enum": [
            "px",
            "rem"
          ],
          "description": "tell if conversion is from rem to px to the other way around"
        },
        "remToPxComment.addKeyBoardShortcuts": {
          "type": "boolean",
          "default": false
        }
      }
    }
  },

As I mentioned in the comment, if you are trying to avoid showing the contributed keybinding in the Keyboard Shortcuts editor I don't think that is possible. You can disable/enable it based on your contributed setting pretty easily though.

In your package.json :

  "configuration": [
   {
    "title": "Folder Operations",
    "properties": {
     "folder-operations.enableKeybinding": {       // your setting name
      "type": "boolean",
      "scope": "machine",
      "default": true,
      "markdownDescription": "your description here"
     }
    }
   }
  ],
 "keybindings": [
   {
     "command": "editor.emmet.action.incrementNumberByOne",
     "key": "ctrl+shift+i",
     "mac": "cmd+shift+i",
     "when": "config.folder-operations.enableKeybinding"  // your extension
   }
 ]
]

The context key config.folder-operations.enableKeybinding will be true or false depending on the value of your setting - whether it remains at the default or is changed by the user. Start the context key with the config. and vscode will automatically retrieve the setting's value each time the keybinding is triggered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM