-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
base.ts
457 lines (368 loc) · 14.1 KB
/
base.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import type {
GitTimelineItem,
SourceControlResourceGroup,
SourceControlResourceState,
TextEditor,
TextEditorEdit,
TimelineItem,
} from 'vscode';
import { commands, Disposable, Uri, window } from 'vscode';
import type { ActionContext } from '../api/gitlens';
import type { Commands } from '../constants';
import type { GitBranch } from '../git/models/branch';
import { isBranch } from '../git/models/branch';
import type { GitCommit, GitStashCommit } from '../git/models/commit';
import { isCommit } from '../git/models/commit';
import { GitContributor } from '../git/models/contributor';
import type { GitFile } from '../git/models/file';
import type { GitReference } from '../git/models/reference';
import { GitRemote } from '../git/models/remote';
import { Repository } from '../git/models/repository';
import type { GitTag } from '../git/models/tag';
import { isTag } from '../git/models/tag';
import { sequentialize } from '../system/function';
import { ViewNode, ViewRefNode } from '../views/nodes/viewNode';
export function getCommandUri(uri?: Uri, editor?: TextEditor): Uri | undefined {
// Always use the editor.uri (if we have one), so we are correct for a split diff
return editor?.document?.uri ?? uri;
}
export interface CommandContextParsingOptions {
expectsEditor: boolean;
}
export interface CommandBaseContext {
command: string;
editor?: TextEditor;
uri?: Uri;
}
export interface CommandGitTimelineItemContext extends CommandBaseContext {
readonly type: 'timeline-item:git';
readonly item: GitTimelineItem;
readonly uri: Uri;
}
export interface CommandScmGroupsContext extends CommandBaseContext {
readonly type: 'scm-groups';
readonly scmResourceGroups: SourceControlResourceGroup[];
}
export interface CommandScmStatesContext extends CommandBaseContext {
readonly type: 'scm-states';
readonly scmResourceStates: SourceControlResourceState[];
}
export interface CommandUnknownContext extends CommandBaseContext {
readonly type: 'unknown';
}
export interface CommandUriContext extends CommandBaseContext {
readonly type: 'uri';
}
export interface CommandUrisContext extends CommandBaseContext {
readonly type: 'uris';
readonly uris: Uri[];
}
// export interface CommandViewContext extends CommandBaseContext {
// readonly type: 'view';
// }
export interface CommandViewNodeContext extends CommandBaseContext {
readonly type: 'viewItem';
readonly node: ViewNode;
}
export interface CommandViewNodesContext extends CommandBaseContext {
readonly type: 'viewItems';
readonly node: ViewNode;
readonly nodes: ViewNode[];
}
export function isCommandContextGitTimelineItem(context: CommandContext): context is CommandGitTimelineItemContext {
return context.type === 'timeline-item:git';
}
export function isCommandContextViewNodeHasBranch(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { branch: GitBranch } } {
if (context.type !== 'viewItem') return false;
return isBranch((context.node as ViewNode & { branch: GitBranch }).branch);
}
export function isCommandContextViewNodeHasCommit<T extends GitCommit | GitStashCommit>(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { commit: T } } {
if (context.type !== 'viewItem') return false;
return isCommit((context.node as ViewNode & { commit: GitCommit | GitStashCommit }).commit);
}
export function isCommandContextViewNodeHasContributor(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { contributor: GitContributor } } {
if (context.type !== 'viewItem') return false;
return GitContributor.is((context.node as ViewNode & { contributor: GitContributor }).contributor);
}
export function isCommandContextViewNodeHasFile(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { file: GitFile; repoPath: string } } {
if (context.type !== 'viewItem') return false;
const node = context.node as ViewNode & { file: GitFile; repoPath: string };
return node.file != null && (node.file.repoPath != null || node.repoPath != null);
}
export function isCommandContextViewNodeHasFileCommit(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { commit: GitCommit; file: GitFile; repoPath: string } } {
if (context.type !== 'viewItem') return false;
const node = context.node as ViewNode & { commit: GitCommit; file: GitFile; repoPath: string };
return node.file != null && isCommit(node.commit) && (node.file.repoPath != null || node.repoPath != null);
}
export function isCommandContextViewNodeHasFileRefs(context: CommandContext): context is CommandViewNodeContext & {
node: ViewNode & { file: GitFile; ref1: string; ref2: string; repoPath: string };
} {
if (context.type !== 'viewItem') return false;
const node = context.node as ViewNode & { file: GitFile; ref1: string; ref2: string; repoPath: string };
return (
node.file != null &&
node.ref1 != null &&
node.ref2 != null &&
(node.file.repoPath != null || node.repoPath != null)
);
}
export function isCommandContextViewNodeHasRef(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { ref: GitReference } } {
return context.type === 'viewItem' && context.node instanceof ViewRefNode;
}
export function isCommandContextViewNodeHasRemote(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { remote: GitRemote } } {
if (context.type !== 'viewItem') return false;
return GitRemote.is((context.node as ViewNode & { remote: GitRemote }).remote);
}
export function isCommandContextViewNodeHasRepository(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { repo: Repository } } {
if (context.type !== 'viewItem') return false;
return (context.node as ViewNode & { repo?: Repository }).repo instanceof Repository;
}
export function isCommandContextViewNodeHasRepoPath(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { repoPath: string } } {
if (context.type !== 'viewItem') return false;
return typeof (context.node as ViewNode & { repoPath?: string }).repoPath === 'string';
}
export function isCommandContextViewNodeHasTag(
context: CommandContext,
): context is CommandViewNodeContext & { node: ViewNode & { tag: GitTag } } {
if (context.type !== 'viewItem') return false;
return isTag((context.node as ViewNode & { tag: GitTag }).tag);
}
export type CommandContext =
| CommandGitTimelineItemContext
| CommandScmGroupsContext
| CommandScmStatesContext
| CommandUnknownContext
| CommandUriContext
| CommandUrisContext
// | CommandViewContext
| CommandViewNodeContext
| CommandViewNodesContext;
function isScmResourceGroup(group: any): group is SourceControlResourceGroup {
if (group == null) return false;
return (
(group as SourceControlResourceGroup).id != null &&
(group as SourceControlResourceGroup).label != null &&
(group as SourceControlResourceGroup).resourceStates != null &&
Array.isArray((group as SourceControlResourceGroup).resourceStates)
);
}
function isScmResourceState(resource: any): resource is SourceControlResourceState {
if (resource == null) return false;
return (resource as SourceControlResourceState).resourceUri != null;
}
function isTimelineItem(item: any): item is TimelineItem {
if (item == null) return false;
return (item as TimelineItem).timestamp != null && (item as TimelineItem).label != null;
}
function isGitTimelineItem(item: any): item is GitTimelineItem {
if (item == null) return false;
return (
isTimelineItem(item) &&
(item as GitTimelineItem).ref != null &&
(item as GitTimelineItem).previousRef != null &&
(item as GitTimelineItem).message != null
);
}
export abstract class Command implements Disposable {
static getMarkdownCommandArgsCore<T>(
command: Commands | `${Commands.ActionPrefix}${ActionContext['type']}`,
args: T,
): string {
return `command:${command}?${encodeURIComponent(JSON.stringify(args))}`;
}
protected readonly contextParsingOptions: CommandContextParsingOptions = { expectsEditor: false };
private readonly _disposable: Disposable;
constructor(command: Commands | Commands[]) {
if (typeof command === 'string') {
this._disposable = commands.registerCommand(
command,
(...args: any[]) => this._execute(command, ...args),
this,
);
return;
}
const subscriptions = command.map(cmd =>
commands.registerCommand(cmd, (...args: any[]) => this._execute(cmd, ...args), this),
);
this._disposable = Disposable.from(...subscriptions);
}
dispose() {
this._disposable.dispose();
}
protected preExecute(_context: CommandContext, ...args: any[]): Promise<unknown> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this.execute(...args);
}
abstract execute(...args: any[]): any;
protected _execute(command: string, ...args: any[]): Promise<unknown> {
const [context, rest] = parseCommandContext(command, { ...this.contextParsingOptions }, ...args);
// If there an array of contexts, then we want to execute the command for each
if (Array.isArray(context)) {
return sequentialize(
this.preExecute,
context.map<[CommandContext, ...any[]]>((c: CommandContext) => [c, ...rest]),
this,
);
}
return this.preExecute(context, ...rest);
}
}
function parseCommandContext(
command: string,
options: CommandContextParsingOptions,
...args: any[]
): [CommandContext | CommandContext[], any[]] {
let editor: TextEditor | undefined = undefined;
let firstArg = args[0];
if (options.expectsEditor) {
if (firstArg == null || (firstArg.id != null && firstArg.document?.uri != null)) {
editor = firstArg;
args = args.slice(1);
firstArg = args[0];
}
if (args.length > 0 && (firstArg == null || firstArg instanceof Uri)) {
const [uri, ...rest] = args as [Uri, any];
if (uri != null) {
// If the uri matches the active editor (or we are in a left-hand side of a diff), then pass the active editor
if (
editor == null &&
(uri.toString() === window.activeTextEditor?.document.uri.toString() ||
command.endsWith('InDiffLeft'))
) {
editor = window.activeTextEditor;
}
const uris = rest[0];
if (uris != null && Array.isArray(uris) && uris.length !== 0 && uris[0] instanceof Uri) {
return [{ command: command, type: 'uris', editor: editor, uri: uri, uris: uris }, rest.slice(1)];
}
return [{ command: command, type: 'uri', editor: editor, uri: uri }, rest];
}
args = args.slice(1);
} else if (editor == null) {
// If we are expecting an editor and we have no uri, then pass the active editor
editor = window.activeTextEditor;
}
}
if (firstArg instanceof ViewNode) {
let [node, ...rest] = args as [ViewNode, unknown];
// If there is a node followed by an array of nodes, then we want to execute the command for each
firstArg = rest[0];
if (Array.isArray(firstArg) && firstArg[0] instanceof ViewNode) {
let nodes;
[nodes, ...rest] = rest as unknown as [ViewNode[], unknown];
const contexts: CommandContext[] = [];
for (const n of nodes) {
if (n?.constructor === node.constructor) {
contexts.push({ command: command, type: 'viewItem', node: n, uri: n.uri });
}
}
return [contexts, rest];
}
return [{ command: command, type: 'viewItem', node: node, uri: node.uri }, rest];
}
if (isScmResourceState(firstArg)) {
const states = [];
let count = 0;
for (const arg of args) {
if (!isScmResourceState(arg)) break;
count++;
states.push(arg);
}
return [
{ command: command, type: 'scm-states', scmResourceStates: states, uri: states[0].resourceUri },
args.slice(count),
];
}
if (isScmResourceGroup(firstArg)) {
const groups = [];
let count = 0;
for (const arg of args) {
if (!isScmResourceGroup(arg)) break;
count++;
groups.push(arg);
}
return [{ command: command, type: 'scm-groups', scmResourceGroups: groups }, args.slice(count)];
}
if (isGitTimelineItem(firstArg)) {
const [item, uri, ...rest] = args as [GitTimelineItem, Uri, any];
return [{ command: command, type: 'timeline-item:git', item: item, uri: uri }, rest];
}
return [{ command: command, type: 'unknown', editor: editor, uri: editor?.document.uri }, args];
}
export abstract class ActiveEditorCommand extends Command {
protected override readonly contextParsingOptions: CommandContextParsingOptions = { expectsEditor: true };
constructor(command: Commands | Commands[]) {
super(command);
}
protected override preExecute(context: CommandContext, ...args: any[]): Promise<any> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this.execute(context.editor, context.uri, ...args);
}
protected override _execute(command: string, ...args: any[]): any {
return super._execute(command, undefined, ...args);
}
abstract override execute(editor?: TextEditor, ...args: any[]): any;
}
let lastCommand: { command: string; args: any[] } | undefined = undefined;
export function getLastCommand() {
return lastCommand;
}
export abstract class ActiveEditorCachedCommand extends ActiveEditorCommand {
constructor(command: Commands | Commands[]) {
super(command);
}
protected override _execute(command: string, ...args: any[]): any {
lastCommand = {
command: command,
args: args,
};
return super._execute(command, ...args);
}
abstract override execute(editor: TextEditor, ...args: any[]): any;
}
export abstract class EditorCommand implements Disposable {
private readonly _disposable: Disposable;
constructor(command: Commands | Commands[]) {
if (!Array.isArray(command)) {
command = [command];
}
const subscriptions = [];
for (const cmd of command) {
subscriptions.push(
commands.registerTextEditorCommand(
cmd,
(editor: TextEditor, edit: TextEditorEdit, ...args: any[]) =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
this.executeCore(cmd, editor, edit, ...args),
this,
),
);
}
this._disposable = Disposable.from(...subscriptions);
}
dispose() {
this._disposable.dispose();
}
private executeCore(command: string, editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any {
return this.execute(editor, edit, ...args);
}
abstract execute(editor: TextEditor, edit: TextEditorEdit, ...args: any[]): any;
}