我正在尝试在 VS Code 中创建一个键盘快捷键,当我使用它时,它会给我一个 1-100 之间的随机数字。
这是我目前拥有的 VS Code 片段。但是,当我使用它时,它会向我提供正文中的文本。
是否可以让它给我一个 1 到 100 的随机数?
"Print to console": {
"prefix": "makerandomnumber",
"body": [
"Math.floor(Math.random() * 100) + 1;"
],
"description": "Generate a random number"
}
一些想法:我编写的扩展可以轻松执行您想要的操作,因为它可以运行您的 JavaScript。 查找并转换。
进行此键绑定:
{
"key": "alt+q", // whatever you want
"command": "findInCurrentFile",
"args" : {
"description": "Insert a random number between 1 - 100",
"replace": [
"$${",
"return Math.floor(Math.random() * 100) + 1;",
"}$$",
],
"restrictFind": "selections",
"postCommands": "cancelSelection"
}
}
您会注意到这是一个键绑定而不是一个片段(尽管我已经在“snippefying”扩展中做了一些工作。
您可以使用 vscode 的内置代码片段变量来接近。 看这个片段:
"random": {
"prefix": "makerandomnumber",
"body": "${RANDOM/\\d{4}(\\d{2})/$1/}" // 00-99
},
${RANDOM}
产生一个 6 位随机数。 然后,该代码片段仅使用最后 2 位数字。 我认为这会产生 00-99 之间的数字,因此不适合您的用例。