VS Code 为带有换行符的常用方法创建自定义快捷方式(例如 dd($var) 或 console.log($var))

VS Code creating custom shortcuts for common methods with line break (e.g. dd($var) or console.log($var))

首先,我在 VSCode 上有一个快捷方式,可以用我输入的内容包裹文本。

<div>
    Hello World
</div>

如果我 select “世界”并使用 Emmet: Wrap with Abbreviation 快捷方式并输入 span 我可以做到:

<div>
    Hello <span>World</span>
</div>

但事情是这样的: 我知道我们可以创建在我们 select 编辑的单词的每一侧都不相同的自定义包装(来源:

{
  "key": "ctrl+i",
  "command": "editor.action.insertSnippet",
  "args": {
    "snippet": "{something}$TM_SELECTED_TEXT{/some other thing}"
  },
  "when": "editorTextFocus && editorHasSelection"
}

我想要的是 select 我的变量,然后使用快捷方式,它将在 selected 行下打印我需要的内容,并且不会中断我所在的当前行来自.

对于这个例子,我select输入变量 $user_id,按快捷键,然后它会添加第二行。

$user_id = User::where('user_name', $user_name)->get()->first()->id;
dd($user_id);

这是一个开始:

{
  "key": "ctrl+alt+c",
  "command": "editor.action.insertSnippet",
  "args": {
    "snippet": "   ==> here we need to find how to line break without spliting the code and then: <==
                             {console.log(}$TM_SELECTED_TEXT{)}"
  },
  "when": "editorTextFocus && editorHasSelection"
}

你觉得可能吗?也许解决方案是将键盘宏与 VSCode 分开使用?

如果我对你的问题的理解正确,你可以只使用 [=10=] 来显示光标的结束位置,然后使用 \n 来插入换行符。

但是,我不完全确定这在从键盘快捷键文件创建片段时是否有效,但它在片段文件中有效,所以我假设它在这里有效。

我想到了这个解决方案,感谢 @Mark 在评论中与此主题相关:

  1. 安装 Multi-command VSCode 扩展程序

  2. 打开扩展的设置文件(settings.json)

  3. 实现你的代码(这是我的 console.log() 和 dd() )

     "multiCommand.commands": [
    
         {
             "command": "multiCommand.console.log",
    
             "sequence": [
                 "editor.action.clipboardCopyAction",
                 "editor.action.insertLineAfter",
                 {
                 "command": "editor.action.insertSnippet",
                 "args": {
                     "snippet": "console.log(\"$CLIPBOARD: \", $$CLIPBOARD)\n[=10=]"
                 }
                 },
             ]
         },
    
    
         {
             "command": "multiCommand.dd",
    
             "sequence": [
               "editor.action.clipboardCopyAction",
               "editor.action.insertLineAfter",
               {
                 "command": "editor.action.insertSnippet",
                 "args": {
                   "snippet": "dd($$CLIPBOARD);"
                 }
               },
             ]
         }
    
  4. 在您的 VSCode 设置中实施快捷方式 (keybindings.json)

    {
        "key": "ctrl+1",
        "command": "extension.multiCommand.execute",
        "args": { "command": "multiCommand.console.log" }
    },
    {
        "key": "ctrl+2",
        "command": "extension.multiCommand.execute",
        "args": { "command": "multiCommand.dd" }
    }