【问题标题】:Visual Studio Code snippet that inserts the method/function name?插入方法/函数名称的 Visual Studio 代码片段?
【发布时间】:2018-02-20 21:16:45
【问题描述】:

我不是在寻找创建方法或函数的 sn-p,我想要一个自动插入方法/函数名称的 sn-p。

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

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

很遗憾,TM_METHODNAME 不是default set of variables 的一部分。

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

也许妥协是使用TM_SELECTED_TEXT,虽然我不明白它是如何工作的,因为当输入sn-p前缀时,所选文本显然被删除了。

【问题讨论】:

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


    【解决方案1】:

    从 v1.20 开始,您可以使用 CLIPBOARD 作为变量,请参阅 v1.20 release notes,因此您可以先复制方法名称并准备好在 sn-p 中使用。我认为目前这是你唯一的选择。

    【讨论】:

    • 那就这样吧!!
    【解决方案2】:

    很遗憾,我们仍然没有可用的$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

    【讨论】:

      【解决方案3】:

      我会向你推荐以下 Visual Studio 扩展。

      Turbo 控制台日志

      有空here

      一些功能:

      选择作为调试对象的变量

      按下

      CTRL + ALT + L

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-07
        • 1970-01-01
        • 2016-11-17
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多