简体   繁体   English

VS 代码片段到当前行下方的 console.log 选择

[英]VS Code snippet to console.log selection beneath current line

This is what I'd like to achieve ( t is selected in editor):这就是我想要实现的(在编辑器中选择了t ):

Before snippet:片段之前:

var t = 'Foobar';

After snippet:片段后:

var t = 'Foobar';
console.log('t', t);

How can I do that?我怎样才能做到这一点? Here is what I tried to do:这是我试图做的:

"log_selection": {
    "prefix": "cls",
    "body": [
        "console.log('$TM_SELECTED_TEXT', $TM_SELECTED_TEXT);"
    ],
    "description": "Logs selected text"
}

But this just replace selected text with snippet.但这只是用片段替换选定的文本。 I think that I could use TM_CURRENT_LINE here but I have no idea what to do with remaining text in the line.我认为我可以在这里使用 TM_CURRENT_LINE 但我不知道如何处理行中的剩余文本。

Do you have any idea for this?你对此有什么想法吗? Maybe it's impossible with snippet?也许使用片段是不可能的? If so, how can I achieve desired effect?如果是这样,我怎样才能达到预期的效果?

Thank you.谢谢你。

Extension macros (executing multiple commands in 1 keybinding).扩展(在 1 个键绑定中执行多个命令)。

settings.json : settings.json

"macros": {
    "snippetWithDescription": [
        "editor.action.clipboardCopyAction",
        "editor.action.insertLineAfter",
        {
            "command": "editor.action.insertSnippet",
                "when": "editorTextFocus",
                "args": {
                    "snippet": "console.log('$CLIPBOARD', $CLIPBOARD)$0"
                }
        }
    ]
}

keybindings.json : keybindings.json

{
    "key": "ctrl+shift+;",
    "command": "macros.snippetWithDescription"
}

PS you can even omit the selection part if you add another command at the beginning of snippetWithDescription : "editor.action.addSelectionToNextFindMatch", . PS,如果您在snippetWithDescription的开头添加另一个命令,您甚至可以省略选择部分: "editor.action.addSelectionToNextFindMatch", Just place cursor beside the word and hit hotkey.只需将 cursor 放在单词旁边并按热键即可。

I came to this question looking for a solution other than installing a macro extension.我来这个问题是为了寻找除了安装宏扩展之外的解决方案。 Yours can be done with a snippet though as long as the cursor is at the end of your var declaration line.只要 cursor 位于您的 var 声明行的末尾,您就可以使用代码片段来完成。 The snippet would use regex:该片段将使用正则表达式:

"log_selection": {
    "prefix": "cls",
    "body": [
        "",
        "console.log('${TM_CURRENT_LINE/var (.+?) =.*$/$1', $1/});"
    ],
    "description": "Logs selected text"
}

The capturing group (.+?) holds your variable name and is placed in $1 .捕获组(.+?)保存您的变量名称并放置在$1中。 I've tested it (and a good thing, because it took a lot of edits to get a working regex).我已经对其进行了测试(这是一件好事,因为需要进行大量编辑才能获得有效的正则表达式)。 You'd probably want to set up a key binding in your settings to trigger the snippet (but it also works typing the snippet prefix):您可能希望在设置中设置键绑定以触发代码段(但它也可以输入代码段前缀):

    "key": "alt+c alt+l",   // some key combo
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus && !editorHasSelection",
    "args": {
        "langId": "js",   // ?? optional?
        "name": "log_selection"  // your snippet name
    }

Unfortunately, in my case I'm trying to alter the current line so it seems I may need a macro to select the line so that it is replaced.不幸的是,在我的情况下,我正在尝试更改当前行,因此看来我可能需要一个宏来 select 行以便将其替换。

this worked for me:这对我有用:

"macros": {
        "logCurrentVariable": [
          "editor.action.addSelectionToNextFindMatch",
          "problems.action.copy",
          "editor.action.clipboardCopyAction",
          {
            "command": "editor.action.insertSnippet",
                "when": "editorTextFocus",
                "args": {
                    "snippet": "console.log('$CLIPBOARD', $CLIPBOARD)$0"
                }
        }
    ]
},

from https://medium.com/@copperfox777/how-to-console-log-variable-under-the-cursor-in-visual-studio-code-ba25feadb00a来自https://medium.com/@copperfox777/how-to-console-log-variable-under-the-cursor-in-visual-studio-code-ba25feadb00a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM