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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Then so be it!!
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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.