【问题标题】:Is There anyway to execute multiple commands in vscode keybindings.json?无论如何要在 vscode keybindings.json 中执行多个命令吗?
【发布时间】:2020-06-10 17:20:58
【问题描述】:

我需要在 vscode 中的 keybindings.json 中执行多个命令才能记录变量名、文件名和变量值。

飞镖中的前任

double x = 2.2;
print("main.dart -> x $x");

默认行为是在同一行中插入片段,但我需要在我的变量下方添加新行,然后插入我的自定义 sn-p。

此外,我尝试了宏和多命令。令人难忘的是,我无法访问 TM_FILENAME_BASE 和 CLIPBOARD 等 VSCode 环境变量。它们只是打印为平面文本,例如

double x = 2.2;
print("$TM_FILENAME_BASE x $x") 

无法将环境变量解析为它的值。

这是来自 mutli-command 扩展 settings.json

"multiCommand.commands": [
        {
            "command": "myCommand",
            "sequence": [
                "cursorDown",
                "insertSnippet",
                {
                    "command": "type",
                    "args": {
                        "text": "print(\" log-> ${TM_FILENAME_BASE} -> 
                       ${TM_SELECTED_TEXT} -> ${${TM_SELECTED_TEXT}} \" );"
                    }
                }
            ]
        },
    ],

这个来自 keybindings.json

{
     "key": "alt+p",
     "command": "editor.action.insertSnippet",
     "when": "editorTextFocus && resourceExtname == .dart",
     "args": {
         "snippet": "print(\" log-> ${TM_FILENAME_BASE} -> ${TM_SELECTED_TEXT} -> ${${TM_SELECTED_TEXT}} \" );"
     }
}

解决方案 这是受 Mark 启发的针对我的问题的调整解决方案

来自 settings.json

 "multiCommand.commands": [
        {
            "command": "multiCommand.printVariable",
            "sequence": [
                "editor.action.clipboardCopyAction", // Copy the selected to the clipboard 
                "editor.action.insertLineAfter", 
                {
                    "command": "editor.action.insertSnippet",
                    "args": {
                        "snippet": "print(\"${TM_FILENAME_BASE} -> ${CLIPBOARD} -> ${${CLIPBOARD}}\");" // then add it here 
                    }
                },
            ]
        },
    ],

来自 keybindings.json

 {
        "key": "alt+p",
        "command": "extension.multiCommand.execute",
        "args": {
            "command": "multiCommand.printVariable"
        },
        "when": "editorTextFocus && resourceExtname == .dart"
 }

【问题讨论】:

  • 请出示您的 sn-p 和/或宏。因此,我们可以帮助修复您编写的内容,而不是自己编写。

标签: visual-studio-code


【解决方案1】:

如果你选择你的变量,这会给出你想要的输出:

"multiCommand.commands": [

  {
    "command": "multiCommand.printVariable",

    "sequence": [
      "editor.action.clipboardCopyAction",
      "editor.action.insertLineAfter",
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "snippet": "print(\"${TM_FILENAME_BASE} -> "
        }
      },
      "editor.action.clipboardPasteAction",
      {
        "command": "type",
        "args": {
          "text": " $"
        }
      },
      "editor.action.clipboardPasteAction",
      {
        "command": "type",
        "args": {
          "text": "\");"
        }
      },
    ]
  },

你使用了${TM_FILENAME_BASE},所以我也这样做了——它会去掉扩展名——但是你显示main.dart作为你想要的输出的一部分,所以也许你想要TM_FILENAME

和你的键绑定:

{
  "key": "alt+p",    // or whichever keybinding you choose
  "command": "extension.multiCommand.execute",
  "args": { "command": "multiCommand.printVariable" },
  "when": "editorTextFocus && resourceExtname == .dart" 
},

和我的回答类似How to create a shortcut to print a variable (vscode)

如果您的输入是一般形式

double x = 2.2;  // where the desired variable is right before an `=`

你可以简化很多,只在行尾使用光标使用以下键绑定(并且没有宏) - 没有选择:

{
  "key": "alt+p",
  "command": "editor.action.insertSnippet",
  "args": {
              // works with cursor end of line, no selection
    "snippet": "\nprint(\"${TM_FILENAME_BASE} -> ${TM_CURRENT_LINE/\\s*\\w*\\b\\s*(.*?)\\s*=.*/$1 $$1\");/}",
  },
   "when": "editorTextFocus && resourceExtname == .dart" 
},

两种方法的演示。首先是选择和宏。二、无选择,光标在行尾,简单sn-p。

注意我在我的keybindings.json 文件中,所以${TM_FILENAME_BASE} = keybindings

【讨论】:

  • 太好了,非常有帮助,非常感谢。我只是稍微调整了 multiCommand,我会在描述中添加它。
  • 当然,我暂时忘记了我很清楚的 ${CLIPBOARD} 变量 - 误会了,很好。
猜你喜欢
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-18
  • 2023-01-13
  • 1970-01-01
  • 2011-01-12
  • 1970-01-01
相关资源
最近更新 更多