插入方法/函数名称的Visual Studio代码段?

时间:2018-02-20 21:16:45

标签: visual-studio-code code-snippets vscode-extensions

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

准确地说,我想要这样的事情:

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

不幸的是,TM_METHODNAME不属于default set of variables

似乎也没有可用的扩展名。

也许妥协是使用TM_SELECTED_TEXT,虽然我不明白它是如何工作的,因为在输入代码段前缀时,选中的文字显然会被删除。

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

可悲的是我们仍然没有$TM_METHOD_NAME可用,无论如何,这里我与$WORKSPACE_NAME$TM_FILENAME一起使用的一些代码片段用于区分输出和着色:

enter image description here

   {
        "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

答案 2 :(得分:0)

我将向您推荐以下Visual Studio扩展。

涡轮控制台日志

here可用

一些功能:

选择作为调试主题的变量

  

CTRL + ALT + L

相对于所选内容,日志消息将插入下一行 像这样的变量:

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