From dc83ca19512d0e18ca39ca5b6b0c0a5519f0377f Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Fri, 2 Dec 2022 08:20:53 -0800 Subject: [PATCH] npm: adopt terminal quick fix api (#167664) Fixes https://github.com/microsoft/vscode/issues/167588 --- extensions/npm/package.json | 21 ++++++++++++++++++++- extensions/npm/src/npmMain.ts | 27 +++++++++++++++++++++++++++ extensions/npm/tsconfig.json | 3 ++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/extensions/npm/package.json b/extensions/npm/package.json index 40ec7e7f4ae..03a60de5622 100644 --- a/extensions/npm/package.json +++ b/extensions/npm/package.json @@ -12,6 +12,9 @@ "categories": [ "Other" ], + "enabledApiProposals": [ + "terminalQuickFixProvider" + ], "scripts": { "compile": "gulp compile-extension:npm", "watch": "gulp watch-extension:npm" @@ -334,7 +337,23 @@ }, "when": "shellExecutionSupported" } - ] + ], + "terminal": { + "quickFixes": [ + { + "id": "ms-vscode.npm-command", + "commandLineMatcher": "npm", + "exitStatus": false, + "outputMatcher": { + "anchor": "bottom", + "length": 8, + "lineMatcher": "Did you mean (?:this|one of these)\\?((?:\\n.+?npm .+ #.+)+)", + "offset": 2, + "multipleMatches": true + } + } + ] + } }, "repository": { "type": "git", diff --git a/extensions/npm/src/npmMain.ts b/extensions/npm/src/npmMain.ts index a8289599917..4c815b8e689 100644 --- a/extensions/npm/src/npmMain.ts +++ b/extensions/npm/src/npmMain.ts @@ -70,6 +70,33 @@ export async function activate(context: vscode.ExtensionContext): Promise return ''; })); context.subscriptions.push(new NpmScriptLensProvider()); + + context.subscriptions.push(vscode.window.registerTerminalQuickFixProvider('ms-vscode.npm-command', { + provideTerminalQuickFixes({ outputMatch }) { + if (!outputMatch) { + return; + } + + const lines = outputMatch.regexMatch[1]; + const fixes: vscode.TerminalQuickFixCommandAction[] = []; + for (const line of lines.split('\n')) { + // search from the second char, since the lines might be prefixed with + // "npm ERR!" which comes before the actual command suggestion. + const begin = line.indexOf('npm', 1); + if (begin === -1) { + continue; + } + + const end = line.lastIndexOf('#'); + fixes.push({ + type: vscode.TerminalQuickFixType.command, + terminalCommand: line.slice(begin, end === -1 ? undefined : end - 1) + }); + } + + return fixes; + }, + })); } async function getNPMCommandPath(): Promise { diff --git a/extensions/npm/tsconfig.json b/extensions/npm/tsconfig.json index 7234fdfeb97..ec12eb547b3 100644 --- a/extensions/npm/tsconfig.json +++ b/extensions/npm/tsconfig.json @@ -8,6 +8,7 @@ }, "include": [ "src/**/*", - "../../src/vscode-dts/vscode.d.ts" + "../../src/vscode-dts/vscode.d.ts", + "../../src/vscode-dts/vscode.proposed.terminalQuickFixProvider.d.ts", ] }