我发布了一个扩展,它允许您创建和保存预定义的查找/替换或搜索/替换:查找和转换并通过命令面板中的命令或通过键绑定调用它们。示例设置:
"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);
显然,您必须添加一些错误检查。