[英]How to initiate "findInFiles" with VSCode extension API?
我正在嘗試編寫一個擴展程序,該擴展程序將自動選擇光標下的單詞,打開在文件中查找對話框,並使用該選擇啟動搜索。 到目前為止,除了實際啟動搜索之外,我已經能夠讓擴展程序執行所有操作。 我仍然必須在文件中查找對話框中按 Enter 才能實際進行搜索。 這是我到目前為止的擴展代碼:
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('extension.searchUnderCursor', () => {
// Get the current editor
let editor = vscode.window.activeTextEditor;
if (!editor) {
console.log('No active editor!');
return;
}
// Get word under cursor position
let wordRange = editor.document.getWordRangeAtPosition(editor.selection.start);
if (!wordRange) {
console.log('No word under the cursor!');
return;
}
// Select the word
editor.selection = new vscode.Selection(wordRange.start, wordRange.end);
// Initiate search
vscode.commands.executeCommand('workbench.action.findInFiles').then(() => {
vscode.commands.executeCommand('default:type', {text: '\n'});
});
});
context.subscriptions.push(disposable);
}
export function deactivate() {}
您可以看到我試圖找到一種方法,在“在文件中查找”對話框中按 Enter 鍵開始搜索。 當然,那是行不通的。 我怎樣才能在這里獲得我想要的功能?
其實,我想通了。 這是我的解決方案:
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('extension.searchUnderCursor', () => {
// Get the current editor
let editor = vscode.window.activeTextEditor;
if (!editor) {
console.log('No active editor!');
return;
}
// Get word under cursor position
let wordRange = editor.document.getWordRangeAtPosition(editor.selection.start);
if (!wordRange) {
console.log('No word under the cursor!');
return;
}
// Get word text
let wordText = editor.document.getText(wordRange);
// Initiate search
vscode.commands.executeCommand('workbench.action.findInFiles', {
query: wordText,
triggerSearch: true,
matchWholeWord: true,
isCaseSensitive: true,
});
});
context.subscriptions.push(disposable);
}
export function deactivate() {}
事實證明, findInFiles
操作有許多它接受的有用參數: https : //github.com/microsoft/vscode/blob/9a987a1cd0d3413ffda4ed41268d9f9ee8b7565f/src/vs/workbench/contrib/search/browser/search#Actions。 L163-L172
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.