0

我想创建一个执行非常简单任务的扩展:

我从命令托盘调用命令:

> commandName: Query

它应该以正则表达式模式启动全局搜索:

SomeToken AnotherToken (some|regex*|prefix?|foo) Query

关键是我不想一直输入SomeToken AnotherToken (some|regex*|prefix?|foo)前缀。

这听起来像是一个非常简单的过程,我希望它在 vscode 中是可能的,但是我在 VSCode API 和相关教程中还没有找到任何关于它的信息。

4

1 回答 1

1

我发布了一个扩展,它允许您创建和保存预定义的查找/替换或搜索/替换:查找和转换并通过命令面板中的命令或通过键绑定调用它们。示例设置:

    "findInCurrentFile": {
        "upcaseSwap2": {
            "title": "swap iif <==> hello",
            "find": "(iif) (hello)",
            "replace": "_\\u$2_ _\\U$1_",  // double-escaped case modifiers
            "restrictFind": "selections"
        },
        "upcaseSelectedKeywords": {
            "title": "Uppercase selected Keywords",
            "find": "(epsilon|alpha|beta)",
            "replace": "\\U$1",
            // "restrictFind": "selections"
        }
    },

    "runInSearchPanel": {
        "removeDigits": {
            "title": "Remove digits from Arturo",
            "find": "^(\\s*Arturo)\\d+",
            "replace": "$1",
            "isRegex": true,
            "triggerSearch": true,
            // "filesToInclude": "${file}"
            "filesToInclude": "${file}",
        }
    },

  • 我认为没有任何方法可以从命令面板调用带有参数的命令,因为您似乎表明您希望这样做。您可以打开一个 InputBox 并询问该值(这很容易做到)或使用当前选择的文本。下面我展示了两种方式。

首先使用当前选定的文本,您可以尝试一个简单的键绑定,看看是否足够:

{
  "key": "ctrl+shift+g",                    // whatever keybinding you wish
  "command": "search.action.openNewEditor",
  "args": {

    "query": "(enum|struct|fn|trait|impl(<.*>)?|type) ${selectedText}",

    "isRegexp": true,

    // "includes": "${relativeFile}",  // works

    "showIncludesExcludes": true,
    "triggerSearch": true,
    "contextLines": 2,
    "focusResults": true,
  },
  "when": "editorTextFocus"
}

它会打开一个搜索编辑器,而不是使用您的搜索视图/面板。如果您希望它在搜索视图/面板中并不那么容易。不幸的是workbench.action.findInFilesargs 不支持${selectedText}. 不过,您可以执行此键绑定,然后只需在最后键入查询的其余部分。

  {
    "key": "ctrl+shift+f",
    "command": "workbench.action.findInFiles",
    "args": {

      "query": "(enum|struct|fn|trait|impl(<.*>)?|type) ",
      "isRegex": true,
      // "replace": "$1",
      "triggerSearch": true,                          // seems to be the default
      // "filesToInclude": "${relativeFileDirname}",  // no variables in findInFiles
      "preserveCase": true,
      "useExcludeSettingsAndIgnoreFiles": false,
      "isCaseSensitive": true,
      // "matchWholeWord": true,
      // "filesToExclude": ""
    }
  },

以下是使用您vscode.commands.registerCommand通话中所选文本的扩展代码的内容:


let disposable = vscode.commands.registerCommand('myExtensionName.myCommandName', function () {

  // get selected text or open an InputBox here

  const selection = vscode.window.activeTextEditor.selection;
  const selectedText = vscode.window.activeTextEditor.document.getText(selection);

  const searchOptions = {
    query: "(enum|struct|fn|trait|impl(<.*>)?|type) " + selectedText,
    triggerSearch: true,
    isRegex: true
  };

  vscode.commands.executeCommand('workbench.action.findInFiles', searchOptions);
});

context.subscriptions.push(disposable);

这将打开一个 InputBox 以从用户那里获取查询词:


let disposable = vscode.commands.registerCommand('myExtensionName.myCommandName', function () {

  vscode.window.showInputBox({ prompt: "Enter a search query" }).then(query => {

    const searchOptions = {
      query: "(enum|struct|fn|trait|impl(<.*>)?|type) " + query,
      triggerSearch: true,
      isRegex: true
    };

    vscode.commands.executeCommand('workbench.action.findInFiles', searchOptions);
  })
});

context.subscriptions.push(disposable);

显然,您必须添加一些错误检查。

于 2021-02-09T03:13:23.923 回答