Visual Studio 插入 method/function 名称的代码片段?

Visual Studio Code snippet that inserts the method/function name?

我不是在寻找创建方法或函数的片段,我想要一个自动插入 method/function 名称的片段。

准确地说,我想要这样的东西:

console.log('${TM_METHODNAME}: ')

很遗憾,TM_METHODNAME 不属于 default set of variables

似乎也没有可用的扩展程序。

也许折衷方案是使用 TM_SELECTED_TEXT,虽然我不明白它是如何工作的,因为在输入代码段前缀时,所选文本显然被删除了。

从 v1.20 开始,您可以将 CLIPBOARD 用作变量,请参阅 v1.20 release notes,这样您可以先复制方法名称并准备好在代码段中使用。我认为此时这是你唯一的选择。

我会向您推荐以下 Visual Studio 扩展程序。

Turbo 控制台日志

可用here

部分功能:

选择作为调试对象的变量

按下

CTRL + ALT + L

日志消息将插入到与所选内容相关的下一行 像这样的变量:

console.log('SelectedVariableEnclosingClassName -> SelectVariableEnclosingFunctionName -> SelectedVariable', SelectedVariable)

遗憾的是我们仍然没有 $TM_METHOD_NAME 可用,无论如何这里有一些片段我使用 $WORKSPACE_NAME$TM_FILENAME 来区分输出和着色:

   {
        "key": "cmd+alt+l",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus",
        "args": {
            "snippet": "console.log(`%c[${WORKSPACE_NAME/(.*)/${1:/upcase}/}] ${CLIPBOARD}`, '${2|background: lightblue; color: #444;,background: lightgreen; color: #444;,background: lightsalmon; color: #444;,background: lightcyan; color: #444;,background: lightpink; color: #444;,background: lightseagreen; color: #444;,background: lightskyblue; color: #444;,background: lightsteelblue; color: #444;,background: khaki; color: #444;,background: purple; color: white;,background: salmon; color: white;,background: blue; color: white;,background: #444; color: white;,background: green; color: white;,background: mediumvioletred; color: white;,background: blueviolet; color: white;,background: chocolate; color: white;,background: mediumvioletred; color: white;,background: brown; color: white;,background: cadetblue; color: white;,background: cornflowerblue; color: white;,background: crimson; color: white;|} padding: 3px; border-radius: 5px;');"
        }
    },
    {
        "key": "cmd+alt+e",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus",
        "args": {
            "snippet": "console.groupCollapsed('$TM_FILENAME');\nError.stackTraceLimit = ${0:4};\nconsole.log(new Error('$TM_FILENAME').stack);\nconsole.groupEnd();\n"
        }
    },
    {
        "key": "cmd+alt+shift+l",
        "command": "editor.action.insertSnippet",
        "when": "editorTextFocus",
        "args": {
            "snippet": "console.groupCollapsed(`%c[${WORKSPACE_NAME/(.*)/${1:/upcase}/}] ${CLIPBOARD}`, '${2|background: lightblue; color: #444;,background: lightgreen; color: #444;,background: lightsalmon; color: #444;,background: lightcyan; color: #444;,background: lightpink; color: #444;,background: lightseagreen; color: #444;,background: lightskyblue; color: #444;,background: lightsteelblue; color: #444;,background: khaki; color: #444;,background: purple; color: white;,background: salmon; color: white;,background: blue; color: white;,background: #444; color: white;,background: green; color: white;,background: mediumvioletred; color: white;,background: blueviolet; color: white;,background: chocolate; color: white;,background: mediumvioletred; color: white;,background: brown; color: white;,background: cadetblue; color: white;,background: cornflowerblue; color: white;,background: crimson; color: white;|} padding: 3px; border-radius: 5px;');\nconsole.log(${CLIPBOARD});\nconsole.groupEnd();\n"
        }
    },

将此拖放到 keybindings.json