Skip to content

Commit f7479fa

Browse files
authored
Show formatter extension install prompt and not the old prompt (#21138)
1 parent e0558ce commit f7479fa

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/client/formatters/baseFormatter.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import { IPythonToolExecutionService } from '../common/process/types';
88
import { IDisposableRegistry, IInstaller, Product } from '../common/types';
99
import { isNotebookCell } from '../common/utils/misc';
1010
import { IServiceContainer } from '../ioc/types';
11-
import { traceError, traceLog } from '../logging';
11+
import { traceError } from '../logging';
1212
import { getTempFileWithDocumentContents, getTextEditsFromPatch } from './../common/editor';
1313
import { IFormatterHelper } from './types';
1414
import { IInstallFormatterPrompt } from '../providers/prompts/types';
1515

1616
export abstract class BaseFormatter {
1717
protected readonly workspace: IWorkspaceService;
1818
private readonly helper: IFormatterHelper;
19+
private errorShown: boolean = false;
1920

2021
constructor(public Id: string, private product: Product, protected serviceContainer: IServiceContainer) {
2122
this.helper = serviceContainer.get<IFormatterHelper>(IFormatterHelper);
@@ -101,23 +102,22 @@ export abstract class BaseFormatter {
101102
}
102103

103104
protected async handleError(_expectedFileName: string, error: Error, resource?: vscode.Uri) {
104-
let customError = `Formatting with ${this.Id} failed.`;
105-
106105
if (isNotInstalledError(error)) {
107106
const prompt = this.serviceContainer.get<IInstallFormatterPrompt>(IInstallFormatterPrompt);
108107
if (!(await prompt.showInstallFormatterPrompt(resource))) {
109108
const installer = this.serviceContainer.get<IInstaller>(IInstaller);
110109
const isInstalled = await installer.isInstalled(this.product, resource);
111-
if (!isInstalled) {
112-
customError += `\nYou could either install the '${this.Id}' formatter, turn it off or use another formatter.`;
113-
installer
114-
.promptToInstall(this.product, resource)
115-
.catch((ex) => traceError('Python Extension: promptToInstall', ex));
110+
if (!isInstalled && !this.errorShown) {
111+
traceError(
112+
`\nPlease install '${this.Id}' into your environment.`,
113+
"\nIf you don't want to use it you can turn it off or use another formatter in the settings.",
114+
);
115+
this.errorShown = true;
116116
}
117117
}
118118
}
119119

120-
traceLog(`\n${customError}\n${error}`);
120+
traceError(`Formatting with ${this.Id} failed:\n${error}`);
121121
}
122122

123123
/**

src/client/providers/prompts/installFormatterPrompt.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,21 @@ const SHOW_FORMATTER_INSTALL_PROMPT_DONOTSHOW_KEY = 'showFormatterExtensionInsta
2121

2222
@injectable()
2323
export class InstallFormatterPrompt implements IInstallFormatterPrompt {
24-
private shownThisSession = false;
24+
private currentlyShown = false;
2525

2626
constructor(@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer) {}
2727

28+
/*
29+
* This method is called when the user saves a python file or a cell.
30+
* Returns true if an extension was selected. Otherwise returns false.
31+
*/
2832
public async showInstallFormatterPrompt(resource?: Uri): Promise<boolean> {
2933
if (!inFormatterExtensionExperiment(this.serviceContainer)) {
3034
return false;
3135
}
3236

3337
const promptState = doNotShowPromptState(SHOW_FORMATTER_INSTALL_PROMPT_DONOTSHOW_KEY, this.serviceContainer);
34-
if (this.shownThisSession || promptState.value) {
38+
if (this.currentlyShown || promptState.value) {
3539
return false;
3640
}
3741

@@ -53,7 +57,7 @@ export class InstallFormatterPrompt implements IInstallFormatterPrompt {
5357
let selection: string | undefined;
5458

5559
if (black || autopep8) {
56-
this.shownThisSession = true;
60+
this.currentlyShown = true;
5761
if (black && autopep8) {
5862
selection = await showInformationMessage(
5963
ToolsExtensions.selectMultipleFormattersPrompt,
@@ -81,15 +85,15 @@ export class InstallFormatterPrompt implements IInstallFormatterPrompt {
8185
}
8286
}
8387
} else if (formatter === 'black' && !black) {
84-
this.shownThisSession = true;
88+
this.currentlyShown = true;
8589
selection = await showInformationMessage(
8690
ToolsExtensions.installBlackFormatterPrompt,
8791
'Black',
8892
'Autopep8',
8993
Common.doNotShowAgain,
9094
);
9195
} else if (formatter === 'autopep8' && !autopep8) {
92-
this.shownThisSession = true;
96+
this.currentlyShown = true;
9397
selection = await showInformationMessage(
9498
ToolsExtensions.installAutopep8FormatterPrompt,
9599
'Black',
@@ -98,23 +102,32 @@ export class InstallFormatterPrompt implements IInstallFormatterPrompt {
98102
);
99103
}
100104

105+
let userSelectedAnExtension = false;
101106
if (selection === 'Black') {
102107
if (black) {
108+
userSelectedAnExtension = true;
103109
await updateDefaultFormatter(BLACK_EXTENSION, resource);
104110
} else {
111+
userSelectedAnExtension = true;
105112
await installFormatterExtension(BLACK_EXTENSION, resource);
106113
}
107114
} else if (selection === 'Autopep8') {
108115
if (autopep8) {
116+
userSelectedAnExtension = true;
109117
await updateDefaultFormatter(AUTOPEP8_EXTENSION, resource);
110118
} else {
119+
userSelectedAnExtension = true;
111120
await installFormatterExtension(AUTOPEP8_EXTENSION, resource);
112121
}
113122
} else if (selection === Common.doNotShowAgain) {
123+
userSelectedAnExtension = false;
114124
await promptState.updateValue(true);
125+
} else {
126+
userSelectedAnExtension = false;
115127
}
116128

117-
return this.shownThisSession;
129+
this.currentlyShown = false;
130+
return userSelectedAnExtension;
118131
}
119132
}
120133

0 commit comments

Comments
 (0)