Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various terminal quick fix fixes #162083

Merged
merged 5 commits into from Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -660,23 +660,20 @@ export function getOutputMatchForCommand(executedMarker: IMarker | undefined, en
if (outputMatcher?.length && (endLine - startLine) < outputMatcher.length) {
return undefined;
}
let output = '';
let line: string | undefined;
if (outputMatcher?.anchor === 'bottom') {
for (let i = endLine - (outputMatcher.offset || 0); i >= startLine; i--) {
line = getXtermLineContent(buffer, i, i, cols);
output = line + output;
const match = output.match(outputMatcher.lineMatcher);
const match = line.match(outputMatcher.lineMatcher);
if (match) {
return match;
}
}
} else {
for (let i = startLine + (outputMatcher?.offset || 0); i < endLine; i++) {
line = getXtermLineContent(buffer, i, i, cols);
output += line;
if (outputMatcher) {
const match = output.match(outputMatcher.lineMatcher);
const match = line.match(outputMatcher.lineMatcher);
if (match) {
return match;
}
Expand Down
2 changes: 0 additions & 2 deletions src/vs/workbench/contrib/terminal/browser/terminal.ts
Expand Up @@ -920,14 +920,12 @@ export interface ITerminalInstance {
}

export interface ITerminalQuickFixOptions {
quickFixLabel: string | DynamicQuickFixLabel;
commandLineMatcher: string | RegExp;
outputMatcher?: ITerminalOutputMatcher;
getQuickFixes: QuickFixCallback;
exitStatus?: boolean;
}
export type QuickFixMatchResult = { commandLineMatch: RegExpMatchArray; outputMatch?: RegExpMatchArray | null };
export type DynamicQuickFixLabel = (matchResult: QuickFixMatchResult) => string;
export type QuickFixCallback = (matchResult: QuickFixMatchResult, command: ITerminalCommand) => ITerminalQuickFixAction[] | undefined;

export interface ITerminalQuickFixAction extends IAction {
Expand Down
Expand Up @@ -16,12 +16,13 @@ export const AnyCommandLineRegex = /.+/;
export const GitSimilarOutputRegex = /most similar command is\s*([^\s]{3,})/;
export const FreePortOutputRegex = /address already in use \d\.\d\.\d\.\d:(\d\d\d\d)|Unable to bind [^ ]*:(\d+)|can't listen on port (\d+)|listen EADDRINUSE [^ ]*:(\d+)/;
export const GitPushOutputRegex = /git push --set-upstream origin ([^\s]+)/;
export const GitCreatePrOutputRegex = /Create a pull request for \'([^\s]+)\' on GitHub by visiting:\s*remote:\s*(https:.+pull.+)/;
// 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*(https:\/\/github\.com\/.+\/.+\/pull\/new\/.+)/;

export function gitSimilarCommand(): ITerminalQuickFixOptions {
return {
commandLineMatcher: GitCommandLineRegex,
quickFixLabel: (matchResult: QuickFixMatchResult) => matchResult.outputMatch ? `Run git ${matchResult.outputMatch[1]}` : ``,
outputMatcher: {
lineMatcher: GitSimilarOutputRegex,
anchor: 'bottom',
Expand All @@ -35,10 +36,11 @@ export function gitSimilarCommand(): ITerminalQuickFixOptions {
if (!fixedCommand) {
return;
}
const label = localize("terminal.gitSimilarCommand", "Run git {0}", fixedCommand);
const commandToRunInTerminal = `git ${fixedCommand}`;
const label = localize("terminal.runCommand", "Run: {0}", commandToRunInTerminal);
actions.push({
class: undefined, tooltip: label, id: 'terminal.gitSimilarCommand', label, enabled: true,
commandToRunInTerminal: `git ${fixedCommand}`,
commandToRunInTerminal,
addNewLine: true,
run: () => { }
});
Expand All @@ -48,7 +50,6 @@ export function gitSimilarCommand(): ITerminalQuickFixOptions {
}
export function freePort(terminalInstance?: Partial<ITerminalInstance>): ITerminalQuickFixOptions {
return {
quickFixLabel: (matchResult: QuickFixMatchResult) => matchResult.outputMatch ? `Free port ${matchResult.outputMatch[1]}` : '',
commandLineMatcher: AnyCommandLineRegex,
// TODO: Support free port on Windows https://github.com/microsoft/vscode/issues/161775
outputMatcher: isWindows ? undefined : {
Expand Down Expand Up @@ -79,7 +80,6 @@ export function freePort(terminalInstance?: Partial<ITerminalInstance>): ITermin
}
export function gitPushSetUpstream(): ITerminalQuickFixOptions {
return {
quickFixLabel: (matchResult: QuickFixMatchResult) => matchResult.outputMatch ? `Git push ${matchResult.outputMatch[1]}` : '',
commandLineMatcher: GitPushCommandLineRegex,
outputMatcher: {
lineMatcher: GitPushOutputRegex,
Expand All @@ -94,11 +94,11 @@ export function gitPushSetUpstream(): ITerminalQuickFixOptions {
return;
}
const actions: ITerminalQuickFixAction[] = [];
const label = localize("terminal.gitPush", "Git push {0}", branch);
command.command = `git push --set-upstream origin ${branch}`;
const commandToRunInTerminal = `git push --set-upstream origin ${branch}`;
const label = localize("terminal.runCommand", "Run: {0}", commandToRunInTerminal);
actions.push({
class: undefined, tooltip: label, id: 'terminal.gitPush', label, enabled: true,
commandToRunInTerminal: command.command,
commandToRunInTerminal,
addNewLine: true,
run: () => { }
});
Expand All @@ -109,7 +109,6 @@ export function gitPushSetUpstream(): ITerminalQuickFixOptions {

export function gitCreatePr(openerService: IOpenerService): ITerminalQuickFixOptions {
return {
quickFixLabel: (matchResult: QuickFixMatchResult) => matchResult.outputMatch ? `Create PR for ${matchResult.outputMatch[1]}` : '',
commandLineMatcher: GitPushCommandLineRegex,
outputMatcher: {
lineMatcher: GitCreatePrOutputRegex,
Expand All @@ -122,13 +121,12 @@ export function gitCreatePr(openerService: IOpenerService): ITerminalQuickFixOpt
if (!command) {
return;
}
const branch = matchResult?.outputMatch?.[1];
const link = matchResult?.outputMatch?.[2];
if (!branch || !link) {
const link = matchResult?.outputMatch?.[1];
if (!link) {
return;
}
const actions: IAction[] = [];
const label = localize("terminal.gitCreatePr", "Create PR");
const label = localize("terminal.openLink", "Open link: {0}", link);
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
actions.push({
class: undefined, tooltip: label, id: 'terminal.gitCreatePr', label, enabled: true,
run: () => openerService.open(link)
Expand Down
Expand Up @@ -56,9 +56,9 @@ suite('QuickFixAddon', () => {
const actions = [
{
id: 'terminal.gitSimilarCommand',
label: 'Run git status',
label: 'Run: git status',
run: true,
tooltip: 'Run git status',
tooltip: 'Run: git status',
enabled: true
}
];
Expand Down Expand Up @@ -136,9 +136,9 @@ suite('QuickFixAddon', () => {
const actions = [
{
id: 'terminal.gitPush',
label: 'Git push test22',
label: 'Run: git push --set-upstream origin test22',
run: true,
tooltip: 'Git push test22',
tooltip: 'Run: git push --set-upstream origin test22',
enabled: true
}
];
Expand Down Expand Up @@ -179,9 +179,9 @@ suite('QuickFixAddon', () => {
const actions = [
{
id: 'terminal.gitCreatePr',
label: 'Create PR',
label: 'Open link: https://github.com/meganrogge/xterm.js/pull/new/test22',
run: true,
tooltip: 'Create PR',
tooltip: 'Open link: https://github.com/meganrogge/xterm.js/pull/new/test22',
enabled: true
}
];
Expand Down Expand Up @@ -219,9 +219,9 @@ suite('QuickFixAddon', () => {
const actions = [
{
id: 'terminal.gitPush',
label: 'Git push test22',
label: 'Run: git push --set-upstream origin test22',
run: true,
tooltip: 'Git push test22',
tooltip: 'Run: git push --set-upstream origin test22',
enabled: true
}
];
Expand Down