Skip to content

Commit 7c196e5

Browse files
committed
Optimizes delay loading of custom views
1 parent 63eb11b commit 7c196e5

File tree

6 files changed

+88
-50
lines changed

6 files changed

+88
-50
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3295,7 +3295,7 @@
32953295
{
32963296
"id": "gitlens.gitExplorer",
32973297
"name": "GitLens",
3298-
"when": "gitlens:enabled && gitlens:gitExplorer && config.gitlens.gitExplorer.enabled"
3298+
"when": "gitlens:enabled && gitlens:gitExplorer"
32993299
},
33003300
{
33013301
"id": "gitlens.resultsExplorer",

src/commands.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ export * from './commands/toggleFileRecentChanges';
4949
export * from './commands/toggleLineBlame';
5050

5151
import * as Commands from './commands';
52+
import { CommandContext, setCommandContext } from './constants';
5253

5354
export function configureCommands(): void {
55+
setCommandContext(CommandContext.KeyMap, Container.config.keymap);
56+
5457
Container.context.subscriptions.push(commands.registerTextEditorCommand('gitlens.computingFileAnnotations', () => { }));
5558

5659
Container.context.subscriptions.push(new Commands.CloseUnchangedFilesCommand());

src/container.ts

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,60 @@
11
'use strict';
2-
import { ExtensionContext } from 'vscode';
2+
import { Disposable, ExtensionContext, languages, workspace } from 'vscode';
33
import { AnnotationController } from './annotations/annotationController';
44
import { CodeLensController } from './codeLensController';
55
import { configuration, IConfig } from './configuration';
66
import { CurrentLineController } from './currentLineController';
77
import { DocumentTracker, GitDocumentState } from './trackers/documentTracker';
88
import { ExplorerCommands } from './views/explorerCommands';
9+
import { GitContentProvider } from './gitContentProvider';
910
import { GitExplorer } from './views/gitExplorer';
11+
import { GitRevisionCodeLensProvider } from './gitRevisionCodeLensProvider';
1012
import { GitService } from './gitService';
1113
import { Keyboard } from './keyboard';
1214
import { ResultsExplorer } from './views/resultsExplorer';
1315

1416
export class Container {
1517

1618
static initialize(context: ExtensionContext, config: IConfig) {
17-
Container._context = context;
18-
Container._config = config;
19+
this._context = context;
20+
this._config = config;
1921

20-
context.subscriptions.push(Container._tracker = new DocumentTracker<GitDocumentState>());
21-
context.subscriptions.push(Container._git = new GitService());
22+
context.subscriptions.push(this._tracker = new DocumentTracker<GitDocumentState>());
23+
context.subscriptions.push(this._git = new GitService());
2224

2325
// Since there is a chicken/egg problem with the DocumentTracker and the GitService, initialize the tracker once the GitService is loaded
24-
Container._tracker.initialize();
26+
this._tracker.initialize();
2527

26-
context.subscriptions.push(Container._annotationController = new AnnotationController());
27-
context.subscriptions.push(Container._currentLineController = new CurrentLineController());
28-
context.subscriptions.push(Container._codeLensController = new CodeLensController());
29-
context.subscriptions.push(Container._explorerCommands = new ExplorerCommands());
30-
context.subscriptions.push(Container._keyboard = new Keyboard());
28+
context.subscriptions.push(this._annotationController = new AnnotationController());
29+
context.subscriptions.push(this._currentLineController = new CurrentLineController());
30+
context.subscriptions.push(this._codeLensController = new CodeLensController());
31+
context.subscriptions.push(this._keyboard = new Keyboard());
3132

32-
Container._gitExplorer = new GitExplorer();
33-
Container._resultsExplorer = new ResultsExplorer();
33+
if (config.gitExplorer.enabled) {
34+
context.subscriptions.push(this._gitExplorer = new GitExplorer());
35+
}
36+
else {
37+
let disposable: Disposable;
38+
disposable = configuration.onDidChange(e => {
39+
if (configuration.changed(e, configuration.name('gitExplorer')('enabled').value)) {
40+
disposable.dispose();
41+
context.subscriptions.push(this._gitExplorer = new GitExplorer());
42+
}
43+
});
44+
}
45+
46+
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitContentProvider.scheme, new GitContentProvider()));
47+
context.subscriptions.push(languages.registerCodeLensProvider(GitRevisionCodeLensProvider.selector, new GitRevisionCodeLensProvider()));
3448
}
3549

3650
private static _annotationController: AnnotationController;
3751
static get annotations() {
38-
return Container._annotationController;
52+
return this._annotationController;
3953
}
4054

4155
private static _codeLensController: CodeLensController;
4256
static get codeLens() {
43-
return Container._codeLensController;
57+
return this._codeLensController;
4458
}
4559

4660
private static _config: IConfig | undefined;
@@ -53,42 +67,49 @@ export class Container {
5367

5468
private static _context: ExtensionContext;
5569
static get context() {
56-
return Container._context;
70+
return this._context;
5771
}
5872

59-
private static _explorerCommands: ExplorerCommands;
73+
private static _explorerCommands: ExplorerCommands | undefined;
6074
static get explorerCommands() {
61-
return Container._explorerCommands;
75+
if (this._explorerCommands === undefined) {
76+
this._context.subscriptions.push(this._explorerCommands = new ExplorerCommands());
77+
}
78+
return this._explorerCommands;
6279
}
6380

6481
private static _git: GitService;
6582
static get git() {
66-
return Container._git;
83+
return this._git;
6784
}
6885

69-
private static _gitExplorer: GitExplorer;
70-
static get gitExplorer() {
71-
return Container._gitExplorer;
86+
private static _gitExplorer: GitExplorer | undefined;
87+
static get gitExplorer(): GitExplorer {
88+
return this._gitExplorer!;
7289
}
7390

7491
private static _keyboard: Keyboard;
7592
static get keyboard() {
76-
return Container._keyboard;
93+
return this._keyboard;
7794
}
7895

7996
private static _currentLineController: CurrentLineController;
8097
static get lineAnnotations() {
81-
return Container._currentLineController;
98+
return this._currentLineController;
8299
}
83100

84-
private static _resultsExplorer: ResultsExplorer;
101+
private static _resultsExplorer: ResultsExplorer | undefined;
85102
static get resultsExplorer() {
86-
return Container._resultsExplorer;
103+
if (this._resultsExplorer === undefined) {
104+
this._context.subscriptions.push(this._resultsExplorer = new ResultsExplorer());
105+
}
106+
107+
return this._resultsExplorer;
87108
}
88109

89110
private static _tracker: DocumentTracker<GitDocumentState>;
90111
static get tracker() {
91-
return Container._tracker;
112+
return this._tracker;
92113
}
93114

94115
static resetConfig() {

src/extension.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
'use strict';
22
import { Objects, Versions } from './system';
3-
import { ConfigurationTarget, ExtensionContext, extensions, languages, window, workspace } from 'vscode';
3+
import { ConfigurationTarget, ExtensionContext, extensions, window, workspace } from 'vscode';
44
import { configuration, Configuration, IConfig } from './configuration';
55
import { CommandContext, ExtensionKey, GlobalState, QualifiedExtensionId, setCommandContext } from './constants';
66
import { configureCommands } from './commands';
77
import { Container } from './container';
8-
import { GitContentProvider } from './gitContentProvider';
9-
import { GitRevisionCodeLensProvider } from './gitRevisionCodeLensProvider';
108
import { GitService } from './gitService';
119
import { Logger } from './logger';
1210
import { Messages, SuppressedMessages } from './messages';
@@ -64,21 +62,11 @@ export async function activate(context: ExtensionContext) {
6462

6563
Container.initialize(context, cfg);
6664

67-
context.subscriptions.push(workspace.registerTextDocumentContentProvider(GitContentProvider.scheme, new GitContentProvider()));
68-
context.subscriptions.push(languages.registerCodeLensProvider(GitRevisionCodeLensProvider.selector, new GitRevisionCodeLensProvider()));
69-
70-
context.subscriptions.push(window.registerTreeDataProvider('gitlens.gitExplorer', Container.gitExplorer));
71-
context.subscriptions.push(window.registerTreeDataProvider('gitlens.resultsExplorer', Container.resultsExplorer));
72-
7365
configureCommands();
7466

7567
// Constantly over my data cap so stop collecting initialized event
7668
// Telemetry.trackEvent('initialized', Objects.flatten(cfg, 'config', true));
7769

78-
setCommandContext(CommandContext.KeyMap, configuration.get(configuration.name('keymap').value));
79-
// Slightly delay enabling the explorer to not stop the rest of GitLens from being usable
80-
setTimeout(() => setCommandContext(CommandContext.GitExplorer, true), 1000);
81-
8270
const duration = process.hrtime(start);
8371
Logger.log(`GitLens(v${gitlensVersion}) activated in ${(duration[0] * 1000) + Math.floor(duration[1] / 1000000)} ms`);
8472
}

src/views/gitExplorer.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ export interface OpenFileRevisionCommandArgs {
1717
showOptions?: TextDocumentShowOptions;
1818
}
1919

20-
export class GitExplorer implements TreeDataProvider<ExplorerNode> {
20+
export class GitExplorer extends Disposable implements TreeDataProvider<ExplorerNode> {
2121

22+
private _disposable: Disposable | undefined;
2223
private _root?: ExplorerNode;
2324
private _view: GitExplorerView | undefined;
2425

@@ -33,14 +34,17 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
3334
}
3435

3536
constructor() {
37+
super(() => this.dispose());
38+
39+
Container.explorerCommands;
3640
commands.registerCommand('gitlens.gitExplorer.refresh', this.refresh, this);
3741
commands.registerCommand('gitlens.gitExplorer.refreshNode', this.refreshNode, this);
3842
commands.registerCommand('gitlens.gitExplorer.setFilesLayoutToAuto', () => this.setFilesLayout(ExplorerFilesLayout.Auto), this);
3943
commands.registerCommand('gitlens.gitExplorer.setFilesLayoutToList', () => this.setFilesLayout(ExplorerFilesLayout.List), this);
4044
commands.registerCommand('gitlens.gitExplorer.setFilesLayoutToTree', () => this.setFilesLayout(ExplorerFilesLayout.Tree), this);
4145

42-
commands.registerCommand('gitlens.gitExplorer.setAutoRefreshToOn', () => this.setAutoRefresh(configuration.get<boolean>(configuration.name('gitExplorer')('autoRefresh').value), true), this);
43-
commands.registerCommand('gitlens.gitExplorer.setAutoRefreshToOff', () => this.setAutoRefresh(configuration.get<boolean>(configuration.name('gitExplorer')('autoRefresh').value), false), this);
46+
commands.registerCommand('gitlens.gitExplorer.setAutoRefreshToOn', () => this.setAutoRefresh(Container.config.gitExplorer.autoRefresh, true), this);
47+
commands.registerCommand('gitlens.gitExplorer.setAutoRefreshToOff', () => this.setAutoRefresh(Container.config.gitExplorer.autoRefresh, false), this);
4448
commands.registerCommand('gitlens.gitExplorer.switchToHistoryView', () => this.switchTo(GitExplorerView.History), this);
4549
commands.registerCommand('gitlens.gitExplorer.switchToRepositoryView', () => this.switchTo(GitExplorerView.Repository), this);
4650

@@ -52,6 +56,10 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
5256
this.onConfigurationChanged(configuration.initializingChangeEvent);
5357
}
5458

59+
dispose() {
60+
this._disposable && this._disposable.dispose();
61+
}
62+
5563
private async onActiveEditorChanged(editor: TextEditor | undefined) {
5664
if (this._view !== GitExplorerView.History) return;
5765

@@ -68,11 +76,15 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
6876
!configuration.changed(e, configuration.name('gitExplorer').value) &&
6977
!configuration.changed(e, configuration.name('defaultGravatarsStyle').value)) return;
7078

79+
if (initializing || configuration.changed(e, configuration.name('gitExplorer')('enabled').value)) {
80+
setCommandContext(CommandContext.GitExplorer, this.config.enabled);
81+
}
82+
7183
if (initializing || configuration.changed(e, configuration.name('gitExplorer')('autoRefresh').value)) {
72-
this.setAutoRefresh(configuration.get<boolean>(configuration.name('gitExplorer')('autoRefresh').value));
84+
this.setAutoRefresh(Container.config.gitExplorer.autoRefresh);
7385
}
7486

75-
let view = configuration.get<GitExplorerView>(configuration.name('gitExplorer')('view').value);
87+
let view = this.config.view;
7688
if (view === GitExplorerView.Auto) {
7789
view = Container.context.workspaceState.get<GitExplorerView>(WorkspaceState.GitExplorerView, GitExplorerView.Repository);
7890
}
@@ -82,6 +94,8 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
8294
setCommandContext(CommandContext.GitExplorerView, this._view);
8395

8496
this.setRoot(await this.getRootNode(window.activeTextEditor));
97+
98+
this._disposable = window.registerTreeDataProvider('gitlens.gitExplorer', this);
8599
}
86100
else {
87101
this.reset(view);
@@ -110,7 +124,7 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
110124
}
111125

112126
get autoRefresh() {
113-
return configuration.get<boolean>(configuration.name('gitExplorer')('autoRefresh').value) &&
127+
return this.config.autoRefresh &&
114128
Container.context.workspaceState.get<boolean>(WorkspaceState.GitExplorerAutoRefresh, true);
115129
}
116130

@@ -248,7 +262,7 @@ export class GitExplorer implements TreeDataProvider<ExplorerNode> {
248262
setView(view: GitExplorerView) {
249263
if (this._view === view) return;
250264

251-
if (configuration.get<GitExplorerView>(configuration.name('gitExplorer')('view').value) === GitExplorerView.Auto) {
265+
if (Container.config.gitExplorer.view === GitExplorerView.Auto) {
252266
Container.context.workspaceState.update(WorkspaceState.GitExplorerView, view);
253267
}
254268

src/views/resultsExplorer.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
import { Functions } from '../system';
3-
import { commands, ConfigurationChangeEvent, ConfigurationTarget, Event, EventEmitter, TreeDataProvider, TreeItem } from 'vscode';
3+
import { commands, ConfigurationChangeEvent, ConfigurationTarget, Disposable, Event, EventEmitter, TreeDataProvider, TreeItem, window } from 'vscode';
44
import { configuration, ExplorerFilesLayout, IExplorerConfig } from '../configuration';
55
import { CommandContext, setCommandContext, WorkspaceState } from '../constants';
66
import { Container } from '../container';
@@ -12,8 +12,9 @@ import { Messages } from '../messages';
1212

1313
export * from './explorerNodes';
1414

15-
export class ResultsExplorer implements TreeDataProvider<ExplorerNode> {
15+
export class ResultsExplorer extends Disposable implements TreeDataProvider<ExplorerNode> {
1616

17+
private _disposable: Disposable | undefined;
1718
private _roots: ExplorerNode[] = [];
1819

1920
private _onDidChangeTreeData = new EventEmitter<ExplorerNode>();
@@ -22,6 +23,9 @@ export class ResultsExplorer implements TreeDataProvider<ExplorerNode> {
2223
}
2324

2425
constructor() {
26+
super(() => this.dispose());
27+
28+
Container.explorerCommands;
2529
commands.registerCommand('gitlens.resultsExplorer.refresh', this.refreshNodes, this);
2630
commands.registerCommand('gitlens.resultsExplorer.refreshNode', this.refreshNode, this);
2731
commands.registerCommand('gitlens.resultsExplorer.setFilesLayoutToAuto', () => this.setFilesLayout(ExplorerFilesLayout.Auto), this);
@@ -41,6 +45,10 @@ export class ResultsExplorer implements TreeDataProvider<ExplorerNode> {
4145
this.onConfigurationChanged(configuration.initializingChangeEvent);
4246
}
4347

48+
dispose() {
49+
this._disposable && this._disposable.dispose();
50+
}
51+
4452
private async onConfigurationChanged(e: ConfigurationChangeEvent) {
4553
const initializing = configuration.initializing(e);
4654

@@ -51,6 +59,10 @@ export class ResultsExplorer implements TreeDataProvider<ExplorerNode> {
5159
if (!initializing && this._roots.length !== 0) {
5260
this.refresh(RefreshReason.ConfigurationChanged);
5361
}
62+
63+
if (initializing) {
64+
this._disposable = window.registerTreeDataProvider('gitlens.resultsExplorer', this);
65+
}
5466
}
5567

5668
get config(): IExplorerConfig {

0 commit comments

Comments
 (0)