-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
statusBarController.ts
313 lines (279 loc) · 10 KB
/
statusBarController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
'use strict';
import {
CancellationToken,
CancellationTokenSource,
ConfigurationChangeEvent,
Disposable,
StatusBarAlignment,
StatusBarItem,
TextEditor,
Uri,
window,
} from 'vscode';
import { command, Commands, ToggleFileChangesAnnotationCommandArgs } from '../commands';
import { configuration, FileAnnotationType, StatusBarCommand } from '../configuration';
import { GlyphChars, isTextEditor } from '../constants';
import { Container } from '../container';
import { CommitFormatter, GitBlameCommit, PullRequest } from '../git/git';
import { LogCorrelationContext, Logger } from '../logger';
import { debug, Promises } from '../system';
import { LinesChangeEvent } from '../trackers/gitLineTracker';
export class StatusBarController implements Disposable {
private _cancellation: CancellationTokenSource | undefined;
private readonly _disposable: Disposable;
private _statusBarBlame: StatusBarItem | undefined;
private _statusBarMode: StatusBarItem | undefined;
constructor() {
this._disposable = Disposable.from(configuration.onDidChange(this.onConfigurationChanged, this));
this.onConfigurationChanged();
}
dispose() {
this.clearBlame();
this._statusBarBlame?.dispose();
this._statusBarMode?.dispose();
Container.lineTracker.stop(this);
this._disposable.dispose();
}
private onConfigurationChanged(e?: ConfigurationChangeEvent) {
if (configuration.changed(e, 'mode')) {
const mode =
Container.config.mode.active && Container.config.mode.statusBar.enabled
? Container.config.modes?.[Container.config.mode.active]
: undefined;
if (mode?.statusBarItemName) {
const alignment =
Container.config.mode.statusBar.alignment !== 'left'
? StatusBarAlignment.Right
: StatusBarAlignment.Left;
if (configuration.changed(e, 'mode.statusBar.alignment')) {
if (this._statusBarMode?.alignment !== alignment) {
this._statusBarMode?.dispose();
this._statusBarMode = undefined;
}
}
this._statusBarMode =
this._statusBarMode ??
window.createStatusBarItem(
'gitlens.mode',
alignment,
alignment === StatusBarAlignment.Right ? 999 : 1,
);
this._statusBarMode.name = 'GitLens Modes';
this._statusBarMode.command = Commands.SwitchMode;
this._statusBarMode.text = mode.statusBarItemName;
this._statusBarMode.tooltip = 'Switch GitLens Mode';
this._statusBarMode.show();
} else {
this._statusBarMode?.dispose();
this._statusBarMode = undefined;
}
}
if (!configuration.changed(e, 'statusBar')) return;
if (Container.config.statusBar.enabled) {
const alignment =
Container.config.statusBar.alignment !== 'left' ? StatusBarAlignment.Right : StatusBarAlignment.Left;
if (configuration.changed(e, 'statusBar.alignment')) {
if (this._statusBarBlame?.alignment !== alignment) {
this._statusBarBlame?.dispose();
this._statusBarBlame = undefined;
}
}
this._statusBarBlame =
this._statusBarBlame ??
window.createStatusBarItem(
'gitlens.blame',
alignment,
alignment === StatusBarAlignment.Right ? 1000 : 0,
);
this._statusBarBlame.name = 'GitLens Current Line Blame';
this._statusBarBlame.command = Container.config.statusBar.command;
if (configuration.changed(e, 'statusBar.enabled')) {
Container.lineTracker.start(
this,
Container.lineTracker.onDidChangeActiveLines(this.onActiveLinesChanged, this),
);
}
} else if (configuration.changed(e, 'statusBar.enabled')) {
Container.lineTracker.stop(this);
this._statusBarBlame?.dispose();
this._statusBarBlame = undefined;
}
}
@debug({
args: {
0: (e: LinesChangeEvent) =>
`editor=${e.editor?.document.uri.toString(true)}, selections=${e.selections
?.map(s => `[${s.anchor}-${s.active}]`)
.join(',')}, pending=${Boolean(e.pending)}, reason=${e.reason}`,
},
})
private onActiveLinesChanged(e: LinesChangeEvent) {
// If we need to reduceFlicker, don't clear if only the selected lines changed
let clear = !(
Container.config.statusBar.reduceFlicker &&
e.reason === 'selection' &&
(e.pending || e.selections != null)
);
if (!e.pending && e.selections != null) {
const state = Container.lineTracker.getState(e.selections[0].active);
if (state?.commit != null) {
void this.updateBlame(e.editor!, state.commit);
return;
}
clear = true;
}
if (clear) {
this.clearBlame();
} else if (this._statusBarBlame != null) {
this._statusBarBlame.text = this._statusBarBlame.text.replace('$(git-commit)', '$(loading~spin)');
}
}
clearBlame() {
this._cancellation?.cancel();
this._statusBarBlame?.hide();
}
@debug({ args: false })
private async updateBlame(editor: TextEditor, commit: GitBlameCommit, options?: { pr?: PullRequest | null }) {
const cfg = Container.config.statusBar;
if (!cfg.enabled || this._statusBarBlame == null || !isTextEditor(editor)) return;
const cc = Logger.getCorrelationContext();
// TODO: Make this configurable?
const timeout = 100;
const [getBranchAndTagTips, pr] = await Promise.all([
CommitFormatter.has(cfg.format, 'tips')
? Container.git.getBranchesAndTagsTipsFn(commit.repoPath)
: undefined,
cfg.pullRequests.enabled &&
CommitFormatter.has(
cfg.format,
'pullRequest',
'pullRequestAgo',
'pullRequestAgoOrDate',
'pullRequestDate',
'pullRequestState',
) &&
options?.pr === undefined
? this.getPullRequest(commit, { timeout: timeout })
: options?.pr ?? undefined,
]);
if (pr != null) {
this._cancellation?.cancel();
this._cancellation = new CancellationTokenSource();
void this.waitForPendingPullRequest(editor, commit, pr, this._cancellation.token, timeout, cc);
}
this._statusBarBlame.text = `$(git-commit) ${CommitFormatter.fromTemplate(cfg.format, commit, {
dateFormat: cfg.dateFormat === null ? Container.config.defaultDateFormat : cfg.dateFormat,
getBranchAndTagTips: getBranchAndTagTips,
messageTruncateAtNewLine: true,
pullRequestOrRemote: pr,
pullRequestPendingMessage: 'PR $(loading~spin)',
})}`;
switch (cfg.command) {
case StatusBarCommand.CopyRemoteCommitUrl:
this._statusBarBlame.tooltip = 'Copy Remote Commit Url';
break;
case StatusBarCommand.CopyRemoteFileUrl:
this._statusBarBlame.command = Commands.CopyRemoteFileUrl;
this._statusBarBlame.tooltip = 'Copy Remote File Revision Url';
break;
case StatusBarCommand.DiffWithPrevious:
this._statusBarBlame.command = Commands.DiffLineWithPrevious;
this._statusBarBlame.tooltip = 'Open Line Changes with Previous Revision';
break;
case StatusBarCommand.DiffWithWorking:
this._statusBarBlame.command = Commands.DiffLineWithWorking;
this._statusBarBlame.tooltip = 'Open Line Changes with Working File';
break;
case StatusBarCommand.OpenCommitOnRemote:
this._statusBarBlame.tooltip = 'Open Commit on Remote';
break;
case StatusBarCommand.OpenFileOnRemote:
this._statusBarBlame.tooltip = 'Open Revision on Remote';
break;
case StatusBarCommand.RevealCommitInView:
this._statusBarBlame.tooltip = 'Reveal Commit in the Side Bar';
break;
case StatusBarCommand.ShowCommitsInView:
this._statusBarBlame.tooltip = 'Search for Commit';
break;
case StatusBarCommand.ShowQuickCommitDetails:
this._statusBarBlame.tooltip = 'Show Commit';
break;
case StatusBarCommand.ShowQuickCommitFileDetails:
this._statusBarBlame.tooltip = 'Show Commit (file)';
break;
case StatusBarCommand.ShowQuickCurrentBranchHistory:
this._statusBarBlame.tooltip = 'Show Branch History';
break;
case StatusBarCommand.ShowQuickFileHistory:
this._statusBarBlame.tooltip = 'Show File History';
break;
case StatusBarCommand.ToggleCodeLens:
this._statusBarBlame.tooltip = 'Toggle Git CodeLens';
break;
case StatusBarCommand.ToggleFileBlame:
this._statusBarBlame.tooltip = 'Toggle File Blame';
break;
case StatusBarCommand.ToggleFileChanges: {
this._statusBarBlame.command = command<[Uri, ToggleFileChangesAnnotationCommandArgs]>({
title: 'Toggle File Changes',
command: Commands.ToggleFileChanges,
arguments: [
commit.uri,
{
type: FileAnnotationType.Changes,
context: { sha: commit.sha, only: false, selection: false },
},
],
});
this._statusBarBlame.tooltip = 'Toggle File Changes';
break;
}
case StatusBarCommand.ToggleFileChangesOnly: {
this._statusBarBlame.command = command<[Uri, ToggleFileChangesAnnotationCommandArgs]>({
title: 'Toggle File Changes',
command: Commands.ToggleFileChanges,
arguments: [
commit.uri,
{
type: FileAnnotationType.Changes,
context: { sha: commit.sha, only: true, selection: false },
},
],
});
this._statusBarBlame.tooltip = 'Toggle File Changes';
break;
}
case StatusBarCommand.ToggleFileHeatmap:
this._statusBarBlame.tooltip = 'Toggle File Heatmap';
break;
}
this._statusBarBlame.show();
}
private async getPullRequest(commit: GitBlameCommit, { timeout }: { timeout?: number } = {}) {
const remote = await Container.git.getRichRemoteProvider(commit.repoPath);
if (remote?.provider == null) return undefined;
const { provider } = remote;
try {
return await Container.git.getPullRequestForCommit(commit.ref, provider, { timeout: timeout });
} catch (ex) {
return ex;
}
}
private async waitForPendingPullRequest(
editor: TextEditor,
commit: GitBlameCommit,
pr: PullRequest | Promises.CancellationError<Promise<PullRequest | undefined>> | undefined,
cancellationToken: CancellationToken,
timeout: number,
cc: LogCorrelationContext | undefined,
) {
if (cancellationToken.isCancellationRequested || !(pr instanceof Promises.CancellationError)) return;
// If the PR timed out, refresh the status bar once it completes
Logger.debug(cc, `${GlyphChars.Dot} pull request query took too long (over ${timeout} ms)`);
pr = await pr.promise;
if (cancellationToken.isCancellationRequested) return;
Logger.debug(cc, `${GlyphChars.Dot} pull request query completed; refreshing...`);
void this.updateBlame(editor, commit, { pr: pr ?? null });
}
}