|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +import { inject, named } from 'inversify'; |
| 7 | +import { DiagnosticSeverity } from 'vscode'; |
| 8 | +import { IWorkspaceService } from '../../../common/application/types'; |
| 9 | +import { CODE_RUNNER_EXTENSION_ID } from '../../../common/constants'; |
| 10 | +import { DeprecatePythonPath } from '../../../common/experimentGroups'; |
| 11 | +import { IDisposableRegistry, IExperimentsManager, IExtensions, Resource } from '../../../common/types'; |
| 12 | +import { Common, Diagnostics } from '../../../common/utils/localize'; |
| 13 | +import { IServiceContainer } from '../../../ioc/types'; |
| 14 | +import { BaseDiagnostic, BaseDiagnosticsService } from '../base'; |
| 15 | +import { IDiagnosticsCommandFactory } from '../commands/types'; |
| 16 | +import { DiagnosticCodes } from '../constants'; |
| 17 | +import { DiagnosticCommandPromptHandlerServiceId, MessageCommandPrompt } from '../promptHandler'; |
| 18 | +import { DiagnosticScope, IDiagnostic, IDiagnosticHandlerService } from '../types'; |
| 19 | + |
| 20 | +export class UpgradeCodeRunnerDiagnostic extends BaseDiagnostic { |
| 21 | + constructor(message: string, resource: Resource) { |
| 22 | + super( |
| 23 | + DiagnosticCodes.UpgradeCodeRunnerDiagnostic, |
| 24 | + message, |
| 25 | + DiagnosticSeverity.Information, |
| 26 | + DiagnosticScope.Global, |
| 27 | + resource |
| 28 | + ); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export const UpgradeCodeRunnerDiagnosticServiceId = 'UpgradeCodeRunnerDiagnosticServiceId'; |
| 33 | + |
| 34 | +export class UpgradeCodeRunnerDiagnosticService extends BaseDiagnosticsService { |
| 35 | + public _diagnosticReturned: boolean = false; |
| 36 | + private workspaceService: IWorkspaceService; |
| 37 | + constructor( |
| 38 | + @inject(IServiceContainer) serviceContainer: IServiceContainer, |
| 39 | + @inject(IDiagnosticHandlerService) |
| 40 | + @named(DiagnosticCommandPromptHandlerServiceId) |
| 41 | + protected readonly messageService: IDiagnosticHandlerService<MessageCommandPrompt>, |
| 42 | + @inject(IDisposableRegistry) disposableRegistry: IDisposableRegistry, |
| 43 | + @inject(IExtensions) private readonly extensions: IExtensions |
| 44 | + ) { |
| 45 | + super([DiagnosticCodes.UpgradeCodeRunnerDiagnostic], serviceContainer, disposableRegistry, true); |
| 46 | + this.workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService); |
| 47 | + } |
| 48 | + public async diagnose(resource: Resource): Promise<IDiagnostic[]> { |
| 49 | + if (this._diagnosticReturned) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + const experiments = this.serviceContainer.get<IExperimentsManager>(IExperimentsManager); |
| 53 | + experiments.sendTelemetryIfInExperiment(DeprecatePythonPath.control); |
| 54 | + if (!experiments.inExperiment(DeprecatePythonPath.experiment)) { |
| 55 | + return []; |
| 56 | + } |
| 57 | + const extension = this.extensions.getExtension(CODE_RUNNER_EXTENSION_ID); |
| 58 | + if (!extension) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + const flagValue: boolean | undefined = extension.packageJSON?.featureFlags?.usingNewPythonInterpreterPathApi; |
| 62 | + if (flagValue) { |
| 63 | + // Using new version of Code runner already, no need to upgrade |
| 64 | + return []; |
| 65 | + } |
| 66 | + const pythonExecutor = this.workspaceService |
| 67 | + .getConfiguration('code-runner', resource) |
| 68 | + .get<string>('executorMap.python'); |
| 69 | + if (pythonExecutor?.includes('$pythonPath')) { |
| 70 | + this._diagnosticReturned = true; |
| 71 | + return [new UpgradeCodeRunnerDiagnostic(Diagnostics.upgradeCodeRunner(), resource)]; |
| 72 | + } |
| 73 | + return []; |
| 74 | + } |
| 75 | + |
| 76 | + protected async onHandle(diagnostics: IDiagnostic[]): Promise<void> { |
| 77 | + if (diagnostics.length === 0 || !(await this.canHandle(diagnostics[0]))) { |
| 78 | + return; |
| 79 | + } |
| 80 | + const diagnostic = diagnostics[0]; |
| 81 | + if (await this.filterService.shouldIgnoreDiagnostic(diagnostic.code)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + const commandFactory = this.serviceContainer.get<IDiagnosticsCommandFactory>(IDiagnosticsCommandFactory); |
| 85 | + const options = [ |
| 86 | + { |
| 87 | + prompt: Common.doNotShowAgain(), |
| 88 | + command: commandFactory.createCommand(diagnostic, { type: 'ignore', options: DiagnosticScope.Global }) |
| 89 | + } |
| 90 | + ]; |
| 91 | + |
| 92 | + await this.messageService.handle(diagnostic, { commandPrompts: options }); |
| 93 | + } |
| 94 | +} |
0 commit comments