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

Allow opt-in multi-line quick fixes #162189

Merged
merged 2 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 @@ -490,7 +490,7 @@ export class CommandDetectionCapability implements ICommandDetectionCapability {
commandStartLineContent: this._currentCommand.commandStartLineContent,
hasOutput: () => !executedMarker?.isDisposed && !endMarker?.isDisposed && !!(executedMarker && endMarker && executedMarker?.line < endMarker!.line),
getOutput: () => getOutputForCommand(executedMarker, endMarker, buffer),
getOutputMatch: (outputMatcher?: { lineMatcher: string | RegExp; anchor?: 'top' | 'bottom'; offset?: number; length?: number }) => getOutputMatchForCommand(executedMarker, endMarker, buffer, this._terminal.cols, outputMatcher),
getOutputMatch: (outputMatcher: { lineMatcher: string | RegExp; anchor?: 'top' | 'bottom'; offset?: number; length?: number }) => getOutputMatchForCommand(executedMarker, endMarker, buffer, this._terminal.cols, outputMatcher),
markProperties: options?.markProperties
};
this._commands.push(newCommand);
Expand Down Expand Up @@ -647,43 +647,35 @@ function getOutputForCommand(executedMarker: IMarker | undefined, endMarker: IMa
return output === '' ? undefined : output;
}

export function getOutputMatchForCommand(executedMarker: IMarker | undefined, endMarker: IMarker | undefined, buffer: IBuffer, cols: number, outputMatcher: { lineMatcher: string | RegExp; anchor?: 'top' | 'bottom'; offset?: number; length?: number } | undefined): RegExpMatchArray | undefined {
export function getOutputMatchForCommand(executedMarker: IMarker | undefined, endMarker: IMarker | undefined, buffer: IBuffer, cols: number, outputMatcher: { lineMatcher: string | RegExp; anchor?: 'top' | 'bottom'; offset?: number; length?: number }): RegExpMatchArray | undefined {
if (!executedMarker || !endMarker) {
return undefined;
}
const startLine = executedMarker.line;
const endLine = endMarker.line;

if (startLine === endLine) {
return undefined;
}

let line: string | undefined;
if (outputMatcher?.anchor === 'bottom') {
const matcher = outputMatcher.lineMatcher;
const linesToCheck = typeof matcher === 'string' ? 1 : countNewLines(matcher);
const lines: string[] = [];
if (outputMatcher.anchor === 'bottom') {
for (let i = endLine - (outputMatcher.offset || 0); i >= startLine; i--) {
let wrappedLineStart = i;
const wrappedLineEnd = i;
while (wrappedLineStart >= startLine && buffer.getLine(wrappedLineStart)?.isWrapped) {
wrappedLineStart--;
lines.unshift(getXtermLineContent(buffer, i, i, cols));
if (lines.length > linesToCheck) {
lines.pop();
}
i = wrappedLineStart;
line = getXtermLineContent(buffer, wrappedLineStart, wrappedLineEnd, cols);
const match = line.match(outputMatcher.lineMatcher);
const match = lines.join('\n').match(matcher);
if (match) {
return match;
}
}
} else {
for (let i = startLine + (outputMatcher?.offset || 0); i < endLine; i++) {
const wrappedLineStart = i;
let wrappedLineEnd = i;
while (wrappedLineEnd + 1 < endLine && buffer.getLine(wrappedLineEnd + 1)?.isWrapped) {
wrappedLineEnd++;
for (let i = startLine + (outputMatcher.offset || 0); i < endLine; i++) {
lines.push(getXtermLineContent(buffer, i, i, cols));
if (lines.length === linesToCheck) {
lines.shift();
}
i = wrappedLineEnd;
line = getXtermLineContent(buffer, wrappedLineStart, wrappedLineEnd, cols);
if (outputMatcher) {
const match = line.match(outputMatcher.lineMatcher);
const match = lines.join('\n').match(matcher);
if (match) {
return match;
}
Expand All @@ -709,3 +701,17 @@ function getXtermLineContent(buffer: IBuffer, lineStart: number, lineEnd: number
}
return content;
}

function countNewLines(regex: RegExp): number {
if (!regex.multiline) {
return 1;
}
const source = regex.source;
let count = 1;
let i = source.indexOf('\\n');
while (i !== -1) {
count++;
i = source.indexOf('\\n', i + 1);
}
return count;
}
25 changes: 1 addition & 24 deletions src/vs/workbench/contrib/terminal/browser/terminal.ts
Expand Up @@ -20,7 +20,7 @@ import { IEditableData } from 'vs/workbench/common/views';
import { TerminalFindWidget } from 'vs/workbench/contrib/terminal/browser/terminalFindWidget';
import { ITerminalStatusList } from 'vs/workbench/contrib/terminal/browser/terminalStatusList';
import { ITerminalQuickFix } from 'vs/workbench/contrib/terminal/browser/xterm/quickFixAddon';
import { INavigationMode, IRegisterContributedProfileArgs, IRemoteTerminalAttachTarget, IStartExtensionTerminalRequest, ITerminalBackend, ITerminalConfigHelper, ITerminalFont, ITerminalProcessExtHostProxy } from 'vs/workbench/contrib/terminal/common/terminal';
import { INavigationMode, IRegisterContributedProfileArgs, IRemoteTerminalAttachTarget, IStartExtensionTerminalRequest, ITerminalBackend, ITerminalConfigHelper, ITerminalFont, ITerminalOutputMatcher, ITerminalProcessExtHostProxy } from 'vs/workbench/contrib/terminal/common/terminal';
import { EditorGroupColumn } from 'vs/workbench/services/editor/common/editorGroupColumn';
import { IMarker } from 'xterm';

Expand Down Expand Up @@ -933,29 +933,6 @@ export interface ITerminalQuickFixAction extends IAction {
addNewLine?: boolean;
}

/**
* A matcher that runs on a sub-section of a terminal command's output
*/
export interface ITerminalOutputMatcher {
/**
* A string or regex to match against the unwrapped line.
*/
lineMatcher: string | RegExp;
/**
* Which side of the output to anchor the {@link offset} and {@link length} against.
*/
anchor: 'top' | 'bottom';
/**
* How far from either the top or the bottom of the butter to start matching against.
*/
offset: number;
/**
* The number of rows to match against, this should be as small as possible for performance
* reasons.
*/
length: number;
}

export interface IXtermTerminal {
/**
* An object that tracks when commands are run and enables navigating and selecting between
Expand Down
Expand Up @@ -12,7 +12,7 @@ import { ITerminalCommand } from 'vs/workbench/contrib/terminal/common/terminal'
export const GitCommandLineRegex = /git/;
export const GitPushCommandLineRegex = /git\s+push/;
export const AnyCommandLineRegex = /.+/;
export const GitSimilarOutputRegex = /most similar command is\s*([^\s]{3,})/;
export const GitSimilarOutputRegex = /most similar command is\s*\n\s*([^\s]{3,})/m;
export const FreePortOutputRegex = /address already in use \d+\.\d+\.\d+\.\d+:(\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 ([^\s]+)/;
// The previous line starts with "Create a pull request for \'([^\s]+)\' on GitHub by visiting:\s*",
Expand Down
23 changes: 20 additions & 3 deletions src/vs/workbench/contrib/terminal/common/terminal.ts
Expand Up @@ -96,11 +96,28 @@ export interface IShellLaunchConfigResolveOptions {
allowAutomationShell?: boolean;
}

/**
* A matcher that runs on a sub-section of a terminal command's output
*/
export interface ITerminalOutputMatcher {
/**
* A string or regex to match against the unwrapped line. If this is a regex with the multiline
* flag, it will scan an amount of lines equal to `\n` instances in the regex + 1.
*/
lineMatcher: string | RegExp;
anchor?: 'top' | 'bottom';
offset?: number;
length?: number;
/**
* Which side of the output to anchor the {@link offset} and {@link length} against.
*/
anchor: 'top' | 'bottom';
/**
* How far from either the top or the bottom of the butter to start matching against.
*/
offset: number;
/**
* The number of rows to match against, this should be as small as possible for performance
* reasons.
*/
length: number;
}

export interface ITerminalBackend {
Expand Down
Expand Up @@ -14,9 +14,10 @@ import { IOpenerService } from 'vs/platform/opener/common/opener';
import { ITerminalCommand, TerminalCapability } from 'vs/platform/terminal/common/capabilities/capabilities';
import { CommandDetectionCapability } from 'vs/platform/terminal/common/capabilities/commandDetectionCapability';
import { TerminalCapabilityStore } from 'vs/platform/terminal/common/capabilities/terminalCapabilityStore';
import { ITerminalQuickFixAction, ITerminalInstance, ITerminalOutputMatcher } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ITerminalQuickFixAction, ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';
import { freePort, FreePortOutputRegex, gitCreatePr, GitCreatePrOutputRegex, GitPushOutputRegex, gitPushSetUpstream, gitSimilarCommand, GitSimilarOutputRegex } from 'vs/workbench/contrib/terminal/browser/terminalQuickFixBuiltinActions';
import { TerminalQuickFixAddon, getQuickFixes } from 'vs/workbench/contrib/terminal/browser/xterm/quickFixAddon';
import { ITerminalOutputMatcher } from 'vs/workbench/contrib/terminal/common/terminal';
import { Terminal } from 'xterm';

suite('QuickFixAddon', () => {
Expand Down