如何在 Visual Studio 代码中生成 UUID?

How to generate a UUID in Visual Studio Code?

是否可以创建代码片段或任务 运行ner(或其他任何东西)来让我在 VSCode 中生成 UUID?这个问题也可以用更通用的方式解释:Can I 运行 an external tool/program and insert the result at the current cursor position.

谢谢。

很遗憾,您现在无法使用代码段执行此操作。我也不认为它可以通过任务工作。

我唯一能为你做的就是为你提供一个适用于 C#、TypeScript 和 Yaml 文件的 hacky 解决方案(在 VSCode 0.9.0 中测试)。 让我们为TypeScripts实现它来做一个例子:

  • 转到安装 VSCode 的文件夹
  • 打开文件resources\app\extensions\typescript\out\features\suggestSupport.js
  • 添加方法newGuid()SuggestSupport class:

    // copied from: https://github.com/Microsoft/ApplicationInsights-JS/blob/master/JavaScript/JavaScriptSDK/Util.ts
    SuggestSupport.prototype.newGuid = function() {
        var hexValues = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
    
        // c.f. rfc4122 (UUID version 4 = xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx)
        var oct = "", tmp;
        for (var a = 0; a < 4; a++) {
            tmp = (4294967296 * Math.random()) | 0;
            oct += hexValues[tmp & 0xF] + hexValues[tmp >> 4 & 0xF] + hexValues[tmp >> 8 & 0xF] + hexValues[tmp >> 12 & 0xF] + hexValues[tmp >> 16 & 0xF] + hexValues[tmp >> 20 & 0xF] + hexValues[tmp >> 24 & 0xF] + hexValues[tmp >> 28 & 0xF];
        }
    
        // "Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively"
        var clockSequenceHi = hexValues[8 + (Math.random() * 4) | 0];
        return oct.substr(0, 8) + "-" + oct.substr(9, 4) + "-4" + oct.substr(13, 3) + "-" + clockSequenceHi + oct.substr(16, 3) + "-" + oct.substr(19, 12);
    }  
    
  • 找到行 var suggests = []; [在 suggest 函数内]

  • 在该行下方添加此建议:

            suggests.push({
                label: "Create new UUID",
                codeSnippet: "\"" + _this.newGuid() + "\"",
                type: "keyword"
            });
    

重新启动代码后,您将始终在 .ts 文件中收到一个名为 "Create new UUID" 的建议,它会在当前光标位置添加一个类似 "243BC2A6-1AB5-445B-B086-DBDED67368F5" 的值。您可以通过按 CTRL + Space.

强制显示建议框

要将此建议添加到 C# 和 YAML,您需要在相应的 suggestSupport.js 文件中执行相同的操作。

以下扩展非常易于使用,并且完全符合您的要求:https://github.com/heaths/vscode-guid (or get it here 来自 VS Marketplace)

关于 code sniper 和 uuid,请考虑 VSCode 1.53(2021 年 1 月)引入了一个有趣的功能:

New Snippet Variables

There are new snippet variables for inserting uuids and for inserting the relative path the current file.

The sample snippet below would print:

let someId = 'foo/test.js/c13d226f-1932-40e2-9fd9-10198c219e33'
// sample snippet using UUID and RELATIVE_FILEPATH
{
 "scope": "javascript",
 "prefix": "newVars",
 "body": "let someId = '${RELATIVE_FILEPATH}/${UUID}'[=11=]"
}