简体   繁体   English

VS 代码扩展:根据扩展配置中的 boolean 值在 package.json 中添加新的键绑定

[英]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我在 package.json 中创建了一个带有这些贡献的 vs 代码扩展

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在这里,我有一个名为addKeyBoardShortcuts的配置,它是一个 boolean,当用户检查它时,我希望仅当 boolean 为true时才更新keybindings数组

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.现在,当启用此扩展时, editor.emmet.action.incrementNumberByOne命令键绑定始终存在,但我希望此键绑定仅在用户检查addKeyBoardShortcuts时出现。

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.正如我在评论中提到的,如果您试图避免在Keyboard Shortcuts编辑器中显示贡献的键绑定,我认为这是不可能的。 You can disable/enable it based on your contributed setting pretty easily though.不过,您可以很容易地根据您提供的设置禁用/启用它。

In your package.json :在您的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.上下文键config.folder-operations.enableKeybinding将为真或假,具体取决于您的设置值 - 无论它保持默认值还是由用户更改。 Start the context key with the config.使用config. and vscode will automatically retrieve the setting's value each time the keybinding is triggered.每次触发键绑定时,vscode 都会自动检索设置的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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