vscode - 有没有api可以得到搜索结果?

vscode - Is there any api to get search results?

当我尝试在文件中搜索文本时,我想知道我是否可以获得 vscode.Is 搜索的结果,那里有一些我可以使用的 api?

我在vscode中发现了一些像TextSearchProvider这样的功能api,但是这个api是用来整体搜索文本的workplace.I只想要一个搜索结果文件。

example picture

例如,当我尝试搜索 Selection 时,我想要这个搜索的结果。

我的扩展中的一些代码 Find and Transform:

function _findAndSelect(editor, findValue, restrictFind) {

    let foundSelections = [];

        // get all the matches in the document
        let fullText = editor.document.getText();
        let matches = [...fullText.matchAll(new RegExp(findValue, "gm"))];

        matches.forEach((match, index) => {
            let startPos = editor.document.positionAt(match.index);
            let endPos = editor.document.positionAt(match.index + match[0].length);
            foundSelections[index] = new vscode.Selection(startPos, endPos);
        });
        editor.selections = foundSelections; // this will remove all the original selections

}

只需获取当前文档的文本,使用您的搜索词进行一些字符串搜索,例如 matchAll。在上面的代码中,我想 select 文档中的所有匹配项——您可能对此感兴趣,也可能不感兴趣。

您似乎希望搜索词成为当前 selection:

// editor is the vscode.window.activeTextEditor

let selection = editor.selection;  
let selectedRange = new vscode.Range(selection.start, selection.end);
let selectedText = editor.document.getText(selectedRange);