Skip to content

Commit d809af4

Browse files
authored
Use new formatter install prompt on missing formatter (#20904)
1 parent 43a21a2 commit d809af4

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

src/client/formatters/baseFormatter.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { IServiceContainer } from '../ioc/types';
1111
import { traceError, traceLog } from '../logging';
1212
import { getTempFileWithDocumentContents, getTextEditsFromPatch } from './../common/editor';
1313
import { IFormatterHelper } from './types';
14+
import { IInstallFormatterPrompt } from '../providers/prompts/types';
1415

1516
export abstract class BaseFormatter {
1617
protected readonly workspace: IWorkspaceService;
@@ -103,13 +104,16 @@ export abstract class BaseFormatter {
103104
let customError = `Formatting with ${this.Id} failed.`;
104105

105106
if (isNotInstalledError(error)) {
106-
const installer = this.serviceContainer.get<IInstaller>(IInstaller);
107-
const isInstalled = await installer.isInstalled(this.product, resource);
108-
if (!isInstalled) {
109-
customError += `\nYou could either install the '${this.Id}' formatter, turn it off or use another formatter.`;
110-
installer
111-
.promptToInstall(this.product, resource)
112-
.catch((ex) => traceError('Python Extension: promptToInstall', ex));
107+
const prompt = this.serviceContainer.get<IInstallFormatterPrompt>(IInstallFormatterPrompt);
108+
if (!(await prompt.showInstallFormatterPrompt(resource))) {
109+
const installer = this.serviceContainer.get<IInstaller>(IInstaller);
110+
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));
116+
}
113117
}
114118
}
115119

src/client/providers/prompts/installFormatterPrompt.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
import { Uri } from 'vscode';
5+
import { inject, injectable } from 'inversify';
56
import { IDisposableRegistry } from '../../common/types';
67
import { Common, ToolsExtensions } from '../../common/utils/localize';
78
import { isExtensionEnabled } from '../../common/vscodeApis/extensionsApi';
@@ -18,31 +19,32 @@ import { AUTOPEP8_EXTENSION, BLACK_EXTENSION, IInstallFormatterPrompt } from './
1819

1920
const SHOW_FORMATTER_INSTALL_PROMPT_DONOTSHOW_KEY = 'showFormatterExtensionInstallPrompt';
2021

22+
@injectable()
2123
export class InstallFormatterPrompt implements IInstallFormatterPrompt {
2224
private shownThisSession = false;
2325

24-
constructor(private readonly serviceContainer: IServiceContainer) {}
26+
constructor(@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer) {}
2527

26-
public async showInstallFormatterPrompt(resource?: Uri): Promise<void> {
28+
public async showInstallFormatterPrompt(resource?: Uri): Promise<boolean> {
2729
if (!inFormatterExtensionExperiment(this.serviceContainer)) {
28-
return;
30+
return false;
2931
}
3032

3133
const promptState = doNotShowPromptState(SHOW_FORMATTER_INSTALL_PROMPT_DONOTSHOW_KEY, this.serviceContainer);
3234
if (this.shownThisSession || promptState.value) {
33-
return;
35+
return false;
3436
}
3537

3638
const config = getConfiguration('python', resource);
3739
const formatter = config.get<string>('formatting.provider', 'none');
3840
if (!['autopep8', 'black'].includes(formatter)) {
39-
return;
41+
return false;
4042
}
4143

4244
const editorConfig = getConfiguration('editor', { uri: resource, languageId: 'python' });
4345
const defaultFormatter = editorConfig.get<string>('defaultFormatter', '');
4446
if ([BLACK_EXTENSION, AUTOPEP8_EXTENSION].includes(defaultFormatter)) {
45-
return;
47+
return false;
4648
}
4749

4850
const black = isExtensionEnabled(BLACK_EXTENSION);
@@ -111,12 +113,14 @@ export class InstallFormatterPrompt implements IInstallFormatterPrompt {
111113
} else if (selection === Common.doNotShowAgain) {
112114
await promptState.updateValue(true);
113115
}
116+
117+
return this.shownThisSession;
114118
}
115119
}
116120

117121
export function registerInstallFormatterPrompt(serviceContainer: IServiceContainer): void {
118122
const disposables = serviceContainer.get<IDisposableRegistry>(IDisposableRegistry);
119-
const installFormatterPrompt = new InstallFormatterPrompt(serviceContainer);
123+
const installFormatterPrompt = serviceContainer.get<IInstallFormatterPrompt>(IInstallFormatterPrompt);
120124
disposables.push(
121125
onDidSaveTextDocument(async (e) => {
122126
const editorConfig = getConfiguration('editor', { uri: e.uri, languageId: 'python' });

src/client/providers/prompts/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
import { Uri } from 'vscode';
5+
46
export const BLACK_EXTENSION = 'ms-python.black-formatter';
57
export const AUTOPEP8_EXTENSION = 'ms-python.autopep8';
68

9+
export const IInstallFormatterPrompt = Symbol('IInstallFormatterPrompt');
710
export interface IInstallFormatterPrompt {
8-
showInstallFormatterPrompt(): Promise<void>;
11+
showInstallFormatterPrompt(resource?: Uri): Promise<boolean>;
912
}

src/client/providers/serviceRegistry.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import { IExtensionSingleActivationService } from '../activation/types';
77
import { IServiceManager } from '../ioc/types';
88
import { CodeActionProviderService } from './codeActionProvider/main';
9+
import { InstallFormatterPrompt } from './prompts/installFormatterPrompt';
10+
import { IInstallFormatterPrompt } from './prompts/types';
911

1012
export function registerTypes(serviceManager: IServiceManager): void {
1113
serviceManager.addSingleton<IExtensionSingleActivationService>(
1214
IExtensionSingleActivationService,
1315
CodeActionProviderService,
1416
);
17+
serviceManager.addSingleton<IInstallFormatterPrompt>(IInstallFormatterPrompt, InstallFormatterPrompt);
1518
}

0 commit comments

Comments
 (0)