-
Notifications
You must be signed in to change notification settings - Fork 29.4k
/
terminalQuickFixBuiltinActions.ts
143 lines (138 loc) · 4.61 KB
/
terminalQuickFixBuiltinActions.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { TerminalQuickFixMatchResult, ITerminalQuickFixOptions, ITerminalInstance, TerminalQuickFixAction } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ITerminalCommand } from 'vs/workbench/contrib/terminal/common/terminal';
import { IExtensionTerminalQuickFix } from 'vs/platform/terminal/common/terminal';
export const GitCommandLineRegex = /git/;
export const GitPushCommandLineRegex = /git\s+push/;
export const GitTwoDashesRegex = /error: did you mean `--(.+)` \(with two dashes\)\?/;
export const AnyCommandLineRegex = /.+/;
export const GitSimilarOutputRegex = /(?:(most similar (command|commands) (is|are)))((\n\s*(?<fixedCommand>[^\s]+))+)/m;
export const FreePortOutputRegex = /address already in use (0\.0\.0\.0|127\.0\.0\.1|localhost|::):(?<portNumber>\d{4,5})|Unable to bind [^ ]*:(\d{4,5})|can't listen on port (\d{4,5})|listen EADDRINUSE [^ ]*:(\d{4,5})/;
export const GitPushOutputRegex = /git push --set-upstream origin (?<branchName>[^\s]+)/;
// The previous line starts with "Create a pull request for \'([^\s]+)\' on GitHub by visiting:\s*"
// it's safe to assume it's a github pull request if the URL includes `/pull/`
export const GitCreatePrOutputRegex = /remote:\s*(?<link>https:\/\/github\.com\/.+\/.+\/pull\/new\/.+)/;
export function gitSimilar(): ITerminalQuickFixOptions {
return {
source: 'builtin',
id: 'Git Similar',
commandLineMatcher: GitCommandLineRegex,
outputMatcher: {
lineMatcher: GitSimilarOutputRegex,
anchor: 'bottom',
offset: 0,
length: 10
},
exitStatus: false,
getQuickFixes: (matchResult: TerminalQuickFixMatchResult, command: ITerminalCommand) => {
if (!matchResult?.outputMatch) {
return;
}
const actions: TerminalQuickFixAction[] = [];
const results = matchResult.outputMatch[0].split('\n').map(r => r.trim());
for (let i = 1; i < results.length; i++) {
const fixedCommand = results[i];
if (fixedCommand) {
actions.push({
id: 'Git Similar',
type: 'command',
command: command.command.replace(/git\s+[^\s]+/, `git ${fixedCommand}`),
addNewLine: true
});
}
}
return actions;
}
};
}
export function gitTwoDashes(): ITerminalQuickFixOptions {
return {
source: 'builtin',
id: 'Git Two Dashes',
commandLineMatcher: GitCommandLineRegex,
outputMatcher: {
lineMatcher: GitTwoDashesRegex,
anchor: 'bottom',
offset: 0,
length: 2
},
exitStatus: false,
getQuickFixes: (matchResult: TerminalQuickFixMatchResult, command: ITerminalCommand) => {
const problemArg = matchResult?.outputMatch?.[1];
if (!problemArg) {
return;
}
return {
type: 'command',
id: 'Git Two Dashes',
command: command.command.replace(` -${problemArg}`, ` --${problemArg}`),
addNewLine: true
};
}
};
}
export function freePort(terminalInstance?: Partial<ITerminalInstance>): ITerminalQuickFixOptions {
return {
source: 'builtin',
id: 'Free Port',
commandLineMatcher: AnyCommandLineRegex,
outputMatcher: {
lineMatcher: FreePortOutputRegex,
anchor: 'bottom',
offset: 0,
length: 30
},
exitStatus: false,
getQuickFixes: (matchResult: TerminalQuickFixMatchResult, command: ITerminalCommand) => {
const port = matchResult?.outputMatch?.groups?.portNumber;
if (!port) {
return;
}
const label = localize("terminal.freePort", "Free port {0}", port);
return {
class: undefined,
tooltip: label,
id: 'terminal.freePort',
label,
enabled: true,
run: async () => {
await terminalInstance?.freePortKillProcess?.(port, command.command);
}
};
}
};
}
export function gitPushSetUpstream(): IExtensionTerminalQuickFix {
return {
id: 'Git Push Set Upstream',
commandLineMatcher: GitPushCommandLineRegex,
outputMatcher: {
lineMatcher: GitPushOutputRegex,
anchor: 'bottom',
offset: 0,
length: 5
},
exitStatus: false,
commandToRun: 'git push --set-upstream origin ${group:branchName}',
extensionIdentifier: 'git'
};
}
export function gitCreatePr(): IExtensionTerminalQuickFix {
return {
id: 'Git Create Pr',
commandLineMatcher: GitPushCommandLineRegex,
outputMatcher: {
lineMatcher: GitCreatePrOutputRegex,
anchor: 'bottom',
offset: 0,
length: 5
},
exitStatus: true,
linkToOpen: '${group:link}',
extensionIdentifier: 'git'
};
}