Skip to content

Commit

Permalink
Use new formatter install prompt on missing formatter (#20904)
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig authored Mar 27, 2023
1 parent 43a21a2 commit d809af4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
18 changes: 11 additions & 7 deletions src/client/formatters/baseFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { IServiceContainer } from '../ioc/types';
import { traceError, traceLog } from '../logging';
import { getTempFileWithDocumentContents, getTextEditsFromPatch } from './../common/editor';
import { IFormatterHelper } from './types';
import { IInstallFormatterPrompt } from '../providers/prompts/types';

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

if (isNotInstalledError(error)) {
const installer = this.serviceContainer.get<IInstaller>(IInstaller);
const isInstalled = await installer.isInstalled(this.product, resource);
if (!isInstalled) {
customError += `\nYou could either install the '${this.Id}' formatter, turn it off or use another formatter.`;
installer
.promptToInstall(this.product, resource)
.catch((ex) => traceError('Python Extension: promptToInstall', ex));
const prompt = this.serviceContainer.get<IInstallFormatterPrompt>(IInstallFormatterPrompt);
if (!(await prompt.showInstallFormatterPrompt(resource))) {
const installer = this.serviceContainer.get<IInstaller>(IInstaller);
const isInstalled = await installer.isInstalled(this.product, resource);
if (!isInstalled) {
customError += `\nYou could either install the '${this.Id}' formatter, turn it off or use another formatter.`;
installer
.promptToInstall(this.product, resource)
.catch((ex) => traceError('Python Extension: promptToInstall', ex));
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions src/client/providers/prompts/installFormatterPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

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

const SHOW_FORMATTER_INSTALL_PROMPT_DONOTSHOW_KEY = 'showFormatterExtensionInstallPrompt';

@injectable()
export class InstallFormatterPrompt implements IInstallFormatterPrompt {
private shownThisSession = false;

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

public async showInstallFormatterPrompt(resource?: Uri): Promise<void> {
public async showInstallFormatterPrompt(resource?: Uri): Promise<boolean> {
if (!inFormatterExtensionExperiment(this.serviceContainer)) {
return;
return false;
}

const promptState = doNotShowPromptState(SHOW_FORMATTER_INSTALL_PROMPT_DONOTSHOW_KEY, this.serviceContainer);
if (this.shownThisSession || promptState.value) {
return;
return false;
}

const config = getConfiguration('python', resource);
const formatter = config.get<string>('formatting.provider', 'none');
if (!['autopep8', 'black'].includes(formatter)) {
return;
return false;
}

const editorConfig = getConfiguration('editor', { uri: resource, languageId: 'python' });
const defaultFormatter = editorConfig.get<string>('defaultFormatter', '');
if ([BLACK_EXTENSION, AUTOPEP8_EXTENSION].includes(defaultFormatter)) {
return;
return false;
}

const black = isExtensionEnabled(BLACK_EXTENSION);
Expand Down Expand Up @@ -111,12 +113,14 @@ export class InstallFormatterPrompt implements IInstallFormatterPrompt {
} else if (selection === Common.doNotShowAgain) {
await promptState.updateValue(true);
}

return this.shownThisSession;
}
}

export function registerInstallFormatterPrompt(serviceContainer: IServiceContainer): void {
const disposables = serviceContainer.get<IDisposableRegistry>(IDisposableRegistry);
const installFormatterPrompt = new InstallFormatterPrompt(serviceContainer);
const installFormatterPrompt = serviceContainer.get<IInstallFormatterPrompt>(IInstallFormatterPrompt);
disposables.push(
onDidSaveTextDocument(async (e) => {
const editorConfig = getConfiguration('editor', { uri: e.uri, languageId: 'python' });
Expand Down
5 changes: 4 additions & 1 deletion src/client/providers/prompts/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { Uri } from 'vscode';

export const BLACK_EXTENSION = 'ms-python.black-formatter';
export const AUTOPEP8_EXTENSION = 'ms-python.autopep8';

export const IInstallFormatterPrompt = Symbol('IInstallFormatterPrompt');
export interface IInstallFormatterPrompt {
showInstallFormatterPrompt(): Promise<void>;
showInstallFormatterPrompt(resource?: Uri): Promise<boolean>;
}
3 changes: 3 additions & 0 deletions src/client/providers/serviceRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import { IExtensionSingleActivationService } from '../activation/types';
import { IServiceManager } from '../ioc/types';
import { CodeActionProviderService } from './codeActionProvider/main';
import { InstallFormatterPrompt } from './prompts/installFormatterPrompt';
import { IInstallFormatterPrompt } from './prompts/types';

export function registerTypes(serviceManager: IServiceManager): void {
serviceManager.addSingleton<IExtensionSingleActivationService>(
IExtensionSingleActivationService,
CodeActionProviderService,
);
serviceManager.addSingleton<IInstallFormatterPrompt>(IInstallFormatterPrompt, InstallFormatterPrompt);
}

0 comments on commit d809af4

Please sign in to comment.