Support pwsh unix command not found error

Fixes #170484
This commit is contained in:
Daniel Imms 2023-01-09 10:35:37 -08:00
parent 04dc8ba26a
commit 6877e7d265
No known key found for this signature in database
GPG key ID: E5CF412B63651C69
2 changed files with 63 additions and 8 deletions

View file

@ -59,7 +59,7 @@ import { IDetectedLinks, TerminalLinkManager } from 'vs/workbench/contrib/termin
import { TerminalLinkQuickpick } from 'vs/workbench/contrib/terminal/browser/links/terminalLinkQuickpick';
import { IRequestAddInstanceToGroupEvent, ITerminalExternalLinkProvider, ITerminalInstance, TerminalDataTransfers } from 'vs/workbench/contrib/terminal/browser/terminal';
import { TerminalLaunchHelpAction } from 'vs/workbench/contrib/terminal/browser/terminalActions';
import { freePort, gitCreatePr, gitPushSetUpstream, gitSimilar, gitTwoDashes, pwshGeneralCommandErrorFeedback as pwshGeneralCommandError } from 'vs/workbench/contrib/terminal/browser/terminalQuickFixBuiltinActions';
import { freePort, gitCreatePr, gitPushSetUpstream, gitSimilar, gitTwoDashes, pwshGeneralError as pwshGeneralError, pwshUnixCommandNotFoundError } from 'vs/workbench/contrib/terminal/browser/terminalQuickFixBuiltinActions';
import { TerminalConfigHelper } from 'vs/workbench/contrib/terminal/browser/terminalConfigHelper';
import { TerminalEditorInput } from 'vs/workbench/contrib/terminal/browser/terminalEditorInput';
import { TerminalFindWidget } from 'vs/workbench/contrib/terminal/browser/terminalFindWidget';
@ -739,11 +739,11 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
this._quickFixAddon = this._scopedInstantiationService.createInstance(TerminalQuickFixAddon, this._aliases, this.capabilities);
this.xterm?.raw.loadAddon(this._quickFixAddon);
this._registerQuickFixProvider(this._quickFixAddon, gitTwoDashes(), freePort(this), gitSimilar(), gitPushSetUpstream(), gitCreatePr());
if ((this.os || OS) === OperatingSystem.Linux) {
// this.registerQuickFixProvider(pwshUnixCommandNotFound);
} else {
this._registerQuickFixProvider(this._quickFixAddon, pwshGeneralCommandError());
this._registerQuickFixProvider(this._quickFixAddon, pwshUnixCommandNotFoundError());
// Avoid the general provide on Linux as the unix command not found error provider is
// generally preferable.
if ((this.os || OS) !== OperatingSystem.Linux) {
this._registerQuickFixProvider(this._quickFixAddon, pwshGeneralError());
}
this._register(this._quickFixAddon.onDidRequestRerunCommand(async (e) => await this.runCommand(e.command, e.addNewLine || false)));
this.updateAccessibilitySupport();

View file

@ -194,9 +194,9 @@ export function gitCreatePr(): IInternalOptions {
};
}
export function pwshGeneralCommandErrorFeedback(): IInternalOptions {
export function pwshGeneralError(): IInternalOptions {
return {
id: 'Pwsh General Command Error Feedback',
id: 'Pwsh General Error',
type: 'internal',
commandLineMatcher: /.+/,
outputMatcher: {
@ -225,3 +225,58 @@ export function pwshGeneralCommandErrorFeedback(): IInternalOptions {
}
};
}
export function pwshUnixCommandNotFoundError(): IInternalOptions {
return {
id: 'Unix Command Not Found',
type: 'internal',
commandLineMatcher: /.+/,
outputMatcher: {
lineMatcher: /^Suggestion \[cmd-not-found\]:/,
anchor: 'bottom',
offset: 0,
length: 10
},
commandExitResult: 'error',
getQuickFixes: (matchResult: ITerminalCommandMatchResult) => {
const lines = matchResult.outputMatch?.regexMatch.input?.split('\n');
if (!lines) {
return;
}
const result: ITerminalQuickFixCommandAction[] = [];
// Always remove the first element as it's the "Suggestion [cmd-not-found]"" line
let inSuggestions = false;
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (line.length === 0) {
break;
}
const installCommand = line.match(/You also have .+ installed, you can run '(?<command>.+)' instead./)?.groups?.command;
if (installCommand) {
result.push({
id: 'Pwsh Unix Command Not Found Error',
type: TerminalQuickFixType.Command,
terminalCommand: installCommand,
source: QuickFixSource.Builtin
});
inSuggestions = false;
continue;
}
if (line.match(/Command '.+' not found, but can be installed with:/)) {
inSuggestions = true;
continue;
}
if (inSuggestions) {
result.push({
id: 'Pwsh Unix Command Not Found Error',
type: TerminalQuickFixType.Command,
terminalCommand: line.trim(),
source: QuickFixSource.Builtin
});
}
}
return result;
}
};
}