VS Code Git 扩展 API [英] VS Code Git Extension API

查看:47
本文介绍了VS Code Git 扩展 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么地方可以获得更多关于如何创建使用 Git 扩展 API 的扩展的文档?

Is there somewhere one can obtain more documentation on how to create an extension that uses the Git Extension API?

https://github.com/microsoft/vscode/blob/master/extensions/git/README.md 微软提供的唯一文档是:

At https://github.com/microsoft/vscode/blob/master/extensions/git/README.md the only documentation Microsoft provides is this:

注意:此扩展与 Visual Studio Code 捆绑在一起.可以禁用但不能卸载.

Notice: This extension is bundled with Visual Studio Code. It can be disabled but not uninstalled.

请参阅 VS Code 中的 Git 支持以了解功能这个扩展.

See Git support in VS Code to learn about the features of this extension.

Git 扩展公开了一个 API,可由任何其他扩展访问.

The Git extension exposes an API, reachable by any other extension.

  1. 复制src/api/git.d.ts到你的扩展源;
  2. 在扩展程序的编译中包含 git.d.ts.
  3. 使用以下代码段获取 API:

  1. Copy src/api/git.d.ts to your extension's sources;
  2. Include git.d.ts in your extension's compilation.
  3. Get a hold of the API with the following snippet:

const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git').exports;
const git = gitExtension.getAPI(1);

它真的没有帮助,当我尝试使用那两行时,扩展会运行,但如果我尝试检查,例如,git.repositories[0],它返回 undefined.Idk 如果我做错了什么?:(

It really doesn't help and when I try to use those 2 lines the extension runs but if I try to check, for example, git.repositories[0] it returns undefined. Idk if I'm doing something wrong? :(

推荐答案

你可以看看eamodio/vscode-gitlens,基于Git扩展的主要扩展.

You can have a look at eamodio/vscode-gitlens, the main extension based on Git extension.

它的 /git/gitService.ts 确实调用了 GIt 扩展:

Its src/git/gitService.ts does call the GIt extension:

static async getBuiltInGitApi(): Promise<BuiltInGitApi | undefined> {
    try {
        const extension = extensions.getExtension('vscode.git') as Extension<GitExtension>;
        if (extension !== undefined) {
            const gitExtension = extension.isActive ? extension.exports : await extension.activate();

            return gitExtension.getAPI(1);
        }
    } catch {}

    return undefined;
}

这篇关于VS Code Git 扩展 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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