Skip to content

Commit 6d99c1a

Browse files
Lazy load git dependency
1 parent d42fd5f commit 6d99c1a

File tree

4 files changed

+27
-29
lines changed

4 files changed

+27
-29
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "andreas-talon",
33
"displayName": "Andreas Talon",
44
"description": "VSCode extension used by Talon Voice",
5-
"version": "3.62.0",
5+
"version": "3.63.0",
66
"publisher": "AndreasArvidsson",
77
"license": "MIT",
88
"main": "./out/extension.js",

src/commands/GitUtil.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { Range, TextDocument } from "vscode";
2-
import { API, GitExtension, Remote, Repository } from "../typings/git";
2+
import { API, Remote, Repository } from "../typings/git";
33
import { getActiveFileSchemaEditor } from "../util/getActiveEditor";
4+
import { getGitExtension } from "../util/getExtension";
45

56
export type GitParameters = {
67
useSelection: boolean;
78
useBranch: boolean;
89
};
910

10-
export class GitUtil {
11-
private gitApi: API;
11+
let _gitApi: API;
1212

13-
constructor(gitExtension: GitExtension) {
14-
this.gitApi = gitExtension.getAPI(1);
13+
async function gitApi(): Promise<API> {
14+
if (_gitApi == null) {
15+
const gitExtension = await getGitExtension();
16+
_gitApi = gitExtension.getAPI(1);
1517
}
18+
return _gitApi;
19+
}
1620

17-
getFileURL({ useSelection = false, useBranch = false }: GitParameters): string {
21+
export class GitUtil {
22+
async getFileURL({ useSelection = false, useBranch = false }: GitParameters): Promise<string> {
1823
const { document, selections } = getActiveFileSchemaEditor();
19-
const repository = this.getRepository();
24+
const repository = await this.getRepository();
2025
const platform = getPlatform(repository);
2126
const relativeFilePath = getRelativeFilepath(repository, document.uri.path);
2227
const range = useSelection ? selections[0] : undefined;
@@ -35,28 +40,28 @@ export class GitUtil {
3540
return platform.getFileUrl(commitOrBranch, relativeFilePath, range);
3641
}
3742

38-
getRepoURL(): string {
39-
const repository = this.getRepository();
43+
async getRepoURL(): Promise<string> {
44+
const repository = await this.getRepository();
4045
return getPlatform(repository).getRepoUrl();
4146
}
4247

43-
getIssuesURL(): string {
44-
const repository = this.getRepository();
48+
async getIssuesURL(): Promise<string> {
49+
const repository = await this.getRepository();
4550
return getPlatform(repository).getIssuesUrl();
4651
}
4752

48-
getNewIssueURL(): string {
49-
const repository = this.getRepository();
53+
async getNewIssueURL(): Promise<string> {
54+
const repository = await this.getRepository();
5055
return getPlatform(repository).getNewIssueUrl();
5156
}
5257

53-
getPullRequestsURL(): string {
54-
const repository = this.getRepository();
58+
async getPullRequestsURL(): Promise<string> {
59+
const repository = await this.getRepository();
5560
return getPlatform(repository).getPullRequestsURL();
5661
}
5762

5863
async checkout(branches: string[]) {
59-
const repository = this.getRepository();
64+
const repository = await this.getRepository();
6065
for (const branch of branches) {
6166
try {
6267
await repository.checkout(branch);
@@ -68,8 +73,8 @@ export class GitUtil {
6873
throw Error(`Can't checkout branch '${branches.join(", ")}'`);
6974
}
7075

71-
private getRepository(): Repository {
72-
const { repositories } = this.gitApi;
76+
private async getRepository(): Promise<Repository> {
77+
const { repositories } = await gitApi();
7378
if (repositories.length === 0) {
7479
throw Error("No git repositories available");
7580
}

src/commands/registerCommands.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from "vscode";
22
import type { TreeSitter } from "../treeSitter/TreeSitter";
33
import type { CommandServerExtension } from "../typings/commandServer";
4-
import type { GitExtension } from "../typings/git";
54
import { getFullCommand } from "../util/getFullCommand";
65
import { GetText } from "./GetText";
76
import { GitParameters, GitUtil } from "./GitUtil";
@@ -29,11 +28,10 @@ type Callback = (...args: any[]) => any;
2928

3029
export function registerCommands(
3130
commandServerExtension: CommandServerExtension,
32-
gitExtension: GitExtension,
3331
treeSitter: TreeSitter
3432
): vscode.Disposable {
3533
const getText = new GetText(commandServerExtension, treeSitter);
36-
const git = new GitUtil(gitExtension);
34+
const git = new GitUtil();
3735

3836
const commands: Record<CommandId, Callback> = {
3937
// Files

src/extension.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ import { registerLanguageFormatters } from "./language/registerLanguageFormatter
77
import { registerStateUpdater } from "./stateUpdater";
88
import { createTabView } from "./tabView";
99
import { TreeSitter } from "./treeSitter/TreeSitter";
10-
import {
11-
getCommandServerExtension,
12-
getGitExtension,
13-
getParseTreeExtension
14-
} from "./util/getExtension";
10+
import { getCommandServerExtension, getParseTreeExtension } from "./util/getExtension";
1511
import { getFakeCommandServerExtension } from "./util/getFakeCommandServerExtension";
1612
import { isTesting } from "./util/isTesting";
1713

@@ -26,7 +22,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
2622

2723
async function activateExtension(context: vscode.ExtensionContext): Promise<void> {
2824
const parseTreeExtension = await getParseTreeExtension();
29-
const gitExtension = await getGitExtension();
3025
const commandServerExtension = isTesting
3126
? getFakeCommandServerExtension()
3227
: await getCommandServerExtension();
@@ -39,7 +34,7 @@ async function activateExtension(context: vscode.ExtensionContext): Promise<void
3934
}
4035

4136
context.subscriptions.push(
42-
registerCommands(commandServerExtension, gitExtension, treeSitter),
37+
registerCommands(commandServerExtension, treeSitter),
4338
registerLanguageDefinitions(),
4439
registerLanguageCompletionProviders(),
4540
registerLanguageCodeActions(treeSitter),

0 commit comments

Comments
 (0)