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

3

9

I am not looking for a snippet that creates a method or function, I want a snippet that inserts the name of the method/function automatically.

Precisely, I would like something like this:

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

Unfortunately, TM_METHODNAME is not part of the default set of variables.

There doesn't seem to be an extension available for that either.

Maybe a compromise would be to use TM_SELECTED_TEXT, although I don't understand how it works, as when typing the snippet prefix, the selected text is obviously erased.

Nicolella answered 20/2, 2018 at 21:16 Comment(0)
A
7

As of v1.20 you can use CLIPBOARD as a variable, see v1.20 release notes so you could copy the method name first and have it ready to use in the snippet. I think at this point that is your only option.

Ankus answered 20/2, 2018 at 21:38 Comment(1)
Then so be it!!Warms
R
1

I will recommend you the following Visual Studio extension.

Turbo Console Log

Is available here

Some features:

Selecting the variable which is the subject of the debugging

Pressing

CTRL + ALT + L

The log message will be inserted in the next line relative to the selected variable like this:

console.log('SelectedVariableEnclosingClassName -> SelectVariableEnclosingFunctionName -> SelectedVariable', SelectedVariable)
Ruelle answered 23/7, 2018 at 15:18 Comment(0)
L
1

that's sad we still don't have $TM_METHOD_NAME available, anyway here few snippets I use with $WORKSPACE_NAME and $TM_FILENAME to differentiate the output along with coloring:

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"
        }
    },

drop this to keybindings.json

Lientery answered 14/4, 2020 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.