【问题标题】:I'm not entirely sure how to use QuickInput in VS-Code我不完全确定如何在 VS-Code 中使用 QuickInput
【发布时间】:2021-09-06 07:29:31
【问题描述】:

我最近问了一个问题"how do i add custom parameters to a custom command in a custom vs code extension",通过一些cmets意识到不能在命令面板中输入参数。所以我寻找替代选择。我遇到的一个是 quickInput,它允许用户在窗口中输入文本。然而,作为打字稿的新手,以及拥有更多知识渊博的用户的documentation,我不知道如何使用它。请帮忙!

更新:由于找不到“quickInput”,我决定选择“showInputBox”,但不确定如何使用“title”参数。但只是为了测试 porpuses,我决定测试以下内容:

var info  = vscode.window.showInputBox();
vscode.window.showInformationMessage(info);

但它出现了错误:

No overload matches this call.
Overload 1 of 4, '(message: string, ...items: string[]); Thenable<string | undefined>', gave the following error.
Argument of type 'Thenable<string | undefined>' is not assignable to the parameter of type 'string'.
Overload 2 of 4, '(message: string, ...items: MessageItem[]); Thenable<MessageItem | undefined>', gave the following error.
Argument of type 'Thenable<string | undefined>' is not assignable to the parameter of type 'string'. ts(2769) [29, 40]

我注意到它只给出了重载 1 和 2,而不是 3 和 4 无论如何,现在我需要知道如何添加:1)输入框的标题,以及2)如何解决该错误!!!

编辑 2: 我试过这个来解决变量类型问题:

var info = vscode.window.showInputBox();
if(typeof info === "string") {
    vscode.window.showInformationMessage(info);
}

它没有出现任何错误,但是......它接受了我的输入并没有发送它。

【问题讨论】:

  • 查看扩展示例
  • 我没有看到任何例子,但我会再看一遍。
  • 上面还写着cannot find name 'QuickInput'

标签: typescript visual-studio-code vscode-extensions


【解决方案1】:

我之前使用过的一些示例代码(修改):


let inputBoxOptions = {
  ignoreFocusOut: true,
  title: "your title here"
};

// you can use the above or below ways to specify your inputBoxOptions
// 'command' is a variable I have declared elsewhere I want to use now 

inputBoxOptions.prompt = `Enter an alias for the command: ${ command } . . . . . `;
inputBoxOptions.placeHolder = `Enter an alias(es) for the ${ command } here`;

await vscode.window.showInputBox(inputBoxOptions)
  .then(arg => {
      // run your code using the inputBox 'arg' here
   });
}

你需要await用户的输入。然后在then 函数中使用它或如下:


或文档示例:

export async function showInputBox() {
    const result = await window.showInputBox({
        value: 'abcdef',
        valueSelection: [2, 4],
        placeHolder: 'For example: fedcba. But not: 123',
        validateInput: text => {
            window.showInformationMessage(`Validating: ${text}`);
            return text === '123' ? 'Not 123!' : null;
        }
    });
    window.showInformationMessage(`Got: ${result}`);
}

basic input documentation

【讨论】:

  • 我使用了下面的例子,它发送的只是received ${result}
  • 非虚拟机。我不小心用了 '' 而不是 ``
猜你喜欢
  • 2019-02-26
  • 1970-01-01
  • 2016-08-13
  • 1970-01-01
  • 1970-01-01
  • 2023-01-27
  • 2023-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多