Skip to content

Commit

Permalink
Implement vscode command terminal quick fixes
Browse files Browse the repository at this point in the history
Part of #162950
  • Loading branch information
Tyriar committed Aug 16, 2023
1 parent 3f8a55b commit 4047859
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 31 deletions.
7 changes: 4 additions & 3 deletions src/vs/workbench/api/browser/mainThreadTerminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteA
import { OperatingSystem, OS } from 'vs/base/common/platform';
import { TerminalEditorLocationOptions } from 'vscode';
import { Promises } from 'vs/base/common/async';
import { TerminalQuickFixType } from 'vs/workbench/api/common/extHostTypes';
import { ISerializableEnvironmentDescriptionMap, ISerializableEnvironmentVariableCollection } from 'vs/platform/terminal/common/environmentVariable';
import { ITerminalLinkProviderService } from 'vs/workbench/contrib/terminalContrib/links/browser/links';
import { ITerminalQuickFixService, ITerminalQuickFix } from 'vs/workbench/contrib/terminalContrib/quickFix/browser/quickFix';
import { ITerminalQuickFixService, ITerminalQuickFix, TerminalQuickFixType } from 'vs/workbench/contrib/terminalContrib/quickFix/browser/quickFix';


@extHostNamedCustomer(MainContext.MainThreadTerminalService)
Expand Down Expand Up @@ -448,10 +447,12 @@ export function getOutputMatchForLines(lines: string[], outputMatcher: ITerminal
}

function parseQuickFix(id: string, source: string, fix: TerminalQuickFix): ITerminalQuickFix {
let type = TerminalQuickFixType.Command;
let type = TerminalQuickFixType.TerminalCommand;
if ('uri' in fix) {
fix.uri = URI.revive(fix.uri);
type = TerminalQuickFixType.Opener;
} else if ('id' in fix) {
type = TerminalQuickFixType.VscodeCommand;
}
return { id, type, source, ...fix };
}
1 change: 0 additions & 1 deletion src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,6 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
TerminalLocation: extHostTypes.TerminalLocation,
TerminalProfile: extHostTypes.TerminalProfile,
TerminalExitReason: extHostTypes.TerminalExitReason,
TerminalQuickFixType: extHostTypes.TerminalQuickFixType,
TextDocumentSaveReason: extHostTypes.TextDocumentSaveReason,
TextEdit: extHostTypes.TextEdit,
SnippetTextEdit: extHostTypes.SnippetTextEdit,
Expand Down
5 changes: 3 additions & 2 deletions src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ export enum TerminalOutputAnchor {
}

export enum TerminalQuickFixType {
Command = 0,
Opener = 1
TerminalCommand = 0,
Opener = 1,
Command = 3
}

@es5ClassCompat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface ITerminalQuickFixProviderSelector {
provider: ITerminalQuickFixProvider;
}

export type TerminalQuickFixActionInternal = IAction | ITerminalQuickFixCommandAction | ITerminalQuickFixOpenerAction;
export type TerminalQuickFixActionInternal = IAction | ITerminalQuickFixExecuteTerminalCommandAction | ITerminalQuickFixOpenerAction;
export type TerminalQuickFixCallback = (matchResult: ITerminalCommandMatchResult) => TerminalQuickFixActionInternal[] | TerminalQuickFixActionInternal | undefined;
export type TerminalQuickFixCallbackExtension = (terminalCommand: ITerminalCommand, lines: string[] | undefined, option: ITerminalQuickFixOptions, token: CancellationToken) => Promise<ITerminalQuickFix[] | ITerminalQuickFix | undefined>;

Expand All @@ -44,9 +44,10 @@ export interface ITerminalQuickFixProvider {
}

export enum TerminalQuickFixType {
Command = 0,
TerminalCommand = 0,
Opener = 1,
Port = 2
Port = 2,
VscodeCommand = 3
}

export interface ITerminalQuickFixOptions {
Expand All @@ -63,8 +64,8 @@ export interface ITerminalQuickFix {
source: string;
}

export interface ITerminalQuickFixCommandAction extends ITerminalQuickFix {
type: TerminalQuickFixType.Command;
export interface ITerminalQuickFixExecuteTerminalCommandAction extends ITerminalQuickFix {
type: TerminalQuickFixType.TerminalCommand;
terminalCommand: string;
// TODO: Should this depend on whether alt is held?
addNewLine?: boolean;
Expand All @@ -73,6 +74,9 @@ export interface ITerminalQuickFixOpenerAction extends ITerminalQuickFix {
type: TerminalQuickFixType.Opener;
uri: URI;
}
export interface ITerminalQuickFixCommandAction extends ITerminalQuickFix {
title: string;
}

export interface ITerminalCommandMatchResult {
commandLine: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import { IAnchor } from 'vs/base/browser/ui/contextview/contextview';
import { ILabelService } from 'vs/platform/label/common/label';
import { Schemas } from 'vs/base/common/network';
import { URI } from 'vs/base/common/uri';
import { ITerminalQuickFixInternalOptions, ITerminalQuickFixResolvedExtensionOptions, ITerminalQuickFix, ITerminalQuickFixCommandAction, ITerminalQuickFixOpenerAction, ITerminalQuickFixOptions, ITerminalQuickFixProviderSelector, ITerminalQuickFixService, ITerminalQuickFixUnresolvedExtensionOptions, TerminalQuickFixType } from 'vs/workbench/contrib/terminalContrib/quickFix/browser/quickFix';
import { ITerminalQuickFixInternalOptions, ITerminalQuickFixResolvedExtensionOptions, ITerminalQuickFix, ITerminalQuickFixExecuteTerminalCommandAction, ITerminalQuickFixOpenerAction, ITerminalQuickFixOptions, ITerminalQuickFixProviderSelector, ITerminalQuickFixService, ITerminalQuickFixUnresolvedExtensionOptions, TerminalQuickFixType, ITerminalQuickFixCommandAction } from 'vs/workbench/contrib/terminalContrib/quickFix/browser/quickFix';
import { ITerminalCommandSelector } from 'vs/platform/terminal/common/terminal';
import { ActionListItemKind, IActionListItem } from 'vs/platform/actionWidget/browser/actionList';
import { CodeActionKind } from 'vs/editor/contrib/codeAction/common/types';
import { Codicon } from 'vs/base/common/codicons';
import { ThemeIcon } from 'vs/base/common/themables';
import { ICommandService } from 'vs/platform/commands/common/commands';

const quickFixSelectors = [
DecorationSelector.QuickFix,
Expand Down Expand Up @@ -75,6 +76,7 @@ export class TerminalQuickFixAddon extends Disposable implements ITerminalAddon,
private readonly _aliases: string[][] | undefined,
private readonly _capabilities: ITerminalCapabilityStore,
@ITerminalQuickFixService private readonly _quickFixService: ITerminalQuickFixService,
@ICommandService private readonly _commandService: ICommandService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IAudioCueService private readonly _audioCueService: IAudioCueService,
@IOpenerService private readonly _openerService: IOpenerService,
Expand Down Expand Up @@ -192,7 +194,7 @@ export class TerminalQuickFixAddon extends Disposable implements ITerminalAddon,
await this._extensionService.activateByEvent(`onTerminalQuickFixRequest:${id}`);
return this._quickFixService.providers.get(id)?.provideTerminalQuickFixes(command, lines, { type: 'resolved', commandLineMatcher: selector.commandLineMatcher, outputMatcher: selector.outputMatcher, commandExitResult: selector.commandExitResult, id: selector.id }, new CancellationTokenSource().token);
};
const result = await getQuickFixesForCommand(aliases, terminal, command, this._commandListeners, this._openerService, this._labelService, this._onDidRequestRerunCommand, resolver);
const result = await getQuickFixesForCommand(aliases, terminal, command, this._commandListeners, this._commandService, this._openerService, this._labelService, this._onDidRequestRerunCommand, resolver);
if (!result) {
return;
}
Expand Down Expand Up @@ -293,6 +295,7 @@ export async function getQuickFixesForCommand(
terminal: Terminal,
terminalCommand: ITerminalCommand,
quickFixOptions: Map<string, ITerminalQuickFixOptions[]>,
commandService: ICommandService,
openerService: IOpenerService,
labelService: ILabelService,
onDidRequestRerunCommand?: Emitter<{ command: string; addNewLine?: boolean }>,
Expand Down Expand Up @@ -339,15 +342,15 @@ export async function getQuickFixesForCommand(
let action: ITerminalAction | undefined;
if ('type' in quickFix) {
switch (quickFix.type) {
case TerminalQuickFixType.Command: {
const fix = quickFix as ITerminalQuickFixCommandAction;
case TerminalQuickFixType.TerminalCommand: {
const fix = quickFix as ITerminalQuickFixExecuteTerminalCommandAction;
if (commandQuickFixSet.has(fix.terminalCommand)) {
continue;
}
commandQuickFixSet.add(fix.terminalCommand);
const label = localize('quickFix.command', 'Run: {0}', fix.terminalCommand);
action = {
type: TerminalQuickFixType.Command,
type: TerminalQuickFixType.TerminalCommand,
class: undefined,
source: quickFix.source,
id: quickFix.id,
Expand Down Expand Up @@ -405,6 +408,20 @@ export async function getQuickFixesForCommand(
};
break;
}
case TerminalQuickFixType.VscodeCommand: {
const fix = quickFix as ITerminalQuickFixCommandAction;
action = {
source: quickFix.source,
type: fix.type,
id: fix.id,
label: fix.title,
class: undefined,
enabled: true,
run: () => commandService.executeCommand(fix.id),
tooltip: fix.title
};
break;
}
}
if (action) {
fixes.push(action);
Expand Down Expand Up @@ -477,9 +494,11 @@ function getQuickFixIcon(quickFix: TerminalQuickFixItem): ThemeIcon {
const isUrl = (quickFix.action.uri.scheme === Schemas.http || quickFix.action.uri.scheme === Schemas.https);
return isUrl ? Codicon.linkExternal : Codicon.goToFile;
}
case TerminalQuickFixType.Command:
case TerminalQuickFixType.TerminalCommand:
return Codicon.run;
case TerminalQuickFixType.Port:
return Codicon.debugDisconnect;
case TerminalQuickFixType.VscodeCommand:
return Codicon.lightbulb;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { URI } from 'vs/base/common/uri';
import { localize } from 'vs/nls';
import { ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ITerminalQuickFixInternalOptions, ITerminalCommandMatchResult, ITerminalQuickFixCommandAction, TerminalQuickFixActionInternal, TerminalQuickFixType } from 'vs/workbench/contrib/terminalContrib/quickFix/browser/quickFix';
import { ITerminalQuickFixInternalOptions, ITerminalCommandMatchResult, ITerminalQuickFixExecuteTerminalCommandAction, TerminalQuickFixActionInternal, TerminalQuickFixType } from 'vs/workbench/contrib/terminalContrib/quickFix/browser/quickFix';

export const GitCommandLineRegex = /git/;
export const GitPushCommandLineRegex = /git\s+push/;
Expand Down Expand Up @@ -49,7 +49,7 @@ export function gitSimilar(): ITerminalQuickFixInternalOptions {
if (fixedCommand) {
actions.push({
id: 'Git Similar',
type: TerminalQuickFixType.Command,
type: TerminalQuickFixType.TerminalCommand,
terminalCommand: matchResult.commandLine.replace(/git\s+[^\s]+/, () => `git ${fixedCommand}`),
addNewLine: true,
source: QuickFixSource.Builtin
Expand Down Expand Up @@ -79,7 +79,7 @@ export function gitTwoDashes(): ITerminalQuickFixInternalOptions {
return;
}
return {
type: TerminalQuickFixType.Command,
type: TerminalQuickFixType.TerminalCommand,
id: 'Git Two Dashes',
terminalCommand: matchResult.commandLine.replace(` -${problemArg}`, () => ` --${problemArg}`),
addNewLine: true,
Expand Down Expand Up @@ -175,7 +175,7 @@ export function gitPushSetUpstream(): ITerminalQuickFixInternalOptions {
}
if (fixedCommand) {
actions.push({
type: TerminalQuickFixType.Command,
type: TerminalQuickFixType.TerminalCommand,
id: 'Git Push Set Upstream',
terminalCommand: fixedCommand,
addNewLine: true,
Expand Down Expand Up @@ -268,11 +268,11 @@ export function pwshGeneralError(): ITerminalQuickFixInternalOptions {
if (!suggestions) {
return;
}
const result: ITerminalQuickFixCommandAction[] = [];
const result: ITerminalQuickFixExecuteTerminalCommandAction[] = [];
for (const suggestion of suggestions) {
result.push({
id: 'Pwsh General Error',
type: TerminalQuickFixType.Command,
type: TerminalQuickFixType.TerminalCommand,
terminalCommand: suggestion,
source: QuickFixSource.Builtin
});
Expand Down Expand Up @@ -314,7 +314,7 @@ export function pwshUnixCommandNotFoundError(): ITerminalQuickFixInternalOptions
}

// Always remove the first element as it's the "Suggestion [cmd-not-found]"" line
const result: ITerminalQuickFixCommandAction[] = [];
const result: ITerminalQuickFixExecuteTerminalCommandAction[] = [];
let inSuggestions = false;
for (; i < lines.length; i++) {
const line = lines[i].trim();
Expand All @@ -325,7 +325,7 @@ export function pwshUnixCommandNotFoundError(): ITerminalQuickFixInternalOptions
if (installCommand) {
result.push({
id: 'Pwsh Unix Command Not Found Error',
type: TerminalQuickFixType.Command,
type: TerminalQuickFixType.TerminalCommand,
terminalCommand: installCommand,
source: QuickFixSource.Builtin
});
Expand All @@ -339,7 +339,7 @@ export function pwshUnixCommandNotFoundError(): ITerminalQuickFixInternalOptions
if (inSuggestions) {
result.push({
id: 'Pwsh Unix Command Not Found Error',
type: TerminalQuickFixType.Command,
type: TerminalQuickFixType.TerminalCommand,
terminalCommand: line.trim(),
source: QuickFixSource.Builtin
});
Expand Down
5 changes: 0 additions & 5 deletions src/vscode-dts/vscode.proposed.terminalQuickFixProvider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,4 @@ declare module 'vscode' {
Top = 0,
Bottom = 1
}

enum TerminalQuickFixType {
Command = 0,
Opener = 1
}
}

0 comments on commit 4047859

Please sign in to comment.