如何启动“findInFiles"使用 VSCode 扩展 API? [英] How to initiate "findInFiles" with VSCode extension API?

查看:42
本文介绍了如何启动“findInFiles"使用 VSCode 扩展 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个扩展程序,该扩展程序将自动选择光标下的单词,打开在文件中查找对话框,并使用该选择启动搜索.到目前为止,除了实际启动搜索之外,我已经能够让扩展程序执行所有操作.我仍然必须在文件中查找对话框中按 Enter 才能实际进行搜索.这是我到目前为止的扩展代码:

I'm trying to write an extension that will automatically select the word under the cursor, open the find in files dialog, and initiate a search with that selection. So far, I've been able to get the extension to do everything except actually initiating the search. I still have to press enter in the find in files dialog to actually do the search. Here's the extension code I have so far:

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() {}

你可以看到我试图找到一种在文件中查找对话框中按回车键开始搜索的方法.当然,那是行不通的.我如何才能获得我要在这里使用的功能?

You can see I was trying to find a way to press enter in the find in files dialog to begin the search. Of course, that doesn't work. How can I get the functionality I'm going for here?

推荐答案

其实我想通了.这是我的解决方案:

Actually, I figured it out. Here's my solution:

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/benchsearch/conts//browser/searchActions.ts#L163-L172

As it turns out, the findInFiles action has a number of useful args that it accepts: https://github.com/microsoft/vscode/blob/9a987a1cd0d3413ffda4ed41268d9f9ee8b7565f/src/vs/workbench/contrib/search/browser/searchActions.ts#L163-L172

这篇关于如何启动“findInFiles"使用 VSCode 扩展 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆