VS Code片段到当前行下面的console.log选择 [英] VS Code snippet to console.log selection beneath current line

查看:345
本文介绍了VS Code片段到当前行下面的console.log选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要实现的目标(在编辑器中选择 t ):

This is what I'd like to achieve (t is selected in editor):

在代码段之前:

var t = 'Foobar';

片段之后:

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

但这只是用片段替换所选文本。我想我可以在这里使用TM_CURRENT_LINE,但我不知道如何处理该行中的剩余文本。

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.

你对此有什么想法吗?也许用片段不可能?如果是这样,我怎样才能达到预期效果?

Do you have any idea for this? Maybe it's impossible with snippet? If so, how can I achieve desired effect?

谢谢。

推荐答案

扩展(在1个键绑定中执行多个命令)。

Extension macros (executing multiple commands in 1 keybinding).

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

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

P.S。如果你在 snippetWithDescription 的开头添加另一个命令,你甚至可以省略选择部分:editor.action.addSelectionToNextFindMatch,。只需将光标放在单词旁边,然后点击热键即可。

P.S. you can even omit the selection part if you add another command at the beginning of snippetWithDescription: "editor.action.addSelectionToNextFindMatch",. Just place cursor beside the word and hit hotkey.

这篇关于VS Code片段到当前行下面的console.log选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆