Skip to content

Commit 712cd33

Browse files
committed
Consolidates command link generation
1 parent eb3fd37 commit 712cd33

18 files changed

+127
-108
lines changed

src/commands/base.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import type {
88
TimelineItem,
99
} from 'vscode';
1010
import { commands, Disposable, Uri, window } from 'vscode';
11-
import type { ActionContext } from '../api/gitlens';
1211
import type { Commands } from '../constants.commands';
1312
import type { StoredNamedRef } from '../constants.storage';
1413
import type { GitBranch } from '../git/models/branch';
@@ -284,14 +283,6 @@ function isGitTimelineItem(item: any): item is GitTimelineItem {
284283
}
285284

286285
export abstract class Command implements Disposable {
287-
static getMarkdownCommandArgsCore<T>(
288-
command: Commands | `${Commands.ActionPrefix}${ActionContext['type']}`,
289-
args: T,
290-
): string {
291-
// Since we are using the command in a markdown link, we need to escape ()'s so they don't get interpreted as markdown
292-
return `command:${command}?${encodeURIComponent(JSON.stringify(args)).replace(/([()])/g, '\\$1')}`;
293-
}
294-
295286
protected readonly contextParsingOptions: CommandContextParsingOptions = { expectsEditor: false };
296287

297288
private readonly _disposable: Disposable;

src/commands/cloudIntegrations.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Commands } from '../constants.commands';
22
import type { SupportedCloudIntegrationIds } from '../constants.integrations';
33
import type { Source } from '../constants.telemetry';
44
import type { Container } from '../container';
5+
import { createMarkdownCommandLink } from '../system/commands';
56
import { command } from '../system/vscode/command';
67
import { Command } from './base';
78

@@ -26,6 +27,13 @@ export class ManageCloudIntegrationsCommand extends Command {
2627

2728
@command()
2829
export class ConnectCloudIntegrationsCommand extends Command {
30+
static createMarkdownCommandLink(args: ConnectCloudIntegrationsCommandArgs): string {
31+
return createMarkdownCommandLink<ConnectCloudIntegrationsCommandArgs>(
32+
Commands.PlusConnectCloudIntegrations,
33+
args,
34+
);
35+
}
36+
2937
constructor(private readonly container: Container) {
3038
super(Commands.PlusConnectCloudIntegrations);
3139
}

src/commands/diffWith.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isCommit } from '../git/models/commit';
88
import { deletedOrMissing } from '../git/models/constants';
99
import { isShaLike, isUncommitted, shortenRevision } from '../git/models/reference';
1010
import { showGenericErrorMessage } from '../messages';
11+
import { createMarkdownCommandLink } from '../system/commands';
1112
import { Logger } from '../system/logger';
1213
import { basename } from '../system/path';
1314
import { command } from '../system/vscode/command';
@@ -31,9 +32,9 @@ export interface DiffWithCommandArgs {
3132

3233
@command()
3334
export class DiffWithCommand extends Command {
34-
static getMarkdownCommandArgs(args: DiffWithCommandArgs): string;
35-
static getMarkdownCommandArgs(commit: GitCommit, line?: number): string;
36-
static getMarkdownCommandArgs(argsOrCommit: DiffWithCommandArgs | GitCommit, line?: number): string {
35+
static createMarkdownCommandLink(args: DiffWithCommandArgs): string;
36+
static createMarkdownCommandLink(commit: GitCommit, line?: number): string;
37+
static createMarkdownCommandLink(argsOrCommit: DiffWithCommandArgs | GitCommit, line?: number): string {
3738
let args: DiffWithCommandArgs | GitCommit;
3839
if (isCommit(argsOrCommit)) {
3940
const commit = argsOrCommit;
@@ -74,7 +75,7 @@ export class DiffWithCommand extends Command {
7475
args = argsOrCommit;
7576
}
7677

77-
return super.getMarkdownCommandArgsCore<DiffWithCommandArgs>(Commands.DiffWith, args);
78+
return createMarkdownCommandLink<DiffWithCommandArgs>(Commands.DiffWith, args);
7879
}
7980

8081
constructor(private readonly container: Container) {

src/commands/inspect.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
showGenericErrorMessage,
1111
showLineUncommittedWarningMessage,
1212
} from '../messages';
13+
import { createMarkdownCommandLink } from '../system/commands';
1314
import { Logger } from '../system/logger';
1415
import { command } from '../system/vscode/command';
1516
import type { CommandContext } from './base';
@@ -21,14 +22,14 @@ export interface InspectCommandArgs {
2122

2223
@command()
2324
export class InspectCommand extends ActiveEditorCommand {
24-
static getMarkdownCommandArgs(sha: string, repoPath: string): string;
25-
static getMarkdownCommandArgs(args: InspectCommandArgs): string;
26-
static getMarkdownCommandArgs(argsOrSha: InspectCommandArgs | string, repoPath?: string): string {
25+
static createMarkdownCommandLink(sha: string, repoPath: string): string;
26+
static createMarkdownCommandLink(args: InspectCommandArgs): string;
27+
static createMarkdownCommandLink(argsOrSha: InspectCommandArgs | string, repoPath?: string): string {
2728
const args =
2829
typeof argsOrSha === 'string'
2930
? { ref: createReference(argsOrSha, repoPath!, { refType: 'revision' }), repoPath: repoPath }
3031
: argsOrSha;
31-
return super.getMarkdownCommandArgsCore<InspectCommandArgs>(Commands.ShowCommitInView, args);
32+
return createMarkdownCommandLink<InspectCommandArgs>(Commands.ShowCommitInView, args);
3233
}
3334

3435
constructor(private readonly container: Container) {

src/commands/inviteToLiveShare.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Commands } from '../constants.commands';
22
import type { Container } from '../container';
3+
import { createMarkdownCommandLink } from '../system/commands';
34
import { command } from '../system/vscode/command';
45
import type { CommandContext } from './base';
56
import { Command, isCommandContextViewNodeHasContributor } from './base';
@@ -10,12 +11,12 @@ export interface InviteToLiveShareCommandArgs {
1011

1112
@command()
1213
export class InviteToLiveShareCommand extends Command {
13-
static getMarkdownCommandArgs(args: InviteToLiveShareCommandArgs): string;
14-
static getMarkdownCommandArgs(email: string | undefined): string;
15-
static getMarkdownCommandArgs(argsOrEmail: InviteToLiveShareCommandArgs | string | undefined): string {
14+
static createMarkdownCommandLink(args: InviteToLiveShareCommandArgs): string;
15+
static createMarkdownCommandLink(email: string | undefined): string;
16+
static createMarkdownCommandLink(argsOrEmail: InviteToLiveShareCommandArgs | string | undefined): string {
1617
const args =
1718
argsOrEmail === undefined || typeof argsOrEmail === 'string' ? { email: argsOrEmail } : argsOrEmail;
18-
return super.getMarkdownCommandArgsCore<InviteToLiveShareCommandArgs>(Commands.InviteToLiveShare, args);
19+
return createMarkdownCommandLink<InviteToLiveShareCommandArgs>(Commands.InviteToLiveShare, args);
1920
}
2021

2122
constructor(private readonly container: Container) {

src/commands/openCommitOnRemote.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
showGenericErrorMessage,
1212
} from '../messages';
1313
import { getBestRepositoryOrShowPicker } from '../quickpicks/repositoryPicker';
14+
import { createMarkdownCommandLink } from '../system/commands';
1415
import { Logger } from '../system/logger';
1516
import { command, executeCommand } from '../system/vscode/command';
1617
import type { CommandContext } from './base';
@@ -30,11 +31,11 @@ export interface OpenCommitOnRemoteCommandArgs {
3031

3132
@command()
3233
export class OpenCommitOnRemoteCommand extends ActiveEditorCommand {
33-
static getMarkdownCommandArgs(sha: string): string;
34-
static getMarkdownCommandArgs(args: OpenCommitOnRemoteCommandArgs): string;
35-
static getMarkdownCommandArgs(argsOrSha: OpenCommitOnRemoteCommandArgs | string): string {
34+
static createMarkdownCommandLink(sha: string): string;
35+
static createMarkdownCommandLink(args: OpenCommitOnRemoteCommandArgs): string;
36+
static createMarkdownCommandLink(argsOrSha: OpenCommitOnRemoteCommandArgs | string): string {
3637
const args: OpenCommitOnRemoteCommandArgs = typeof argsOrSha === 'string' ? { sha: argsOrSha } : argsOrSha;
37-
return super.getMarkdownCommandArgsCore<OpenCommitOnRemoteCommandArgs>(Commands.OpenCommitOnRemote, args);
38+
return createMarkdownCommandLink<OpenCommitOnRemoteCommandArgs>(Commands.OpenCommitOnRemote, args);
3839
}
3940

4041
constructor(private readonly container: Container) {

src/commands/openFileAtRevision.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { showCommitPicker } from '../quickpicks/commitPicker';
1212
import { CommandQuickPickItem } from '../quickpicks/items/common';
1313
import type { DirectiveQuickPickItem } from '../quickpicks/items/directive';
1414
import { createDirectiveQuickPickItem, Directive } from '../quickpicks/items/directive';
15+
import { createMarkdownCommandLink } from '../system/commands';
1516
import { Logger } from '../system/logger';
1617
import { pad } from '../system/string';
1718
import { command } from '../system/vscode/command';
@@ -30,9 +31,9 @@ export interface OpenFileAtRevisionCommandArgs {
3031

3132
@command()
3233
export class OpenFileAtRevisionCommand extends ActiveEditorCommand {
33-
static getMarkdownCommandArgs(args: OpenFileAtRevisionCommandArgs): string;
34-
static getMarkdownCommandArgs(revisionUri: Uri, annotationType?: FileAnnotationType, line?: number): string;
35-
static getMarkdownCommandArgs(
34+
static createMarkdownCommandLink(args: OpenFileAtRevisionCommandArgs): string;
35+
static createMarkdownCommandLink(revisionUri: Uri, annotationType?: FileAnnotationType, line?: number): string;
36+
static createMarkdownCommandLink(
3637
argsOrUri: OpenFileAtRevisionCommandArgs | Uri,
3738
annotationType?: FileAnnotationType,
3839
line?: number,
@@ -50,7 +51,7 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand {
5051
args = argsOrUri;
5152
}
5253

53-
return super.getMarkdownCommandArgsCore<OpenFileAtRevisionCommandArgs>(Commands.OpenFileAtRevision, args);
54+
return createMarkdownCommandLink<OpenFileAtRevisionCommandArgs>(Commands.OpenFileAtRevision, args);
5455
}
5556

5657
constructor(private readonly container: Container) {

src/commands/remoteProviders.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { isRemote } from '../git/models/remote';
66
import type { Repository } from '../git/models/repository';
77
import type { RemoteProvider } from '../git/remotes/remoteProvider';
88
import { showRepositoryPicker } from '../quickpicks/repositoryPicker';
9+
import { createMarkdownCommandLink } from '../system/commands';
910
import { first } from '../system/iterable';
1011
import { command } from '../system/vscode/command';
1112
import type { CommandContext } from './base';
@@ -18,9 +19,9 @@ export interface ConnectRemoteProviderCommandArgs {
1819

1920
@command()
2021
export class ConnectRemoteProviderCommand extends Command {
21-
static getMarkdownCommandArgs(args: ConnectRemoteProviderCommandArgs): string;
22-
static getMarkdownCommandArgs(remote: GitRemote): string;
23-
static getMarkdownCommandArgs(argsOrRemote: ConnectRemoteProviderCommandArgs | GitRemote): string {
22+
static createMarkdownCommandLink(args: ConnectRemoteProviderCommandArgs): string;
23+
static createMarkdownCommandLink(remote: GitRemote): string;
24+
static createMarkdownCommandLink(argsOrRemote: ConnectRemoteProviderCommandArgs | GitRemote): string {
2425
let args: ConnectRemoteProviderCommandArgs | GitCommit;
2526
if (isRemote(argsOrRemote)) {
2627
args = {
@@ -31,7 +32,7 @@ export class ConnectRemoteProviderCommand extends Command {
3132
args = argsOrRemote;
3233
}
3334

34-
return super.getMarkdownCommandArgsCore<ConnectRemoteProviderCommandArgs>(Commands.ConnectRemoteProvider, args);
35+
return createMarkdownCommandLink<ConnectRemoteProviderCommandArgs>(Commands.ConnectRemoteProvider, args);
3536
}
3637

3738
constructor(private readonly container: Container) {
@@ -111,9 +112,9 @@ export interface DisconnectRemoteProviderCommandArgs {
111112

112113
@command()
113114
export class DisconnectRemoteProviderCommand extends Command {
114-
static getMarkdownCommandArgs(args: DisconnectRemoteProviderCommandArgs): string;
115-
static getMarkdownCommandArgs(remote: GitRemote): string;
116-
static getMarkdownCommandArgs(argsOrRemote: DisconnectRemoteProviderCommandArgs | GitRemote): string {
115+
static createMarkdownCommandLink(args: DisconnectRemoteProviderCommandArgs): string;
116+
static createMarkdownCommandLink(remote: GitRemote): string;
117+
static createMarkdownCommandLink(argsOrRemote: DisconnectRemoteProviderCommandArgs | GitRemote): string {
117118
let args: DisconnectRemoteProviderCommandArgs | GitCommit;
118119
if (isRemote(argsOrRemote)) {
119120
args = {
@@ -124,10 +125,7 @@ export class DisconnectRemoteProviderCommand extends Command {
124125
args = argsOrRemote;
125126
}
126127

127-
return super.getMarkdownCommandArgsCore<DisconnectRemoteProviderCommandArgs>(
128-
Commands.DisconnectRemoteProvider,
129-
args,
130-
);
128+
return createMarkdownCommandLink<DisconnectRemoteProviderCommandArgs>(Commands.DisconnectRemoteProvider, args);
131129
}
132130

133131
constructor(private readonly container: Container) {

src/commands/showCommitsInView.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { executeGitCommand } from '../git/actions';
55
import { GitUri } from '../git/gitUri';
66
import { createSearchQueryForCommits } from '../git/search';
77
import { showFileNotUnderSourceControlWarningMessage, showGenericErrorMessage } from '../messages';
8+
import { createMarkdownCommandLink } from '../system/commands';
89
import { filterMap } from '../system/iterable';
910
import { Logger } from '../system/logger';
1011
import { command } from '../system/vscode/command';
@@ -17,11 +18,11 @@ export interface ShowCommitsInViewCommandArgs {
1718

1819
@command()
1920
export class ShowCommitsInViewCommand extends ActiveEditorCommand {
20-
static getMarkdownCommandArgs(sha: string, repoPath: string): string;
21-
static getMarkdownCommandArgs(args: ShowCommitsInViewCommandArgs): string;
22-
static getMarkdownCommandArgs(argsOrSha: ShowCommitsInViewCommandArgs | string, repoPath?: string): string {
21+
static createMarkdownCommandLink(sha: string, repoPath: string): string;
22+
static createMarkdownCommandLink(args: ShowCommitsInViewCommandArgs): string;
23+
static createMarkdownCommandLink(argsOrSha: ShowCommitsInViewCommandArgs | string, repoPath?: string): string {
2324
const args = typeof argsOrSha === 'string' ? { refs: [argsOrSha], repoPath: repoPath } : argsOrSha;
24-
return super.getMarkdownCommandArgsCore<ShowCommitsInViewCommandArgs>(Commands.ShowCommitsInView, args);
25+
return createMarkdownCommandLink<ShowCommitsInViewCommandArgs>(Commands.ShowCommitsInView, args);
2526
}
2627

2728
constructor(private readonly container: Container) {

src/commands/showQuickCommit.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
showGenericErrorMessage,
1313
showLineUncommittedWarningMessage,
1414
} from '../messages';
15+
import { createMarkdownCommandLink } from '../system/commands';
1516
import { Logger } from '../system/logger';
1617
import { command } from '../system/vscode/command';
1718
import type { CommandContext } from './base';
@@ -27,11 +28,11 @@ export interface ShowQuickCommitCommandArgs {
2728

2829
@command()
2930
export class ShowQuickCommitCommand extends ActiveEditorCachedCommand {
30-
static getMarkdownCommandArgs(sha: string, repoPath?: string): string;
31-
static getMarkdownCommandArgs(args: ShowQuickCommitCommandArgs): string;
32-
static getMarkdownCommandArgs(argsOrSha: ShowQuickCommitCommandArgs | string, repoPath?: string): string {
31+
static createMarkdownCommandLink(sha: string, repoPath?: string): string;
32+
static createMarkdownCommandLink(args: ShowQuickCommitCommandArgs): string;
33+
static createMarkdownCommandLink(argsOrSha: ShowQuickCommitCommandArgs | string, repoPath?: string): string {
3334
const args = typeof argsOrSha === 'string' ? { sha: argsOrSha, repoPath: repoPath } : argsOrSha;
34-
return super.getMarkdownCommandArgsCore<ShowQuickCommitCommandArgs>(Commands.ShowQuickCommit, args);
35+
return createMarkdownCommandLink<ShowQuickCommitCommandArgs>(Commands.ShowQuickCommit, args);
3536
}
3637

3738
constructor(private readonly container: Container) {

0 commit comments

Comments
 (0)