简体   繁体   English

如何在 VSCode 扩展的命令面板中输入文本

[英]How to enter text in command palette in VSCode extension

I am writing a vscode extension.我正在编写一个 vscode 扩展。 I use the following code to enter text in the TextEditor area.我使用以下代码在 TextEditor 区域中输入文本。

function insertText(params: string) {
  var editor = vscode.window.activeTextEditor;
  editor.edit(edit =>
    editor.selections.forEach(selection => {
      edit.delete(selection);
      edit.insert(selection.start, params);
    })
  );
}

BUT, what I need my extension to be able to enter text in areas like:但是,我需要我的扩展程序才能在以下区域输入文本:

  • command palette命令面板
  • input area when I press Ctrl + G (for workbench.action.gotoLine command)Ctrl + G时的输入区域(用于workbench.action.gotoLine命令)

instead of asking for user input.而不是要求用户输入。


tl;dr tl;博士

pseudocode for what I am asking:我要问的伪代码:

openCommandPallete();
enterTextInCommandPallete("ABCDEF");

You can call the quickOpen command with an argument to pre-fill the text:您可以使用参数调用quickOpen命令来预填充文本:

vscode.commands.executeCommand("workbench.action.quickOpen", "Hello World");

You can switch to the command palette by prefixing the text with > .您可以通过在文本前加上>来切换到命令面板。 The full list of possible prefixes for quick open can be checked with ?可以使用?检查快速打开的可能前缀的完整列表? :

As you can see here, : is the prefix for "Go to Line", so that works with the same command:正如您在此处看到的, :是“转到行”的前缀,因此可以使用相同的命令:

vscode.commands.executeCommand("workbench.action.quickOpen", ":5");

There's a related question that deals with how to utilize arguments for quick open in keybindings here .有一个相关的问题涉及如何在此处的键绑定中使用参数进行快速打开。

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

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